There are use cases where the need to get the AWS Account ID of the Lambda Function during runtime is required. I thought it was easy as getting the AWS Region but it was not. Luckily there is a way to get it, use the step-by-step instructions below.
To get the AWS Account ID where the Lambda Function is running use the code below.
def lambda_handler(event, context):
aws_account_id = context.invoked_function_arn.split(":")
How does the code work?
Continue reading How to get the AWS Account ID in Lambda Python
context object that is being passed to the lambda_handler function provides different methods and properties about the lambda function, like invocation and execution environment.
To get the AWS Region where your Lambda Function is running you will need to import the
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
Editing configuration values inside the code is a high risk for error since there is a high chance that not only the values that you are changing you will change, you might even delete a letter or edit a line. In order to avoid this risk, you want your code to be able to accept configuration values at run time. This is extremely useful when developing codes on different environments like development, testing and production.
With AWS Lambda you can reuse your code on different environments using the Environment Variables.
Below is the way to use Environment Variables on AWS Lambda Console using Python 3.6.
Note: This is the same way to use Environment Variables on Python 2.7 and Python 3.7.
Environment Variables Setup
The Environment Variables section can be found under the Function Code section.
Environment Variables are Key/Value Pairs. The Key is what you will use on your Lambda Code, to access its Value. Continue reading AWS Lambda Console: Accessing Environment Variables via Python