[JAVA] How to apply IAM ROLE to EC2 and get temporary AccessKey and SecretAccessKey

Access keys and secret access keys can be created by an IAM user with "Generate Access Keys". However, this method requires the access key to be replaced in a certain period of time, which is quite troublesome. So I assigned IAM ROLE to EC2 and the access key was planned to be Osaraba, but when IAM ROLE was applied, I could not get the temporary AccessKey, SecretAccessKey that I wrote before. I used to write like this

AWSCredentials credentials = new BasicAWSCredentials("access key", "シークレットaccess key");
AWSSecurityTokenServiceClient sts = new AWSSecurityTokenServiceClient(credentials);
GetSessionTokenRequest req = new GetSessionTokenRequest();
GetSessionTokenResult res = sts.getSessionToken(req);
Credentials tmpCredentials = res.getCredentials();
String accessKeyId = tmpCredentials.getAccessKeyId();
String secretAccessKeyId = tmpCredentials.getSecretAccessKey();

I checked it briefly, but the assume Role is good. Originally it seems to be messing with resources between different accounts, I think this is fine.

AWSSecurityTokenService sts = new AWSSecurityTokenServiceClient();
AssumeRoleResult assumeRoleResult = sts.assumeRole(new AssumeRoleRequest()
		.withRoleArn(ROLE_ARN)
		.withRoleSessionName(ROLE_SESSION_NAME));

Credentials credentials = assumeRoleResult.getCredentials();
System.out.println("AccessKeyId=" + credentials.getAccessKeyId() + " SecretAccessKey=" + credentials.getSecretAccessKey()+ " SessionToken=" + credentials.getSessionToken());

What should I specify for ROLE_ARN and ROLE_SESSION_NAME? ROLE_ARN is the ARN of the role

WS000001.JPG

ROLE_SESSION_NAME is a nice string ex. Like "role_session"

For the last roll you want to use

        {
            "Effect": "Allow",
            "Action": [
                "sts:AssumeRole"
            ],
            "Resource": [
                "arn:aws:iam::xxxxxxxxxxxx:role/full_ec2_lambda"
            ]
        }

It is OK if you add the policy of

You can now get temporary credentials.

Recommended Posts

How to apply IAM ROLE to EC2 and get temporary AccessKey and SecretAccessKey
[Java] How to get and output standard input
How to get and study java SE8 Gold
[Rails] How to get success and error messages
[Ruby] How to get the tens place and the ones place
[Kotlin] How to get IP address and user agent
How to use and apply Java's JFrame / Canvas class
How to batch run JUnit and get coverage as well
[Rails] How to get the URL of the transition source and redirect
[Swift5] How to get an array and the complement of arrays
How to get and add data from Firebase Firestore in Ruby
[Java] How to convert from String to Path type and get the path
How to use StringBurrer and Arrays.toString.
How to use Java HttpClient (Get)
How to get started with slim
How to use EventBus3 and ThreadMode
How to get parameters in Spark
How to call classes and methods
How to use equality and equality (how to use equals)
How to connect Heroku and Sequel
How to convert LocalDate and Timestamp
How to get along with Rails
[Java] How to get the current date and time and specify the display format
How to create your own annotation in Java and get the value