How to retrieve temporary credentials using rest api or by using AssumeRole in AWS SDK

0

hi , ive been trying to retrieve temporary credentials using role arn but getting an error of EC2 Metadata not found in AWS SDK . Thanks

질문됨 2년 전390회 조회
2개 답변
0

Hello

You cant get the credentials for your current role, so you need to call sts:AssumeRole API.

Here is an example with python boto3.

You can now use the s3_client to call S3 using the role you just assumed.

import boto3
sts_client = boto3.client('sts')

role_arn = "MyRoleArn"

role_credentials = sts_client.assume_role(
  RoleArn=role_arn,
  RoleSessionName='MySessionName',
)['Credentials']

print(f"{role_credentials}")

s3_client = boto3.client('s3',
    aws_access_key_id=role_credentials['AccessKeyId'],
    aws_secret_access_key=role_credentials['SecretAccessKey'],
    aws_session_token=role_credentials['SessionToken']
)

NOTE: Be super careful with logging because the role_credentials variable continues your actual credentials.

Hope this helps!

//Carl

profile picture
답변함 2년 전
0

Your question seems to indicate that you are running on an EC2 instance. If you are running code on EC2 instance, the recommended way to get credentials is to use roles for Amazon EC2..

Here is the link to documentation on using temporary credentials, including how to use them with SDKs: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html

AWS
Ashu
답변함 3달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠