Skip to content

How do I use the AWS CLI to make AssumeRole calls and store temporary user credentials?

4 minute read
-1

I want to use the AWS Command Line Interface (AWS CLI) to get credentials from AssumeRoleWithSAML, AssumeRole, and AssumeRoleWithWebIdentity.

Resolution

Note: If you receive errors when you run AWS CLI commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

To get credentials from AssumeRoleWithSAML, AssumeRole, and AssumeRoleWithWebIdentity, call the API and save the output to a text file. Then, use the output to call an API command with the AWS CLI.

Note: The AssumeRoleWithSAML, AssumeRole, and AssumeRoleWithWebIdentity commands require the parameters role-arn and role-session-name.

Get credentials with SAML authentication

Requirement: You must have a valid SAML 2.0 response from your identify provider (IdP) and an AWS Identity and Access Management (IAM) role that trusts the IdP.

Complete the following steps:

  1. Get the SAML response.

  2. Locate the logs, and then open the SAML log file.

  3. Copy the entire SAML response.

  4. Paste the SAML response into file named samlresponse.log that's in the local directory.

  5. Run the following assume-role-with-saml command to call the AWS Security Token Service (AWS STS) token:

    aws sts assume-role-with-saml --role-arn arn:aws:iam::444455556666:role/IAM_ROLE --principal-arn arn:aws:iam::444455556666:saml-provider/SAML_PROVIDER --saml-assertion file://samlresponse.log
     
    awk -F:  '
                    BEGIN { RS = "[,{}]" ; print "[PROFILENAME]"}
                    /:/{ gsub(/"/, "", $2) }
                    /AccessKeyId/{ print "aws_access_key_id = " $2 }
                    /SecretAccessKey/{ print "aws_secret_access_key = " $2 }
                    /SessionToken/{ print "aws_session_token = " $2 }
    ' >> ~/.aws/credentials
    

    Note: Replace the Principal ARN with your SAML provider Amazon Resource Name (ARN). This example uses awk, which is compatible with Linux based distributions. When you run the assume-role-with-saml command, this saves the credentials in a profile inside the ~/.aws/credentials file.

  6. (Optional) Run the following command to back up the credentials:

    cp -a ~/.aws/credentials ~/.aws/credentials.bak.
  7. Run the following describe-instances command to call the user credentials:

    aws ec2 describe-instances --profile PROFILENAME

    Note: Replace PROFILENAME with your profile name.
    Example output that isn't saved to a file:

    {            
        "SubjectType": "persistent",
        "AssumedRoleUser": {
           "AssumedRoleId": "111122223333:example@corp.example.com",
           "Arn": "arn:aws:sts::44445555666:assumed-role/ROLE_ID/example@corp.example.com"
        },    
        "Audience": "https://signin.aws.amazon.com/saml",
        "NameQualifier": "RANDOM_GENERATED_STRING",
        "Credentials": {
           "SecretAccessKey": "SECRET_ACCESS_KEY",
           "SessionToken": "TOKEN_KEY",
           "Expiration": "2015-05-11T20:00:49Z",
           "AccessKeyId": "ACCESS_KEY_ID"
    },
    "Subject": "CORP\\\\EXAMPLE",
    "Issuer": "http://SERVER_NAME.corp.example.com/adfs/services/trust"
    }
    

    Example output saved to the credentials file:

    aws_access_key_id =  ACCESS_KEY_ID
    aws_session_token =  SESSION_TOKEN
    aws_secret_access_key =  SECRET_ACCESS_KEY
    [PROFILENAME]
    

Get credentials with AssumeRole

Important: Your IAM credentials must trust the IAM role that you assume.

Complete the following steps:

  1. Run the following get-caller-identity command to verify a response:

    aws sts get-caller-identity

    Note: If you don't receive a response, check whether the .aws/credentials file stores a valid IAM access or secret key.

  2. Run the following assume-role command:

    aws sts assume-role --role-arn arn:aws:iam::123456789012:role/ExampleRole --role-session-name ExampleSession

    Example response:

    {
        "AssumedRoleUser": {
            "AssumedRoleId": "AROAZRG5BQ2L7OB87N3RE:ExampleSession",
            "Arn": "arn:aws:sts::123456789012:assumed-role/RoleA/ExampleSession"
        },
        "Credentials": {
            "SecretAccessKey": "JCNFKY7XCUwHWTKcQhmmFokpjLetCmNLZ7pg9SJe",
            "SessionToken": "FwoGZXIvYXdzEL7//////////wEaDNoBLBJUNYWKaHXZXCKvARBGJ4CqOs+p2JR2a7Euni0d0XuSs31ZA/1QqpX6Spfuz2WAvHCyqwbE3+oxyvyqYlO8dTJwp56YCFCJ6K4Prt9pMeZU9R5NGBJHvRbXXKfxp+jktLc/ItrAfn4GMXWpoyJKZrY7hzv3MASomlIcwSD/RqWIBS1vAoo1UAxwjy29jai0OAPQ51LAnuFKeabgmllyP5Y2gu488P19D7ikfgQtmBtH5I/Q8+5IEn4qMwYo1bq/8wUyLe1b3+mQwhq+zEz4TSyHD8HBXd9W3KYcB53MIotwiJNS+m0P5ZlZnpcJm3JwtA==",
            "Expiration": "2020-03-16T21:11:01Z",
            "AccessKeyId": "ASIAZRG8BQ4K2EBXGR42"
        }
    }
  3. From the response, export AccessKeyId, SecretAccessKey, and SessionToken to create environmental variables:

    export AWS_ACCESS_KEY_ID=ASIAZRG8BQ4K2EBXGR42
    export AWS_SECRET_ACCESS_KEY=JCNFKY7XCUwHWTKcQhmmFokpjLetCmNLZ7pg9SJe
    export AWS_SESSION_TOKEN=FwoGZXIvYXdzEL7//////////wEaDNoBLBJUNYWKaHXZXCKvARBGJ4CqOs+p2JR2a7Euni0d0XuSs31ZA/1QqpX6Spfuz2WAvHCyqwbE3+oxyvyqYlO8dTJwp56YCFCJ6K4Prt9pMeZU9R5NGBJHvRbXXKfxp+jktLc/ItrAfn4GMXWpoyJKZrY7hzv3MASomlIcwSD/RqWIBS1vAoo1UAxwjy29jai0OAPQ51LAnuFKeabgmllyP5Y2gu488P19D7ikfgQtmBtH5I/Q8+5IEn4qMwYo1bq/8wUyLe1b3+mQwhq+zEz4TSyHD8HBXd9W3KYcB53MIotwiJNS+m0P5ZlZnpcJm3JwtA==
  4. Run the following get-caller-identity to verify the identity:

    aws sts get-caller-identity

    The output shows the identity credentials from the assume-role call.

Get credentials with AssumeRoleWithWebIdentity

Requirement: You must have a valid OAuth 2.0 access token, an OpenID Connect token, and an IAM role that trusts the IdP.

  1. Run the following assume-role command:

    aws sts assume-role-with-web-identity --role-arn arn:aws:iam::123456789012:role/FederatedWebIdentityRole --role-session-name ExampleSession --web-identity-token
    Atza%7CIQEBLjAsAhRFiXuWpUXuRvQ9PZL3GMFcYevydwIUFAHZwXZXXXXXXXXJnrulxKDHwy87oGKPznh0D6bEQZTSCzyoCtL_8S07pLpr0zMbn6w1lfVZKNTBdDansFBmtGnIsIapjI6xKR02Yc_2bQ8LZbUXSGm6Ry6_BG7PrtLZtj_dfCTj92xNGed-CrKqjG7nPBjNIL016GGvuS5gSvPRUxWES3VYfm1wl7WTI7jn-Pcb6M-buCgHhFOzTQxod27L9CqnOLio7N3gZAGpsp6n1-AJBOCJckcyXe2c6uD0srOJeZlKUm2eTDVMf8IehDVI0r1QOnTV6KzzAI3OY87Vd_cVMQ
    

    Example response:

    {
        "SubjectFromWebIdentityToken": "amzn1.account.AF6RHO7KZU5XRVQJGXK6HB56KR2A"
        "Audience": "client.5498841531868486423.1548@apps.example.com",
        "AssumedRoleUser": {
            "Arn": "arn:aws:sts::123456789012:assumed-role/FederatedWebIdentityRole/ExampleSession",
            "AssumedRoleId": "AROACLKWSDQRAOEXAMPLE:ExampleSession"
        }
        "Credentials": {
            "AccessKeyId": "AKIAIOSFODNN7EXAMPLE",
            "SecretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY",
            "SessionToken": "AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE/IvU1dYUg2RVAJBanLiHb4IgRmpRV3zrkuWJOgQs8IZZaIv2BXIa2R4OlgkBN9bkUDNCJiBeb/AXlzBBko7b15fjrBs2+cTQtpZ3CYWFXG8C5zqx37wnOE49mRl/+OtkIKGO7fAE",
            "Expiration": "2020-05-19T18:06:10+00:00"
        },
        "Provider": "www.amazon.com"
    
  2. Save AccessKeyId, SecretAccessKey, and SessionToken in the .aws/credentials file:

    [ExampleRoleProfile]
    aws_access_key_id=AKIAIOSFODNN7EXAMPLE
    aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY
    aws_session_token=AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE/IvU1dYUg2RVAJBanLiHb4IgRmpRV3zrkuWJOgQs8IZZaIv2BXIa2R4OlgkBN9bkUDNCJiBeb/AXlzBBko7b15fjrBs2+cTQtpZ3CYWFXG8C5zqx37wnOE49mRl/+OtkIKGO7fAE
    
  3. Run the get-caller-identity command:

    aws sts get-caller-identity --profile ExampleRoleProfile

    Example output of the identity credentials from the assume-role call:

    {
    "UserId": "AROACLKWSDQRAOEXAMPLE:ExampleSession",
    "Account": "123456789012",
    "Arn": "arn:aws:sts::123456789012:assumed-role/FederatedWebIdentityRole/ExampleSession"
    }
    

Related information

How do I grant my Active Directory users access to the API or AWS CLI with AD FS?

Configure federated identity with the AWS Tools for PowerShell

Configuration and credential file settings in the AWS CLI