Adding environment variables to Lambda function resource using SAM

An example of how to add Lambda function in template.yaml of your SAM configuration

YourLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Description: Description of your function
      Runtime: nodejs16.x
      Environment:
        Variables:
          VAR_1: my_value
          NODE_ENV: production
          SOME_DYNAMIC_VAR: !Select [1, !Split ['/', !GetAtt YourDynamoDBTable.Arn]] 
      Architectures:
        - x86_64
      Handler: index.handler
      MemorySize: 128
      Timeout: 100

The first two variables in the example are static. But you can also create a dynamic variables based on the resources you already described. In SOME_DYNAMIC_VAR variable you can see an example on getting your DynamoDB table name dynamically and passing it to your Lambda function.

More info on the available properties for the Lambda function creation is here. I use CDK documentation, as I have not found related to SAM resources.

Last updated