How to Get Lambda Runtime Region via Python

To get the AWS Region where your Lambda Function is running you will need to import the os module.

import os

Then from the os module, you need to get the value of AWS_REGION from the environ mapping variable. This will return the AWS Region where the Lambda Function is running.

runtime_region = os.environ['AWS_REGION']

Note: The way of getting the Runtime AWS Region of your Lambda Function is the same as when you get a Lambda Environment Variable.

Continue reading How to Get Lambda Runtime Region via Python

Access AWS Lambda Environment Variables using Node.js

If you want to get the values of Environment Variables in AWS Lambda using Node.js runtime follow the instructions below.

Node.js Code to Access Environment Variables

To access the Environment Variables of Lambda Functions using Node.js or javascript simply use the code below.

const environmentVariable = process.env.ENVIRONMENT_VARIABLE

Let’s say that my environment variable has a name of DB_USER, I will use the code below to get its value.

Continue reading Access AWS Lambda Environment Variables using Node.js

How to get the AWS Region where Node.js Lambda Function is running

To get the AWS Region where your Lambda Function is running we need to access the Environment Variable AWS_REGION.

To access the environment variable AWS_REGION using Node.js Lambda Function we can use process.env.AWS_REGION.

Continue reading How to get the AWS Region where Node.js Lambda Function is running

Access AWS Lambda Environment Variables using Ruby

AWS Lambda Environment Variables are a useful way to input configuration values to your AWS Lambda runtime. Especially, when there are configurations that are different in your Development environment compared to your Production environment. Like name of DynamoDB tables or MySQL databases.

Below we discuss how we can retrieve the values of Environment Variables in AWS Lambda using Ruby.


Ruby Code to Access Environment Variables

The code for accessing Environment Variables on AWS Lambda is just the same code for accessing environment variables in your local computer or server.

Here is the code to access environment variables using Ruby.

env_var = ENV['ENVIRONMENT_VARIABLE']

If we want to get the value of an environment variable with the key of DB_HOST then we will use the code below.

Continue reading Access AWS Lambda Environment Variables using Ruby

How to get the Region of a Running AWS Lambda Function using Ruby

If you need to get the Region of your running Lambda Function then you should look for the AWS_REGION in the Environment Variables.

Below is the code on how to access the AWS_REGION Environment Variable using Ruby.

aws_region = ENV['AWS_REGION']
Continue reading How to get the Region of a Running AWS Lambda Function using Ruby

How to get the remaining time of a running AWS Lambda Function using Node.js

To get the remaining time of a running Lambda Function using Node.js, we will use the context object’s getRemainingTimeInMillis() function. This returns the number of milliseconds that the Lambda Function can still run before it times out.

Below is a simple code that fetches the remaining time inside a Lambda Function. I have set the timeout to be only 3 seconds, that is why the output is 2,999 milliseconds or approximately 3 seconds.

Continue reading How to get the remaining time of a running AWS Lambda Function using Node.js

How to read a JSON file in S3 and store it in a Dictionary using boto3 and Python

If you want to get a JSON file from an S3 Bucket and load it into a Python Dictionary then you can use the example codes below.

There are 4 scenarios for the examples scripts below.

  1. Basic JSON file from S3 to Python Dictionary
  2. With Try/Except block
  3. With datetime, date, and time conversions
  4. Running the code in a Lambda Function

AWS boto3 provides 2 ways to access S3 files, the boto3.client('s3') and boto3.resource('s3'). For each of the example scenarios above, a code will be provided for the two methods.

Related: Writing a Dictionary to JSON file in S3 using boto3 and Python

Since both methods will function the same, you can choose whichever method you like.

Continue reading How to read a JSON file in S3 and store it in a Dictionary using boto3 and Python