AWS CLI, SDK, IAM Roles and Policies Flashcards
bad way to run AWS CLI on EC2 instance
- never use “aws configure” with your credentials as you do it on the local computer. NEVER put your credentials on EC2 machine. If EC2 is somehow compromised, everything else is.
- instead use AWS IAM roles.
To set up AWS console on the local computer
- install CLI
- run aws –version in cmd to check
- go to IAM Management console and create Access keys
- in cmd run aws configure and insert these credentials. They will be stored in .aws in special files
good way to run AWS CLI on EC2 instance
use AWS IAM roles.
We can attach IAM role to EC2 instance. IAM Roles can come with a policy which defines exactly what EC2 instance should be able to do.
- So we ssh into EC2. If it’s Linux, we don’t need to install CLI, it’s already there. So we can run “aws” command directly in shell. We can run “aws configure” with empty credentials.
- After that if the EC2 instance doesn’t have IAM roles attached - we will not be allowed to run any meaningful aws commands because we are not authorized through credentials
- go to IAM –> Roles and create Role
For ex., if we want to run aws s3 ls from EC2 instance -
we create a Role for EC2 service and attach a managed policy AmazonS3ReadOnlyAccess - Attach the role to the EC2 instance
now we can list S3 buckets from the instance with aws s3 ls
tools to work with IAM policies
IAM policy generator - to create your own policies
IAM policy simulator - to test it
we have to use AWS SDK when
coding against AWS Services such as DynamoDB
AWS SDK Credentials security
it’s recommended to use the default credential provider chain
NEVER STORE AWS CREDENTIALS IN YOUR CODE
the default credential provider chain will work seamleassly with
- AWS credentials at .aws\credentials
- Instance Profile credentials using IAM roles (for EC2 machines)
- Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
Exponential BackOff
your first API call after failure will wait maybe 10 millisecond, your second API call will run after 20 millisecond. And so on.
the next API call, if it still fails, it will wait double of that time. Exponential Backoff means that if your API calls still keep on failing we will wait twice as long as the previous API call to try again, and that ensures that you don’t overload the API by trying it every millisecond.
Exponential Backoff is included in most SDKs
My EC2 Instance does not have the permissions to perform an API call PutObject on S3. What should I do?
attach a Policy to the IAM Role on my EC2 instance that authorizes it to do the API call
I have an on-premise personal server that I’d like to use to perform AWS API calls
I should run “aws configure” and put my credentials there. Invalidate them when I’m done
Even better would be to create a user specifically for that one on-premise server
I need my colleagues help to debug my code. When he runs the application on his machine, it’s working fine, whereas I get API authorisation exceptions. What should I do?
compare his and mine IAM Policy in the policy simulator to understand the differences