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 AWS Lambda remaining time using Python

To get the remaining time of a running AWS Lambda Function using Python you should check the context object and its get_remaining_time_in_millis function.

Continue reading How to get AWS Lambda remaining time using Python

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 list files in an S3 bucket folder using boto3 and Python

If you want to list the files/objects inside a specific folder within an S3 bucket then you will need to use the list_objects_v2 method with the Prefix parameter in boto3.

Below are 3 examples codes on how to list the objects in an S3 bucket folder.

What the code does is that it gets all the files/objects inside the S3 bucket named radishlogic-bucket within the folder named s3_folder/ and adds their keys inside a Python list (s3_object_key_list). It then prints each of the object keys in the list and also prints the number of files in the folder.

Continue reading How to list files in an S3 bucket folder using boto3 and Python

How to list all objects in an S3 Bucket using boto3 and Python

If you need to list all files/objects inside an AWS S3 Bucket then you will need to use the list_objects_v2 method in boto3.

Below are 3 example codes of how to list all files in a target S3 Bucket.

You can use any of the 3 options since it does the same thing.

It will get all of the files inside the S3 Bucket radishlogic-bucket using Python boto3, put it inside a Python list, then print each object key. It will print the files inside folder recursively, regardless if they are inside a folder or not.

At the end, it will also print the number of items inside the S3 Bucket.

Continue reading How to list all objects in an S3 Bucket using boto3 and Python

How to delete a file in AWS S3 using boto3 and Python

To delete a file inside an AWS S3 Bucket using Python then you will need to use the delete_object function of boto3.

Below are 3 examples to delete an S3 file.

You can use any of the 3 options since it does the same thing. It will delete the file in S3 with the key of s3_folder/file.txt inside the S3 bucket named radishlogic-bucket using Python boto3.

Continue reading How to delete a file in AWS S3 using boto3 and Python

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

If you want to get a file from an S3 Bucket and then put it in a Python string, try the examples below.

boto3, the AWS SDK for Python, offers two distinct methods for accessing files or objects in Amazon S3: client method and the resource method.

Option 1 uses the boto3.client('s3') method, while options 2 and 3 use the boto3.resource('s3') method.

All 3 options do the exact same thing so get the one that you feel comfortable with or the one that will fit your use case.


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