AWS CLI: Difference between revisions

From SWKLS WIKI
Jump to navigation Jump to search
(Created page with "==Multiple Profiles== Source: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html#cli-quick-configuration-multi-profiles ===Default Profile=== Example of...")
 
 
(10 intermediate revisions by the same user not shown)
Line 4: Line 4:
===Default Profile===
===Default Profile===
Example of configuring AWS CLI before use:
Example of configuring AWS CLI before use:
<syntaxhighlight lang="bash">
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
</syntaxhighlight>


$ aws configure AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
<code>
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-east-1
Default output format [None]: json
</code>
===Creating Multiple Profiles===
===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 <code>--profile</code> option and assigning a name. The following example creates a profile named <code>produser</code>. You can specify credentials from a completely different account and region than the other 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 <code>--profile</code> option and assigning a name. The following example creates a profile named <code>produser</code>. You can specify credentials from a completely different account and region than the other profiles.
<code>
<syntaxhighlight lang="bash">
$ aws configure --profile produser
aws configure --profile produser
AWS Access Key ID [None]: AKIAI44QH8DHBEXAMPLE
AWS Access Key ID [None]: AKIAI44QH8DHBEXAMPLE
AWS Secret Access Key [None]: je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
AWS Secret Access Key [None]: je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
Default region name [None]: us-east-1
Default region name [None]: us-east-1
Default output format [None]: text
Default output format [None]: text
</code>
</syntaxhighlight>
Then, when you run a command, you can omit the <code>--profile</code> option and use the credentials and settings stored in the <code><default></code> profile.
Then, when you run a command, you can omit the <code>--profile</code> option and use the credentials and settings stored in the <code><default></code> profile.
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 29: Line 30:
$ aws s3 ls --profile produser
$ aws s3 ls --profile produser
</syntaxhighlight>
</syntaxhighlight>
To update any of your settings, simply run <code>aws configure</code> again (with or without the <code>--profile</code> parameter, depending on which profile you want to update) and enter new values as appropriate.  
To update any of your settings, simply run <code>aws configure</code> again (with or without the <code>--profile</code> parameter, depending on which profile you want to update) and enter new values as appropriate.


Notes on Various Commands
==AWS LightSail==
===Instance Snapshot Examples===
<syntaxhighlight lang="bash">
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`].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`]' --output table
Line 42: Line 45:
aws lightsail get-instance-snapshots --query "sort_by(instanceSnapshots, &createdAt)[*].[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
aws lightsail get-instance-snapshots --query 'instanceSnapshots[?fromInstanceName==`${option.Instance}`].name' --output table
</syntaxhighlight>
===Instance State Examples===
<syntaxhighlight lang="bash">
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' --output text
aws lightsail get-instance --instance-name "Amazon_Linux-4GB-Virginia-1-swkls4" --query 'instance.state.name' --output text
aws lightsail get-instance --instance-name "Amazon_Linux-4GB-Virginia-1-swkls4" --query 'instance.state.name' --output text
</syntaxhighlight>
===Instance IP Address Examples===
<syntaxhighlight lang="bash">
aws lightsail get-instance --instance-name "Amazon_Linux-4GB-Virginia-1-swkls4" --query 'instance.publicIpAddress' --output text
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 lightsail get-static-ip --static-ip-name "StaticIp-Virginia-1-swkls4" --query 'staticIp.ipAddress' --output text
</syntaxhighlight>
===AWS and RunDeck User Profile Examples===
====Create Instance====
<syntaxhighlight lang="bash">
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"
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"
 
</syntaxhighlight>
====Static IP Examples====
<syntaxhighlight lang="bash">
aws lightsail allocate-static-ip --profile "rundeck" --static-ip-name "swkls-sandbox-ip"
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"
aws lightsail attach-static-ip --profile "rundeck" --static-ip-name "swkls-sandbox-ip" --instance-name "swkls-sandbox-nano"
 
</syntaxhighlight>
====Instance Reboot====
<syntaxhighlight lang="bash">
aws lightsail reboot-instance --profile "rundeck" --instance-name "swkls-sandbox-nano"
aws lightsail reboot-instance --profile "rundeck" --instance-name "swkls-sandbox-nano"
 
</syntaxhighlight>
====Get Static IP====
(after reboot)
<syntaxhighlight lang="bash">
aws lightsail get-static-ip --profile "rundeck" --static-ip-name "swkls-sandbox-ip"
aws lightsail get-static-ip --profile "rundeck" --static-ip-name "swkls-sandbox-ip"
 
</syntaxhighlight>
====Open port 443====
<syntaxhighlight lang="bash">
aws lightsail open-instance-public-ports --profile "rundeck" --instance-name "swkls-sandbox-nano" --port-info fromPort=443,protocol=TCP,toPort=443
aws lightsail open-instance-public-ports --profile "rundeck" --instance-name "swkls-sandbox-nano" --port-info fromPort=443,protocol=TCP,toPort=443
</syntaxhighlight>


===BASH Script State Test===
BASH example for testing state and waiting til running:
BASH example for testing state and waiting til running:
<syntaxhighlight lang="bash">
while STATE=$(aws lightsail get-instance --instance-name "Amazon_Linux-4GB-Virginia-1-swkls4" --query 'instance.state.name' --output text); test "$STATE" != "running"; do
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;
     sleep 30;
done;
done;
</syntaxhighlight>
===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.
<syntaxhighlight lang="bash">
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"
</syntaxhighlight>
==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/)
<syntaxhighlight lang="bash">
aws s3api list-objects-v2 --bucket swkls.videos --profile default --prefix zoom.videos/zoom_archives/2020/ --output text > ./z2020.txt
</syntaxhighlight>
[[Category:AWS]]

Latest revision as of 15:05, 18 March 2021

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