The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.
| Order | Prod Use | Option | Description |
|---|---|---|---|
| 1 | ❌ | Command Line Options | aws [command] --profile [profile name] - Profile data uses long term credentials stored locally. This is not recommended for production or instances with public access. |
| 2 | ❌ | Environment Variables | You can store values in environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN. Recommended for temp use in non-production environments. |
| 3 | ❌ | AWS CLI Credentials File | aws configure - Command creates a credentials file stored in ~/.aws/credentials on Linux, MacOSm or Unix, or at C:\Users\USERNAME\.aws\credentials on Windows. THis approach uses long term credential stored locally and is not recommended for production or instances with public access. |
| 4 | ✔️ | Container Credentials | IAM roles associated with AWS Elastic Container Service (ECS) Task Definitions. Temp credentials are available to the Task's containers. This is recommended for ECS environments. |
| 5 | ✔️ | Instance Profile Credentials | IAM roles assiciated with Elastic Compute Cloud (EC2) instances via Instance Profiles. Temp credentials are available to the instance. This is recommended for EC2 environments. |
Install AWS CLI https://aws.amazon.com/cli/
Creds and options stored in ~/.aws/credentials (Linux & Mac) or
%USERPROFILE%\.aws\credentials (Windows)
aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-east-1
Default output format [None]: json
~/.aws/credentials (Linux & Mac) or %USERPROFILE%\.aws\credentials (Windows)
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
[user1]
aws_access_key_id=AKIAI44QH8DHBEXAMPLE
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
[default]
region=us-west-2
output=json
[profile user1]
region=us-east-1
output=text
export AWS_PROFILE=user1
setx AWS_PROFILE user1
aws s3 ls --profile user1
aws s3 mb s3://bucketname
aws s3 ls
aws s3 ls s3://bucketname
aws s3 cp filename.txt s3://bucketname
aws s3 sync .\local_folder s3://bucketname/folder_name
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::BUCKET_NAME/*"
]
}
]
}In this example, we have a VPC containing a bastion host and webserver. We want to add a development role for S3 access from our webserver. DO NOT STORE AWS ACCESS KEYS on instances exposed to the public (use roles).
Open a terminal on the bastion host withing the AWS VPC and create a file:
vi trust_policy_ec2.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}
]
}aws iam create-role --role-name DEV_ROLE --assume-role-policy-document file://trust_policy_ec2.json
vi dev_s3_read_access.json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowUserToSeeBucketListInTheConsole",
"Action": ["s3:ListAllMyBuckets", "s3:GetBucketLocation"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::*"]
},
{
"Effect": "Allow",
"Action": [
"s3:Get*",
"s3:List*"
],
"Resource": [
"arn:aws:s3:::<DEV_S3_BUCKET_NAME>/*",
"arn:aws:s3:::<DEV_S3_BUCKET_NAME>"
]
}
]
}aws iam create-policy --policy-name DevS3ReadAccess --policy-document file://dev_s3_read_access.json
aws iam attach-role-policy --role-name DEV_ROLE --policy-arn "<POLICY_ARN_FROM_LAST_STEP>"
aws iam list-attached-role-policies --role-name DEV_ROLE
Get the policy details, including the current version (be sure to replace <POLICY_ARN_FROM_LAST_STEP> with the policy ARN from earlier):
aws iam get-policy --policy-arn "<POLICY_ARN_FROM_LAST_STEP>"
Get the permissions associated with the current policy version (be sure to replace the <POLICY_ARN> and <DEFAULT_VERSION_ID> with the output of the get-policy command):
aws iam get-policy-version --policy-arn "<POLICY_ARN>" --version-id "<DEFAULT_VERSION_ID>"
aws iam create-instance-profile --instance-profile-name DEV_PROFILE
aws iam add-role-to-instance-profile --instance-profile-name DEV_PROFILE --role-name DEV_ROLE
aws iam get-instance-profile --instance-profile-name DEV_PROFILE
Attach the DEV_PROFILE to an EC2 instance (be sure to replace the <LAB_WEB_SERVER_INSTANCE_ID> with the instance ID of the web server in your lab):
aws ec2 associate-iam-instance-profile --instance-id <LAB_WEB_SERVER_INSTANCE_ID> --iam-instance-profile Name="DEV_PROFILE"
Verify the configuration (be sure to replace the <LAB_WEB_SERVER_INSTANCE_ID> with the instance ID of the web server in your lab):
aws ec2 describe-instances --instance-ids <LAB_WEB_SERVER_INSTANCE_ID>
Log into the web server instance
aws sts get-caller-identity
Verify access to the <DEV_S3_BUCKET_NAME> in your lab (be sure to replace the <DEV_S3_BUCKET_NAME> with the value provided in your lab):
aws s3 ls
aws s3 ls s3://<DEV_S3_BUCKET_NAME>
Verify access is denied to the <SECRET_S3_BUCKET_NAME> (be sure to replace the <SECRET_S3_BUCKET_NAME> with the value provided in your lab):
aws s3 ls s3://<SECRET_S3_BUCKET_NAME>
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo yum install git
git clone https://github.com/<YOUR_GIT_REPO>
cd YOUR_GIT_REPO
docker build -t <name> .
docker images --filter reference=<name>
docker run -t -i -p 80:80 <name>
docker login --username YOUR_USER
docker images
docker tag IMAGEID YOUR_USER/<name>
docker push YOUR_USER/<name>
sudo amazon-linux-extras install epel -y
sudo yum install stress -y
stress --cpu 2 --timeout 30000
aws sqs get-queue-attributes --queue-url https://URL --attribute-names All
aws sqs send-message --queue-url https://URL --message-body "INSERTMESSAGE"
aws sqs receive-message --wait-time-seconds 10 --max-number-of-messages 10 --queue-url https://URL
aws sqs --region us-east-1 receive-message --wait-time-seconds 10 --max-number-of-messages 10 --queue-url https://URL
aws sqs delete-message --queue-url https://URL --receipt-handle "INSERTHANDLE"
aws kms create-key --description "DEVALEX KMS DEMO CMK"
Create an alias -- Point app to use alias since each key is region specific (duplicate alias in each region)
aws kms create-alias --target-key-id XXX --alias-name "alias/devalexkmsdemo" --region us-east-1
echo "this is a secret message" topsecret.txt
aws kms encrypt --key-id KEYID --plaintext file://topsecret.txt --output text --query CiphertextBlob
aws kms encrypt --key-id KEYID --plaintext file://topsecret.txt --output text --query CiphertextBlob > topsecret.base64.encrypted
certutil -decode topsecret.base64.encrypted topsecret.encrypted
aws kms decrypt --ciphertext-blob fileb://topsecret.encrypted --output text --query Plaintext > topsecret.decrypted.base64
certutil topsecret.decrypted.base64 topsecret.decrypted
echo "this is a secret message" topsecret.txt
aws kms encrypt --key-id KEYID --plaintext file://topsecret.txt --output text --query CiphertextBlob
aws kms encrypt --key-id KEYID --plaintext file://topsecret.txt --output text --query CiphertextBlob | base64 --decode > topsecret.encrypted
aws kms decrypt --ciphertext-blob fileb://topsecret.encrypted --output text --query Plaintext | base64 --decode
aws kms generate-data-key --key-id KEYID --key-spec AES_256 --region us-east-1