aws-cdk: (Lambda): Lambda environment variables that reference updated "parameters" are not updated when redeploying the stack
Describe the bug
I have a lambda full of env variables referencing a “parameter”, like the one shown below
![img]
INFO: pay attention that I’m loading the right .env file as shown in the above image. I always set NODE_ENV before I ran any cdk command. And I print the .env path to make sure the right one was selected when building things locally.
![img]
The above screenshots show a template that was synthesized by CDK after I changed the .env file that provide values to my parameters. However, even after deploying this new stack, CDK did not change my env variable values in my lambda functions. In the image below you can see that “ALLOWED_ORIGINS” was not updated.
![img]
Another weird thing is the CDK doesnt even realize that this change will happen. Below you can see the output of cdk diff for the same stack.
Expected Behavior
CDK must update Lambda Env Variables values that reference parameters whenever they are changed.
Current Behavior
CDK does not update Lambda Env Variables values when they reference parameters that have changed
Reproduction Steps
-
create a construct with one lambda
-
create a cfn parameter that is loaded with values from a .env file. You can use dotenv. like I did. ![img] ![img]
-
add an env variable to your lambda, and fill its value with the value from the parameter.
![img]
- synth and deploy it. Open the synthesized template and verify the parameter value is the same one from your .env.
- change the .env value
- synth. Again verify the synthesized template and verify that it has changed, and it again matches the one from your .env.
- deploy it and verify that the value was not changed in AWS
- conclude it is a bug
Possible Solution
Couldn’t find any woraround
Additional Information/Context
No response
CDK CLI Version
2.138.0
Framework Version
No response
Node.js Version
node 18
OS
macos latest
Language
TypeScript
Language Version
typescript@v5.2.2
Other information
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (7 by maintainers)
For testing purposes, without using CDK:
This created Lambda along with environment variable
ALLOWED_ORIGINShaving valuehttp://localhost:8080,http://testdomain.com.allowedOriginstohttp://localhost:8080,http://testdomain.com,http://makemydomain.com. Deployed the below template (updated CloudFormation stack to use a new template):During AWS console workflow, in Specify stack details step showing there was a checkbox
Use previous valueinParameterssection. By default, this checkbox was checked. In the last step Review testlambdastack, it displayed messageNo updates are to be performed..Went back to Specify stack details step and unchecked the checkbox
Use previous value. Unchecking the checkbox blanked out the value (could be a UI issue since it should have read updated value from uploaded YAML file). Hence, manually specified valuehttp://localhost:8080,http://testdomain.com,http://makemydomain.comforallowedOriginsparameter.Going to last Review testlambdastack now displayed Lambda function to be modified in
Change set previewsection.Submitting the changes updated the Lambda function with environment variable
ALLOWED_ORIGINSchanged tohttp://localhost:8080,http://testdomain.com,http://makemydomain.com.So, by default, any changes to CloudFormation parameter are not used (based on observation in AWS console UI).
If we look at the CDK guide Deploying with parameters, is explicitly specifies that
By default, the AWS CDK retains values of parameters from previous deployments and uses them in subsequent deployments if they are not specified explicitly. Use the --no-previous-parameters flag to require all parameters to be specified.(this may be due to limitation of CloudFormation).Going with the above understanding, using
cdk deploy --no-previous-parameterson 2nd deployment updated the value of environment variable in Lambda configuration.@AllanOricil Please use
cdk deploy --no-previous-parametersto use the updated value of CloudFormation parameter.Thanks, Ashish
@AllanOricil Based on my observation in last https://github.com/aws/aws-cdk/issues/30101#issuecomment-2103062508, one possible reasoning is one-to-one parity with CloudFormation UI in AWS console. By default, in AWS console, checkbox
Use previous valueis checked. This is just an assumption. Also, if we change the default behavior, it might break some small set of customers where by default it is currently assumed that new value forCfnParameterwould not be used by default.@AllanOricil Thanks for pointing it out. I modified the code to below and the issue is now reproducible:
It produces below CloudFormation template (as an example), with environment variable as
ReftoallowedOriginsparameter:Running
cdk diff(before 2nd deployment), produces the following output:I’m unsure though if this is a CDK issue or CloudFormation issue since the synthesized template is handled by CloudFormation, and CloudFormation allows declaring parameters.
Need to discuss this with team.
Thanks, Ashish