I want to get rid off hardcoded passwords in my lambda that is deployed to AWS. I found I shall modify packaged.yaml
:
Parameters:
DATABASE_URI:
Description: 'Required. MongoDB connection URL'
Type: 'String'
Resources:
BUDAuthorizeUserHandler:
Type: AWS::Serverless::Function
Properties:
FunctionName: BUDAuthorizeUserHandler
Handler: src/handlers/users/authorizeUser.handler
Runtime: nodejs10.x
Environment:
Variables:
MONGODB_URI: !Ref DATABASE_URI
This is the usage:
const MONGODB_URI = process.env.MONGODB_URI;
console.log(MONGODB_URI);
So far so good and according to the specification. But I spent two hours trying to make it work locally.
Configuration file env.json
{
"BUDAuthorizeUserHandler": {
"MONGODB_URI": "mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true&w=majority"
}
}
I tried these options but the environment variable was never defined:
sam local start-api --env-vars env.json
sam local start-api --parameter-overrides ParameterKey=DATABASE_URI,ParameterValue="mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true&w=majority"
I have walked through these pages:
https://github.com/awslabs/aws-sam-cli/issues/1163 aws-sam-local environment variables How do I specify template parameters when running AWS SAM Local? Setting environmental variables with !Ref in AWS SAM?
SAM CLI, version 0.39.0
How to make it work? What do I do wrong?