You are currently viewing Lambda Function Environment Variables

Lambda Function Environment Variables

Lambda Function Environment Variables

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we had added S3 trigger to our lambda function.

https://cloudaffaire.com/lambda-function-trigger/

In this blog post, we are going to create modify our existing lambda function and add environment variables. The function will read the bucket name and region information from the environment variable and create an S3 bucket accordingly. We will also use a custom function handler name.

Lambda Function Environment Variables:

Environment variables for Lambda functions enable you to dynamically pass settings to your function code and libraries, without making changes to your code. Environment variables are key-value pairs that you create and modify as part of your function configuration, using either the AWS Lambda Console, the AWS Lambda CLI or the AWS Lambda SDK. AWS Lambda then makes these key-value pairs available to your Lambda function code using standard APIs supported by the language, like os.environ for Python functions.

Next, we are going to add an environment variable in our function.

Step 1: Login to AWS console and navigate to ‘Lambda’.

Lambda Function Environment Variables

Step 2: Click on the function name.

Lambda Function Environment Variables

Step 3: In the ‘Environment variables’ section, define the bucket name that needs to be created.

Lambda Function Environment Variables

Step 4: Modify the code of the function to read environment variables and create S3 bucket.

Note: We are using a custom handler ‘create_s3_bucket’ instead of default ‘lambda_handler’. Hence we have to define our custom handler as well. Modify the code indentation before pasting.

Step 6: Modify ‘Handler’ as per your handler name and click ‘Save’.

Lambda Function Environment Variables

Step 7: Execute the lambda function manually using AWS CLI.

Check the CloudWatch Logs

Lambda Function Environment Variables

Check S3 bucket

Lambda Function Environment Variables

Our lambda function successfully created the bucket with a bucket name supplied from the environment variable.

Hope you have enjoyed this article. In the next blog post, we will discuss layers in Lambda functions.

To get more details on Lambda, please refer below AWS documentation

https://docs.aws.amazon.com/lambda/index.html

 

Leave a Reply