https://serverless-stack.com/ logo
#help
Title
# help
s

Shubham Sinha

05/19/2022, 6:28 PM
I need help structuring the stacks. I am using go for lamda functions Following this https://serverless-stack.com/chapters/review-our-app-architecture.html article I created StorageStack to create DynamoDb table and S3 bucket. Now I want to use the table in my functions and for doing that I need to pass table name ( https://serverless-stack.com/examples/how-to-use-dynamodb-in-your-serverless-app.html ) how to I get access to the table defined in StorageStack in ApiStack ? Should I move table instantiation to ApiStack itself ?
t

thdxr

05/19/2022, 6:30 PM
hey so we're in the process of migrating all our examples to 1.0
but check this out, it documents generally ho wyou share resources across stacks: https://docs.serverless-stack.com/constructs/Stack#sharing-resources-between-stacks
s

Shubham Sinha

05/19/2022, 6:32 PM
Thanks
@thdxr https://serverless-stack.com/examples/how-to-create-a-rest-api-in-golang-with-serverless.html For every function we have package as main and a main function. This is causing an error
t

thdxr

05/19/2022, 6:56 PM
with go each function needs to be in its own folder
we should maybe make that more clear in our starter
m

Mohiuddin Sumon

05/20/2022, 9:30 AM
@Shubham Sinha have you tried to make one main function and in it just pass them to wherever required
a

Adrian Schweizer

05/20/2022, 10:16 AM
with regards to the initial question, I'm doing something like this for multiple tables:
Copy code
export const StorageStack = ({ stack }) => {

   const bucket = new Bucket(stack, "Uploads", {
      // bucket config
   });

   const tables = {
      organisations: new Table(stack, "Organisations", {
         // table config
      }),
      participations: new Table(stack, "Participations", {
         // table config
      }),
      // more tables
   };

   // Collect table names to later add them to process environment for lambda functions
   const tableNames = Object.keys(tables).reduce((tableNames, key) => ({
      ...tableNames,
      ["TABLE_NAME_" + key.toUpperCase()]: tables[key].tableName,
   }), {});

   return { bucket, tables, tableNames };
}
and then in api stack:
Copy code
export function ApiStack({ stack, app }) {
   const { bucket, tables, tableNames } = use(StorageStack);

   const api = new Api(stack, "api", {
      defaults: {
         function: {
            environment: {
               ...tableNames,
            },
         },
      },
and then in lambda I can use table names like this:
Copy code
process.env.TABLE_NAME_PARTICIPATIONS
2 Views