Reputation: 21
When I add an environment variable to a aws-lambda through the aws-lambda console, I am able to reference these variables using:
import os
# ...
print("environment variable: " + os.environ['variable'])
How would I use an environment variable in aws-lambda within a cloudformation template? I don't want to declare the environment variable in the aws-lambda console.
Thanks
Upvotes: 1
Views: 5694
Reputation: 578
If you are using YAML template for declaring the cloud formation configuration, then you can define your environment variable under Parameters like:
Parameters:
Domain: {Type: String, Default: 'test', AllowedValues: ['test', 'gamma', 'prod']}
...
And then use it where you have declared the Lambda Resources like below
Resources:
LambdaFunction:
Properties:
Environment:
Variables:
DOMAIN: {Ref: Domain}
.......
Upvotes: 2
Reputation: 2817
According AWS documentation, AWS::Lambda::Function
resource has Environment
property that you may use for specifying environment variables. So your resource in a cloud formation file would look like:
{
"Type" : "AWS::Lambda::Function",
"Properties" : {
"FunctionName" : "Your function name",
"Environment" : {
"Variables": {
"variable1": "value1",
"variable2": "value2"
}
},
...
}
}
Upvotes: 4