I have a problem with `addDefaultFunctionEnv` Meth...
# help
a
I have a problem with
addDefaultFunctionEnv
Method of app Object. I'm trying to bulk pass table names from storage stack to lambdas by using the method like so: StorageStack:
Copy code
export default class StorageStack extends sst.Stack {
   // Use public fields for reference in other stacks
   tables;

   constructor(scope, id, props) {
      super(scope, id, props);

      this.tables = {
         users: new sst.Table(this, "Users", {
            fields: {
               // fieldspecs
            },
            // more table specs
         }),
         organisations: new sst.Table(this, "Organisations", {
            fields: {
               // fieldspecs
            },
            // more table specs
         }),
         ... etc.
index.js:
Copy code
export default function main(app) {
   const storageStack = new StorageStack(app, "storage");

   // Add table names to process environment for lambda functions
   const tableNames = {};
   Object.keys(storageStack.tables).forEach((key) => {
      const name = 'TABLE_NAME_' + key.toUpperCase();
      tableNames[name] = storageStack.tables[key].tableName;
   });
   console.log(tableNames);
   app.addDefaultFunctionEnv(tableNames); // If i comment this line out, deployment works
   ... etc.
the tableNames object that is passed into the function looks like so:
Copy code
{
  TABLE_NAME_USERS: '${Token[TOKEN.190]}',
  TABLE_NAME_ORGANISATIONS: '${Token[TOKEN.197]}',
  TABLE_NAME_EVENTS: '${Token[TOKEN.204]}',
  TABLE_NAME_PARTICIPATIONS: '${Token[TOKEN.211]}'
}
t
Hey Adrian - the table names aren't actually known when your code is executed because they might not have been created yet. This is why you're seeing placeholders
However, it should resolve fine when deployed
a
it doesn't deploy, though, it aborts with an error
t
can you share the error?
a
Copy code
dev-convento-storage | UPDATE_FAILED | AWS::CloudFormation::Stack | dev-convento-storage 

 ❌  dev-convento-storage failed: Stack dev-convento-storage failed to deploy: UPDATE_FAILED


Stack dev-convento-storage
  Status: failed
  Error: Stack dev-convento-storage failed to deploy: UPDATE_FAILED
t
I think the actual error should be printed a bit further up
a
this is the whole output:
Copy code
adsc@tp13:~/projects/convento$ npx sst start
Using stage: dev
Preparing your SST app
Transpiling source
Linting source

=======================
 Deploying debug stack
=======================

Deploying stacks

 ✅  dev-convento-debug-stack (no changes)


Stack dev-convento-debug-stack
  Status: no changes
  Outputs:
    BucketArn: arn:aws:s3:::dev-convento-debug-stack-bucket83908e77-1gxag50oy9bj7
    BucketName: dev-convento-debug-stack-bucket83908e77-1gxag50oy9bj7
    Endpoint: <wss://tzcbop6u9j.execute-api.eu-central-1.amazonaws.com/dev>


===============
 Deploying app
===============

{
  TABLE_NAME_USERS: '${Token[TOKEN.190]}',
  TABLE_NAME_ORGANISATIONS: '${Token[TOKEN.197]}',
  TABLE_NAME_EVENTS: '${Token[TOKEN.204]}',
  TABLE_NAME_PARTICIPATIONS: '${Token[TOKEN.211]}'
}
Deploying stacks
Checking deploy status...
dev-convento-storage | UPDATE_IN_PROGRESS | AWS::CloudFormation::Stack | dev-convento-storage 
dev-convento-storage | UPDATE_IN_PROGRESS | AWS::CloudFormation::Stack | dev-convento-storage 
dev-convento-storage | UPDATE_FAILED | AWS::CloudFormation::Stack | dev-convento-storage 

 ❌  dev-convento-storage failed: Stack dev-convento-storage failed to deploy: UPDATE_FAILED


Stack dev-convento-storage
  Status: failed
  Error: Stack dev-convento-storage failed to deploy: UPDATE_FAILED
  Outputs:
    ExportsOutputFnGetAttEventsTable4B7491D3ArnC8522F39: arn:aws:dynamodb:eu-central-1:787845945917:table/dev-convento-Events
    ExportsOutputFnGetAttOrganisationsTableB3508D80ArnE9F35618: arn:aws:dynamodb:eu-central-1:787845945917:table/dev-convento-Organisations
    ExportsOutputFnGetAttParticipationsTableDBCD3320ArnF53D0D65: arn:aws:dynamodb:eu-central-1:787845945917:table/dev-convento-Participations
    ExportsOutputFnGetAttUploadsBucketC4B27CC7Arn515ECA45: arn:aws:s3:::dev-convento-storage-uploadsbucketc4b27cc7-tz4dlxcwm83z
    ExportsOutputFnGetAttUsersTable6D91A5EEArnF6868322: arn:aws:dynamodb:eu-central-1:787845945917:table/dev-convento-Users
    ExportsOutputRefUploadsBucketC4B27CC7CCC94A6B: dev-convento-storage-uploadsbucketc4b27cc7-tz4dlxcwm83z
  Exports:
    dev-convento-storage:ExportsOutputFnGetAttEventsTable4B7491D3ArnC8522F39: arn:aws:dynamodb:eu-central-1:787845945917:table/dev-convento-Events
    dev-convento-storage:ExportsOutputFnGetAttOrganisationsTableB3508D80ArnE9F35618: arn:aws:dynamodb:eu-central-1:787845945917:table/dev-convento-Organisations
    dev-convento-storage:ExportsOutputFnGetAttParticipationsTableDBCD3320ArnF53D0D65: arn:aws:dynamodb:eu-central-1:787845945917:table/dev-convento-Participations
    dev-convento-storage:ExportsOutputFnGetAttUploadsBucketC4B27CC7Arn515ECA45: arn:aws:s3:::dev-convento-storage-uploadsbucketc4b27cc7-tz4dlxcwm83z
    dev-convento-storage:ExportsOutputFnGetAttUsersTable6D91A5EEArnF6868322: arn:aws:dynamodb:eu-central-1:787845945917:table/dev-convento-Users
    dev-convento-storage:ExportsOutputRefUploadsBucketC4B27CC7CCC94A6B: dev-convento-storage-uploadsbucketc4b27cc7-tz4dlxcwm83z

Stack dev-convento-api
  Status: not deployed
Cannot convert undefined or null to object
the tableNames object is a console log i inserted in index.js
i also checked in sst-debug.log...it doesn't tell anything more
t
can you check in the aws console ui in cloudformation
to see if there's a more clear error? I feel like for some reason the actual error is missing
I suspect it has something to do with a cross stack dependency but I can't tell yet
THose exports it's listing are the resolved values of the TOKEN stuff
a
where would I find this error? it looks like this:
t
under the events tab
check both storage + api stack
a
could it be this:
Copy code
Export with name dev-convento-storage:ExportsOutputRefUsersTable6D91A5EE2FC965A7 is already exported by stack dev-convento-storage
t
ah there it is
is this for sst start or sst deploy?
a
start
and the error disappears if I remove the
app.addDefaultFunctionEnv
call
t
Can you try the following.
sst deploy --stack api
And send me the output of it
Also I suspect deleting both stacks and rerunning sst start would also fix it. You're stuck in this circular exports issue that CFN has, a bit hard to explain but it happens sometimes
a
Copy code
adsc@tp13:~/projects/convento$ npx sst deploy --stack api
Using stage: dev
Preparing your SST app
Transpiling source
Linting source
Building Lambda function src/users/list.main
Building Lambda function src/users/get.main
Building Lambda function src/users/create.main
Building Lambda function src/users/update.main
Building Lambda function src/users/delete.main
Building Lambda function src/organisations/post.main
Building Lambda function src/events/post.main
Building Lambda function src/participations/post.main
Building Lambda function src/registrations/post.main
 > node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:43:28: error: Could not resolve "mock-aws-s3" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
    43 │     const AWSMock = require('mock-aws-s3');
       ╵                             ~~~~~~~~~~~~~

 > node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:112:23: error: Could not resolve "nock" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
    112 │   const nock = require('nock');
        ╵                        ~~~~~~


There was a problem transpiling the Lambda handler.

There was an error synthesizing your app.
t
ok this is a separate issue - try deleting the stacks and running
sst start
again
a
with deleting the stacks you mean sst remove?
or remove them from index.js?
t
I was thinking the UI but any of those work
a
i started delete over ui, but it seems to take forever
still not deleted
something is fucked up I think
the stacks are still in status delete_in_progress...but i started a new stage, and I do it a bit differently, now...I simply pass the tableNames object directly into the api stacks and then merge them into the environments there
instead of using
app.addDefaultFunctionEnv
in index.js
i still don't have to manually update anything when I add new tables, so I guess I achieved my original goal
thanks for your support @thdxr
t
Hm seems like those stacks got into a funky state
strange
glad you figured it out 👍🏽
m
Co-worker just ran into this issue. Generic UPDATE_FAILED state. The template in cloudformation is different than what is being built. On deploy it says no changes but cdk diff shows many changes. Tried to change a logical id to get it to see the changes and it broke the whole stack.
a
hmmm, i still can't deploy this stack, i have to use another stage. "remove" removes everything cleanly, as far as I can see, but then I get this error upon npx sst start:
Copy code
dev-convento-storage | CREATE_FAILED | AWS::CloudFormation::Stack | dev-convento-storage Export with name dev-convento-storage:ExportsOutputRefUsersTable6D91A5EE2FC965A7 is already exported by stack dev-convento-storage
then if i try again, I get this:
Copy code
dev-convento-api | CREATE_FAILED | AWS::CloudFormation::Stack | dev-convento-api No export named dev-convento-storage:ExportsOutputFnGetAttParticipationsTableDBCD3320ArnF53D0D65 found
when i remove and try again, the first error pops up again
ah, i see now that there is a trailing export in cloudformation console that somehow doesn't get removed