Member-only story
Pass environment variables to lambda function deployed with AWS CDK
1 min readNov 24, 2020
There are cases that we need to deal with passing information inside lambda function using environment variables.
Here I am giving an example with CDK python which passes environment variable to a lambda and use those environment variables value inside the lambda.
base_lambda = lb.Function(self, 'base_lambda',
code = lb.Code.from_asset('lambda'),
handler = 'base_lambda.handler',
runtime = lb.Runtime.PYTHON_3_6,
function_name = 'base_lambda',
timeout = core.Duration.seconds(60),
environment = {
'cluster_env': 'staging',
'release': '1.2.1'
}
)
Then you can access this ‘cluster_env’ and ‘release’ environment variable inside the lambda function as follows,
import osdef handler(message, context):
cluster = os.environ['cluster_env']
release = os.environ['release']
print("cluster name is ": cluster)