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
Published 5 years ago
Updated a year ago

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."

egghead
egghead
~ 44 seconds ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today