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 5 months 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 2 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 5 years ago
- asked 9 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 9 months ago
- AWS OFFICIALUpdated 3 years ago
That's already done. The problem is, if I skip
--env-vars env.json
part from thesam local invoke
command thenprocess.env.DYNAMODB_TABLE_NAME
value 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.env
automatically 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.json
for my local invokes to be automatic.