> For the complete documentation index, see [llms.txt](https://tricks.bodik.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tricks.bodik.tech/infrastructure/aws/sam/adding-environment-variables-to-lambda-function-resource-using-sam.md).

# Adding environment variables to Lambda function resource using SAM

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

```yaml
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.

{% embed url="<https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-lambda.Function.html>" %}
