Reputation: 16532
AWS EC2 Meta-Data provides the necessary information about itself from EC2 (duh!) - is there anything equivalent for lambda.
I understand the multi-tenancy and short-lived behavior of the lambda function unlike EC2 but essential info like Account ID, VPC AZ, Region would help do a lot of AWS automation.
Upvotes: 6
Views: 7876
Reputation: 41
In the context object of the Lambda, you can parse out some of the information you're looking for in the invoked_function_arn.
For example in Python: context.split(':')[3]
will give you the region and context.split(':')[4]
will give you the aws account id and context.split(':')[6]
will give you the function name (which is also available in context.function_name
The invoked_function_arn looks like the following:
arn:aws:lambda:us-east-1:741063561123:function:lambda_context
Once you have that information you can use aws libraries (e.g. boto3 for Python) to get the rest of the information about the lambda (such as VPC, SG)
Upvotes: 1
Reputation: 13495
You can set an environment variable when you deploy the lambda: https://docs.aws.amazon.com/lambda/latest/dg/env_variables.html
There are already AWS_REGION
and AWS_DEFAULT_REGION
variables there: https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html#lambda-environment-variables.
event
can have fields like requestContext.accountId
: https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html?shortFooter=true#eventsources-api-gateway-request
Found this out when browsing Terraform's lambda_function configuration.
Upvotes: 3
Reputation: 2761
No, unfortunately not. The context object is the closest thing, but the information it offers is very limited. http://docs.aws.amazon.com/lambda/latest/dg/programming-model-v2.html
If you're invoking the lambda functions yourself or in a programmatic way, you could pass the account ID and region in the payload.
Upvotes: 3