title: AWS IAM: AssumeRole vs GetSessionToken
body: <p>I'm trying to generate a temporary credentials access key and secret key. I've used <code>AssumeRole</code>. The description says it generates an access key and secret key. But <code>GetSessionTokenResult</code> can also generate an access key and secret key. Then what's the use of assumeRole?</p>
  <pre class="lang-java prettyprint-override"><code>AWSSecurityTokenService awsSecurityTokenService = 
      AWSSecurityTokenServiceClientBuilder 
      .standard().withCredentials(new ProfileCredentialsProvider())
      .withRegion(region).build();
  AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest()
      .withRoleArn(
                   &quot;arn:aws:iam::account-id:role/p-27c229ade194_ec2&quot;)
      .withRoleSessionName(&quot;RedshiftSession&quot;);
  AssumeRoleResult assumeRoleResult = awsSecurityTokenService
      .assumeRole(assumeRoleRequest);
  GetSessionTokenRequest getSessionTokenRequest = new GetSessionTokenRequest();
  getSessionTokenRequest.setDurationSeconds(1200);
  GetSessionTokenResult getSessionTokenResult = awsSecurityTokenService
      .getSessionToken(getSessionTokenRequest);
  Credentials sessionCredentials = getSessionTokenResult.getCredentials();
  
  final String adminAccessKeyId = sessionCredentials.getAccessKeyId();
  final String adminAccessSecretKey = sessionCredentials
      .getSecretAccessKey();
  </code></pre>
  <p>Earlier using assumeRole it showed error =&gt; <code>aws:iam::user/admin is not sts:assumeRole on resource role aws:iam::role/role_id</code>.
  By adding <code>aws:iam::user/admin</code> in trusted relationship of <code>role_id</code> it worked.</p>
  <p>If I will comment out <code>AccessRole</code> and its other called class. I can generate an access key and secret key. What's the purpose of using <code>AssumeRole</code>?</p>