Lambda boto3 cross-acount session
For the IAM part, there is the following AWS resource : https://repost.aws/knowledge-center/lambda-function-assume-iam-role
import boto3
def lambda_handler(event, context):
sts_client = boto3.client('sts')
sts_response = sts_client.assume_role(
RoleArn="arn:aws:iam::ACCOUNT_B:role/AssumedRole",
RoleSessionName="AssumedRoleSession"
)
session = boto3.Session(
aws_access_key_id=sts_response['Credentials']['AccessKeyId'],
aws_secret_access_key=sts_response['Credentials']['SecretAccessKey'],
aws_session_token=sts_response['Credentials']['SessionToken'],
region_name="eu-central-1"
)
ssm_client = session.client('ssm')
# ...
autoscaling_client = session.client('autoscaling')
# ...
return