Skip to content

Add utility for handling temporary boto sessions #1174

@thomasklinger1234

Description

@thomasklinger1234

Is your feature request related to a problem? Please describe.

When using the STS AssumeRole API to perform tasks in the context of another IAM role, there is usually a lot of boilerplate code to write:

credentials = sts.assume_role(RoleArn="...", "RoleSessionName="...")["Credentials"]
ec2 = boto3.Session(aws_session_token=credentials["SessionToken"], ...).client("ec2")

This feature request is about adding a new small helper utility for switching roles during Lambda invocations easier.

Describe the solution you'd like
A contextmanager for assuming a session for given context, something like

from aws_lambda_powertools.utilities import scoped_session

with scoped_session("ec2", role_arn="<role-arn>") as ec2:
  ec2.describe_regions()

Describe alternatives you've considered

Maybe there is also the possibility to do this using a decorator but I have no good API design for that.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Closed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions