Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
52 views
in Information Technology by (176k points)
How do you assume a role in AWS using the AWS SDK?

Please log in or register to answer this question.

1 Answer

+1 vote
by (176k points)

To assume a role using the AWS SDK, you need to use the STS (Security Token Service) client.

Example Code (Python):

import boto3

# Create an STS client
sts_client = boto3.client('sts')

# Assume the role
response = sts_client.assume_role(
    RoleArn='arn:aws:iam::123456789012:role/example-role',
    RoleSessionName='example-session'
)

# Extract temporary credentials
credentials = response['Credentials']
access_key = credentials['AccessKeyId']
secret_key = credentials['SecretAccessKey']
session_token = credentials['SessionToken']

# Use the temporary credentials to create a new session
assumed_role_session = boto3.Session(
    aws_access_key_id=access_key,
    aws_secret_access_key=secret_key,
    aws_session_token=session_token
)

# Use the assumed role session to interact with AWS services
s3_client = assumed_role_session.client('s3')
response = s3_client.list_buckets()
print(response['Buckets'])

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...