Use aws-assume-role-lib for transparent session management
mainThe primary benefit of aws-assume-role-lib is that it handles the complexity of caching and refreshing assumed role credentials automatically.
When using aws_assume_role_lib.assume_role(), the returned session is valid for the life of the Lambda function instance because the library uses built-in boto3/botocore mechanisms to refresh credentials before they expire. This allows you to move the role assumption logic to the initialization phase (outside the handler) without worrying about credential expiration during long-lived function instances.
Key Features:
- Automatic Caching/Refreshing: No manual logic needed to check if credentials have expired.
- Automatic Session Naming: If
RoleSessionNameis absent, it is automatically generated. - SourceIdentity Integration: If you use
SourceIdentity, the library automatically sets theRoleSessionNameto match it.