AWS CLI

From SWKLS WIKI
Revision as of 15:05, 18 March 2021 by Unknown user (talk)
Jump to navigation Jump to search

Multiple Profiles

Source: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html#cli-quick-configuration-multi-profiles

Default Profile

Example of configuring AWS CLI before use:

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

Creating Multiple Profiles

If you use the command shown in the previous section, the result is a single profile named default. You can create additional configurations that you can refer to with a name by specifying the --profile option and assigning a name. The following example creates a profile named produser. You can specify credentials from a completely different account and region than the other profiles.

aws configure --profile produser
 AWS Access Key ID [None]: AKIAI44QH8DHBEXAMPLE
 AWS Secret Access Key [None]: je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
 Default region name [None]: us-east-1
 Default output format [None]: text

Then, when you run a command, you can omit the --profile option and use the credentials and settings stored in the <default> profile.

$ aws s3 ls

Or you can specify a --profile <profilename> and use the credentials and settings stored under that name.

$ aws s3 ls --profile produser

To update any of your settings, simply run aws configure again (with or without the --profile parameter, depending on which profile you want to update) and enter new values as appropriate.

AWS LightSail

Instance Snapshot Examples

aws lightsail get-instance-snapshots --query 'instanceSnapshots[?fromInstanceName==`Amazon_Linux-4GB-Virginia-1-swkls3`].name' --output table
aws lightsail get-instance-snapshots --query 'instanceSnapshots[?fromInstanceName==`Amazon_Linux-4GB-Virginia-1-swkls3`]' --output table
aws lightsail get-instance-snapshots --query 'instanceSnapshots[?fromInstanceName==`Amazon_Linux-4GB-Virginia-1-swkls3`].arn' --output table
aws lightsail get-instance-snapshots --query 'instanceSnapshots[].[name]'
aws lightsail get-instance-snapshots --query 'instanceSnapshots[]'
aws lightsail get-instance-snapshots --query 'instanceSnapshots[].[fromInstanceName,name]'
aws lightsail get-instance-snapshots --query 'instanceSnapshots[].[fromInstanceName,name]' --output table
aws lightsail get-instance-snapshots --query 'instanceSnapshots[?].[fromInstanceName,name]' --output table
aws lightsail get-instance-snapshots --query "sort_by(instanceSnapshots, &createdAt)[*].[fromInstanceName,name]" --output table
aws lightsail get-instance-snapshots --query 'instanceSnapshots[?fromInstanceName==`${option.Instance}`].name' --output table

Instance State Examples

aws lightsail get-instance --instance-name "Amazon_Linux-4GB-Virginia-1-swkls4" --query 'instance.state' --output text
aws lightsail get-instance --instance-name "Amazon_Linux-4GB-Virginia-1-swkls4" --query 'instance.state.name' --output text

Instance IP Address Examples

aws lightsail get-instance --instance-name "Amazon_Linux-4GB-Virginia-1-swkls4" --query 'instance.publicIpAddress' --output text
aws lightsail get-static-ip --static-ip-name "StaticIp-Virginia-1-swkls4" --query 'staticIp.ipAddress' --output text

AWS and RunDeck User Profile Examples

Create Instance

aws lightsail create-instances --profile "rundeck" --instance-names "swkls-sandbox-nano" --blueprint-id  "amazon_linux_2018_03_0_1" --availability-zone "us-east-1a" --bundle-id "nano_2_0"

Static IP Examples

aws lightsail allocate-static-ip --profile "rundeck" --static-ip-name "swkls-sandbox-ip"
aws lightsail attach-static-ip --profile "rundeck" --static-ip-name "swkls-sandbox-ip" --instance-name "swkls-sandbox-nano"

Instance Reboot

aws lightsail reboot-instance --profile "rundeck" --instance-name "swkls-sandbox-nano"

Get Static IP

(after reboot)

aws lightsail get-static-ip --profile "rundeck" --static-ip-name "swkls-sandbox-ip"

Open port 443

aws lightsail open-instance-public-ports --profile "rundeck" --instance-name "swkls-sandbox-nano" --port-info fromPort=443,protocol=TCP,toPort=443

BASH Script State Test

BASH example for testing state and waiting til running:

while STATE=$(aws lightsail get-instance --instance-name "Amazon_Linux-4GB-Virginia-1-swkls4" --query 'instance.state.name' --output text); test "$STATE" != "running"; do
    sleep 30;
done;

Create Alarm Example

This will create an alarm for instance "CentOS-sandbox" that sends an email alert when when burst capacity falls below 25 percent remaining.

aws lightsail put-alarm --alarm-name "burstcap" --metric-name "BurstCapacityPercentage" --monitored-resource-name "CentOS-sandbox" --comparison-operator "LessThanOrEqualToThreshold" --threshold 25.0 --evaluation-periods 2 --datapoints-to-alarm 2 --treat-missing-data "notBreaching" --contact-protocols "Email" --notification-triggers "ALARM"

AWS S3 API

List Objects and Keys

This will output a list of objects in a given bucket & folder and their keys (partial URL, add https://s3.amazonaws.com/<bucket_name>)

aws s3api list-objects-v2 --bucket swkls.videos --profile default --prefix zoom.videos/zoom_archives/2020/ --output text > ./z2020.txt