Table of Contents
On this lab we will use the AWS CLI to assume a role and open the AWS Console. The instructor will provide you with credentials for this and the following lab.
- Log into AWS. Change your password when prompted.
AWS Console URL: https://ldcp-iis-my-ctrl-app-lrn.signin.aws.amazon.com/console
Passwords must have:
- 11 characters
- Mixed case letters
- At least 1 number
- At least 1 special character
- Setup MFA for your new IAM user: select
Services>IAM, selectUsers, select your user name, selectSecurity Credentials>Manage MFA Device. SelectA virtual MFA deviceclickNext Step. Enter in two consecutive MFA codes (1st inAuthentication Code 1and second inAuthentication Code 2) and clickActivate Virtual MFA.
Generate API access keys and configure them into your AWS CLI.
- Under your user's
Security CredentialsclickCreate Access Keys>Download Credentials(save the file) >Close.
- This will download a credentials.csv file that contains your User Name, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY
- On your command line, use
aws configureto configure the credentials you just downloaded.
E.g.,
vagrant ssh
$ aws configure --profile dso
AWS Access Key ID [None]: <YOUR_AWS_ACCESS_KEY_ID>
AWS Secret Access Key [None]: <YOUR_AWS_SECRET_ACCESS_KEY>
Default region name [None]: us-west-2
Default output format [None]: json
Use AWS STS to assume the role of DeploymentAdmin (DA) into the target account.
First set your AWS_USERNAME environment variable.
echo "export AWS_USERNAME=<YOUR_USERNAME>" >> ~/.bash_profile
source ~/.bash_profile
- Use STS to assume the control account role:
aws sts assume-role \
--role-arn arn:aws:iam::100352119871:role/dso/ctrl/my-app/CTL-my-app-DeploymentAdmin \
--role-session-name $AWS_USERNAME-$$ --profile dso \
--serial-number arn:aws:iam::100352119871:mfa/$AWS_USERNAME \
--token-code <MFA_TOKEN_CODE>
- This will return JSON formatted text which contains your temporary credentials (e.g. AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN) to be used in the next step.
- Export control STS credentials:
export AWS_ACCESS_KEY_ID=<TEMPORARY_AWS_ACCESS_KEY_ID>
export AWS_SECRET_ACCESS_KEY=<TEMPORARY_AWS_SECRET_ACCESS_KEY>
export AWS_SESSION_TOKEN=<TEMPORARY_AWS_SESSION_TOKEN>
- Use AWS STS to assume the target account role (DeploymentAdmin).
aws sts assume-role \
--role-arn arn:aws:iam::717986480831:role/human/dso/TGT-dso-DeploymentAdmin \
--role-session-name $AWS_USERNAME-$$
- Export target STS credentials.
export AWS_ACCESS_KEY_ID=A<TEMPORARY_AWS_ACCESS_KEY_ID>
export AWS_SECRET_ACCESS_KEY=<TEMPORARY_AWS_SECRET_ACCESS_KEY>
export AWS_SESSION_TOKEN=<TEMPORARY_AWS_SESSION_TOKEN>
- Open AWS console with temporary credentials.
You can use the following ruby script (console.rb) to do this:
#!/usr/bin/env ruby
require 'json'
require 'cgi'
require 'net/http'
issuer_url = 'gui.rb'
console_url = 'https://console.aws.amazon.com/'
signin_url = 'https://signin.aws.amazon.com/federation'
session_json = { sessionId: ENV['AWS_ACCESS_KEY_ID'],
sessionKey: ENV['AWS_SECRET_ACCESS_KEY'],
sessionToken: ENV['AWS_SESSION_TOKEN'] }.to_json
get_signin_token_url = signin_url + '?Action=getSigninToken' + '&SessionType=json&Session=' + CGI.escape(session_json)
returned_content = Net::HTTP.get(URI.parse(get_signin_token_url))
signin_token = JSON.parse(returned_content)['SigninToken']
signin_token_param = '&SigninToken=' + CGI.escape(signin_token)
issuer_param = '&Issuer=' + CGI.escape(issuer_url)
destination_param = '&Destination=' + CGI.escape(console_url)
login_url = signin_url + '?Action=login' + signin_token_param + issuer_param + destination_param
puts "\n\nCopy and paste this URL into your browser:\n#{login_url}"