Section 12: AWS CLI, SDK, IAM Roles & Policies Flashcards
- AWS EC2 Instance Metadata (IMDS) allows EC2 instances to “learn about themselves” without using an IAM Role for that purpose.
- URL is http://169.254.169.254/latest/meta-data (I suppose you reach this from an ec2 instance aws console?) m
- you can retrieve a lot of things from the metadata, IAM Role name included, but you cannot include IAM policy
- This url allows us to both access the metadata and the userdata (ec2 instance launch script)
Subsection: AWS EC2 Instance Metadata
IMDSv1 accesses http://169.254.169.254/latest/meta-data directly, but is less secure.
IMDSv2 is more secure and uses two steps:
* 1) get session token using headers & PUT: $ TOKEN=curl -X put "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"
* 2) use session token in IMDSv2 call using headers: $ curl http://169.254.169.254/latest/meta-data/profile -H “X-aws-ec2-metadata-token: $TOKEN”
not useful for exam -_-
Add another aws account to your local aws cli. Here’s a hint.
~/.aws $ cat credentials
[default]
aws_access_key_id = asdf
aws_secret_access_key = zxcv
~/.aws $ aws configure –profile my-other-aws-account
AWS Access Key ID [None]: qwer
AWS Secret Access Key [None]: poiu
Default regoin name [None]: us-west-2
Default output format [None]:
now if you
~/.aws $ cat credentials
you see
[default]
aws_access_key_id = asdf
aws_secret_access_key = zxcv
[my-other-aws-account]
aws_access_key_id = qwer
aws_secret_access_key = poiu
and if you
$ ~/.aws $ cat config
[default]
region = eu-west-3 (or whatever it is)
[profile my-other-aws-account]
region = eu-west-2
not useful for exam -_-
Using AWS cli, how you execute the following line using an already local-cli-configured non-default profile named “my-other-aws-account”?
~/.aws $ aws s3 ls –profile my-other-aws-account
can come up on exam
MFA WITH CLI
1. What’s the api you have to call if you want to run MFA with the CLI?
2. What’s the command line command for it?
- STS GetSessionToken
- $ aws sts get-session-token –serial-number-arn-of-the-mfa-device –token-code code-from-token –duration-seconds 3600
Subsection: AWS CLI with MFA
Say you run this on locally configured aws cli (possibly also using aws cli). Assume you already have the serial-number-arn-of-the-mfa-device and code-from-token-no-dash-separating-token-halves values.
1. Give an example of the kind of response you are expecting.
2. Give an example of how to use that response to run something like “$ aws s3 ls –profile mfa”. Just make up the values, but try to get the cli commands and keys correct.
$ aws sts get-session-token –serial-number-arn-of-the-mfa-device –token-code code-from-token-no-dash-separating-token-halves –duration-seconds 3600
- {“Credentials”: {
“SecretAccessKey”: “secret-access-key”,
“SessionToken”: “temporary-session-token”,
“Expiration”: “expiration-date-time”,
“AccessKeyId”: “access-key-id”
}} - $ aws configure –profile mfa
AWS Access Key ID [None]: “access-key-id”
AWS Secret Access Key [None]: “secret-access-key”
Default region name [None]:
Default output format [None]:
~ $ open ~/.aws/credentials -a TextEdit
[default]
whatever my default values are
[mfa]
aws_access_key_id = “access-key-id”
aws_secret_access_key = “secret-access-key”
aws_session_token = “temporary-session-token”
now after you save that you should be able to run “$ aws s3 ls –profile mfa” in your local aws cli and get results.
Subsection: AWS CLI with MFA
- How you get the arn number you need to use mfa with cli?
- Give an exmaple of the arn, making up the numbers, assuming the User you’re doing this for is ‘Anna’
- AWS -> IAM -> Users -> Anna -> Security credentials -> Assign MFA device -> virtual MFA device -> scan QR code (google authenticator picked it up for me) -> enter MFA codes -> Assign MFA -> Now back in Security credentials there’s an arn for Assigned MFA device.
- arn:aws:iam::12341234:mfa/anna
Subsection: AWS CLI with MFA
What do you use if you want to perform actions on AWS directly for your applications code?
* A) Keep dreaming
* B) Use AWS SDK
B ) Use AWS SDK
Subsection AWS SDK Overview
Which of the following are official SDKs?
Java, .NET, Node.js, PHP, Python, Go, Ruby, C++
All
Subsection AWS SDK Overview
can be tested on this!
What is the default region chosen for AWS SDK?
us-east-1
Subsection: AWS SDK Overview
How many times you can call an AWS API in a row. Ex:
API Rate Limits
* DescribeInstances: API for EC2 with a limit of 100 calls per second
* GetObject: on S2 has a limit of 5500 GET per second per prefix
* for Intermittend Errors: implement an Exponentnial Backoff
* For consistent errros: request an API throttling limit increase
Service Quotas (Service Limits)
* Running on demand standard instances: 1152 vCPY
* you can request a service limit increase bu opening a ticket
* you can request a service quota increase by using the Service Quotas API
exam question
If you get a ThrottlingException intermittendly (because you’re doing too many API calls) use exponential backoff
This retry mechanism is already included in the AWS SDK API calls.
Must implement yourself if using the AWS API as-is or in specific cases.
- True/False: You must only implement the retries on 5xx server errors and throttling
- True/False: Do not implement on the 4xx client errors
- True/False: Exponential Backoff works by increasing the amount of wait between request retries. For example. if the first AWS API request fails, there’s a 1 second wait before the second attempt. If that one fails, there’s a 2 second wait before the 3rd attempt. If that one fails, it’s a 3 second wait etc.
True and True and True
Exponential Backoff & Service Limit
Can come up in one exam question
CLI will look for credentials in this order:
1. Command line options: –region, –output, –profile
2. Environment vars: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN
3. CLI credentials file: $ aws configure
(possibly at ~/.aws/configure on Linux and MAC and C:\Users\USERNAME.aws\credentials on Windows)
4. CLI configuration file: $ aws configure
(possibly at ~/.aws/configure on Linux and MAC and C:\Users\USERNAME.aws\config on Windows)
5. Container credentials: for ECS tasks
6. Instance profile credentials: for ec2 instance profiles
AWS Credentials Provider & Chain
Can come up in one exam question
The Java SDK will look for creds in this order:
1. Java sys props: aws.accessKeyId and aws.secretKey
2. env vars: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
3. the default creds profiles file: ex at ~/.aws/credentials
4. Amazon ECS container creds: for ecs containers
5. instance profile credentials: used on ec2 instances
6.
Can come up in one exam question
- Say you deploy an app on an ec2 instance and you’re using env vars from an iam user to call the amazon s3 api (bad practice but we’re doing it anyway).
- The IAM user has S3FullAccess permissions (can do whatever they want on any S3 bucket).
- The app only uses one S3 bucket so according to best praactices an IAM Role and EC2 Instance Profile was created for the EC2 Instance. The Role was assigned the min permissions to access that one S3 bucket.
The IAM Instance Profile was assigned to the EC2 Instance, but it still had access to all the S3 buckets. Why?
The credentials chain (order of operations/order in which it prioritizes which creds get applies) the environmental variables of the IAM user on the EC2 instance are ranked higher (and so take effect) than the IAM Role and EC2 Instance profile permissions.