1. 10
    Pass environment variables to a lambda function deployed with AWS CDK
    1m 16s
⚠️ This lesson is retired and might contain outdated information.

Pass environment variables to a lambda function deployed with AWS CDK

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet

Environment variables are really useful in programming - they allow us to avoid hardcoding secrets (such as API keys) in our code with added bonus of using different values depending on the environment (hence the name!).

In this quick lesson we're going to learn how to pass environment variables to an AWS Lambda function deployed with Cloud Development Kit (in a single line of code!)

Instructor: [0:00] With CDK, we can also pass in environment variables to our lambda functions. They can behave differently depending on those values.

[0:07] In order to add an environment variable to this hello lambda function, we have to add an additional prop. This prop is going to be environment. We can see that this prop takes a key value pair. In other words, an object.

[0:18] By default, there are no environment variables provided to a lambda function, so I'm going to do environment. This is going to be isProduction. I'm going to set it to a string, "absolutely not" because we are just experimenting over here.

[0:30] Right now, if I go to our lambda function, I can go ahead and console log isProduction. I'm going to console log process.env isProduction. Once we save that, go back to our stock and now it's ready to be deployed.

[0:46] Now we can open up our terminal and then run CDK deploy, in order to deploy this change to AWS. After a successful deployment, we can see that the line that we've added over here is also present in the AWS console.

[0:57] If you scroll down a bit, we are going to be able to see this environment variable. The key is "is production" and the value is "absolutely not." If we call this function using a test event, if I click over here, after expanding that, we are going to be able to see over here that our environment variable has been logged out. So "is production," "absolutely not."