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(":") print(aws_account_id)
How does the code work?
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.