Monday 20 May 2019

Create STS connection using Boto3

This is not production ready code. It is just as a sample.

import boto3
from boto3 import Session

def connect_sts(region, role_arn):
  sts_client = boto3.client('sts')
 
  session = Session(region_name=region)

  assumedRoleObject = sts_client.assume_role(
    RoleArn=role_arn,
    RoleSessionName="AssumeRoleSession1"
  )

  credentials = assumedRoleObject['Credentials']
  connector = {
    'cloudformation' : session.resource('cloudformation', aws_access_key_id = credentials['AccessKeyId'], aws_secret_access_key = credentials['SecretAccessKey'], aws_session_token = credentials['SessionToken']),
    'iam' : session.resource('iam', aws_access_key_id = credentials['AccessKeyId'], aws_secret_access_key = credentials['SecretAccessKey'], aws_session_token = credentials['SessionToken']),
    'ec2' : session.resource('ec2', aws_access_key_id = credentials['AccessKeyId'], aws_secret_access_key = credentials['SecretAccessKey'], aws_session_token = credentials['SessionToken']),
    's3' : session.resource('s3', aws_access_key_id = credentials['AccessKeyId'], aws_secret_access_key = credentials['SecretAccessKey'], aws_session_token = credentials['SessionToken'])

  }
  #print connector
  return connector

ec22 = connect_sts('eu-west-1','arn:aws:iam::837274771294:role/Administrator')
for vpc in ec22['ec2'].vpcs.all():
  print vpc

3 comments:

  1. The right advice can be crucial to growing your startup, and one of the best sources of advice can be derived by engaging a startup consulting firm.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Your Blog is really informative. I really like it keep share these kind of blogs. MyAssignmentHelp.co.uk has proved to be one of the consummate providers in the assiduity of assignment writing service. Scholars can ask for help we give chccom005 assignment answers online with assignment help covering nearly all being areas of study ranging from operation, trades, mathematics, drugs, to nursing as well. Our services rest on two introductory principles being quality and authenticity and as soon as you deliver your request; our assignment experts will guide you with a result within the handed deadlines.

    ReplyDelete