APURV
  • Home
  • Journey
  • Projects
  • Blogs
  • Interview
  • Exams
Resume
APURV

Building scalable, secure, and production-ready cloud infrastructure. Automation first.

NAVIGATION

HomeExperienceProjectsCertificationsSkills

TECH STACK

AWSGCPK8sCI/CDLinuxDocker

CONNECT

LinkedInGitHubEmailResume

© 2026 Apurv Gujjar. All rights reserved.
Apurv Gujjar
Apurv GujjarDevOps & Cloud Engineer
|Interview Documentation
Portfolio
Handbooks
🎯Linux🐙Git & GitHub🤖GitHub Actions🌐Networking☁AWS🛠Terraform🐳Docker☸Kubernetes📊Monitoring🛡DevSecOps💰Cost Optimization🚨Incident Scenarios👤HR & Behavioral☁GCP🐍Python
Interview DocumentationPythonHow do you assume an IAM role programmatically inside a Boto3 script?
Back to all Python questions
Q5

How do you assume an IAM role programmatically inside a Boto3 script?

💬Answer

Call AWS Security Token Service (STS) assume_role to fetch temporary credentials, then initialize a new Boto3 session:

import boto3

sts = boto3.client("sts")

assumed_role = sts.assume_role(
    RoleArn="arn:aws:iam::123456789012:role/TargetDevOpsRole",
    RoleSessionName="AssumedRoleSession"
)

# Extract credentials
creds = assumed_role["Credentials"]

# Create session with temporary credentials
session = boto3.Session(
    aws_access_key_id=creds["AccessKeyId"],
    aws_secret_access_key=creds["SecretAccessKey"],
    aws_session_token=creds["SessionToken"]
)

# Use resources within the context of the assumed role
s3 = session.resource("s3")

Related Python Questions

View All PythonQuestions →
Q1

What is the difference between a List and a Tuple?

Q2

What is the difference between the `==` and `is` operators?

Q3

What are the primary Python data types?

Q4

What is a dictionary and why is it heavily used in DevOps?

Apurv Gujjar - DevOps & Cloud Engineer
Created by

Apurv Gujjar

DevOps & Cloud Engineer

Specialized in:DevOpsAWSGCPKubernetesTerraformDocker
View Portfolio