3 Answers
- Newest
- Most votes
- Most comments
1
It's not well documented but any env vars that you want to override via --env-vars must be defined in the SAM template. I too had this problem until I finally ran across https://stackoverflow.com/questions/48104665/aws-sam-local-environment-variables/48139216#48139216.
answered 2 years ago
0
If you want to use the generated table name in the stack you can use a cloudformation construct called !Ref and set an environment variable for the lambda function that can be used. As indicated here, dynamo outputs the name of the table that you can then reference in your lambda function as an env variable. Here is an example.
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
....
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
Variables:
DYNAMODB_TABLE_NAME: !Ref DynamoDBTable
DynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
answered 4 years ago
0
Try putting the function name at the end of the invoke line:
sam local invoke -e events/event-post-item.json --profile myprofile -n local.json putItemFunction
Relevant content
- asked 2 years ago
- asked 6 years ago
- asked 2 years ago

That's already done. The problem is, if I skip
--env-vars env.jsonpart from thesam local invokecommand thenprocess.env.DYNAMODB_TABLE_NAMEvalue I get is justDynamoDBTable. Not the correct name of database that was generated with the stack, ex:DynamoDBTable-T69PQM8X9UBJ...I guess I understand what's going on. I can't get the generated DB name to be sent to the
process.envautomatically when invoking locally. I have to get the generated names from the deploy output and pipe it to some script to be injected it intoenv.jsonfor my local invokes to be automatic.