Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current execution role in a lambda?

I'm having issues with a lambda that does not seem have the permissions to perform an action and want to get some troubleshooting information.

I can do this to get the current user:

print("Current user: " + boto3.resource('iam').CurrentUser().arn)

Is there a way to get the execution role at runtime? Better yet, is there a way to get the policies that are attached to this role dynamically?

They shouldn't change from when I created the lambda, but I want to verify to be sure.

like image 663
Nate Reed Avatar asked Jun 23 '26 19:06

Nate Reed


1 Answers

Do not know, how much relevance this will bring to the OP.

But we can get lambda function configuration at runtime.

lambda_client = boto3.client('lambda')
role_response = (lambda_client.get_function_configuration(
    FunctionName = os.environ['AWS_LAMBDA_FUNCTION_NAME'])
)
print(role_response)
role_arn = role_response['Role']

role_response will have role arn.

role_response =>

{'ResponseMetadata': {'RequestId': '', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'GMT', 'content-type': 'application/json', 'content-length': '877', 'connection': 'keep-alive', 'x-amzn-requestid': ''}, 'RetryAttempts': 0}, 'FunctionName': 'lambda_name', 'FunctionArn': 'arn:aws:lambda:<region>:<account_id>:function:lambda_arn', 'Runtime': 'python3.8', 'Role': 'arn:aws:iam::<account_id>:role/<role_name>', 'Handler': 'handlers.handle', 'CodeSize': 30772, 'Description': '', 'Timeout': 30, 'MemorySize': 128, 'LastModified': '', 'CodeSha256': '', 'Version': '$LATEST', 'VpcConfig': {'SubnetIds': [], 'SecurityGroupIds': [], 'VpcId': ''}, 'TracingConfig': {'Mode': 'PassThrough'}, 'RevisionId': '', 'State': 'Active', 'LastUpdateStatus': 'Successful'}
like image 106
Ayush Avatar answered Jun 25 '26 23:06

Ayush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!