AWS CLI commands

Guide how to use AWS CLI for S3, Lambda, EC2, IoT

Lambda

Check metadata for lambda:

#!/bin/bash
aws lambda get-function --region us-east-1  --function-name MySuperLambda_java
#OR
aws lambda get-function --region us-east-1  --function-name MySuperLambda_java --query 'Code.Location'

Example of return:

...
"Code": {
    "RepositoryType": "S3",
    "Location": "https://aab-04-2014-tasks.s3.us-east-2.amazonaws.com/snapshots/3844232/LongHex..
....

Download sources:

wget https://aab-04-2014-tasks.s3.us-east-2.amazonaws.com/snapshots/3844232/LongHex..

S3

Download folder:

aws s3 cp s3://lambda-log-files/my_ota/  . --recursive

IoT

Check thing by name:

#!/bin/bash
aws iot describe-thing \
    --region us-east-1 \
    --thing-name $1

EC2

Check instances:

#!/bin/bash
aws ec2 describe-instances \
    --output table \
    --query "Reservations[*].Instances[*].{
    Instance:InstanceId,
    Type:InstanceType,
    KeyName:KeyName,
    Name:Tags[?Key==\`Name\`]|[0].Value,
    Project:Tags[?Key==\`Project\`]|[0].Value,
    IP:PublicIpAddress,
    State:State.Name,
    CPUcores:CpuOptions.CoreCount,
    GID:NetworkInterfaces[0].Groups[0].GroupName,
    VolumeId:BlockDeviceMappings[0].Ebs.VolumeId,
    CPUThreads:CpuOptions.ThreadsPerCore
}" | tee 

Creating instance:

#!/bin/bash
KEY_NAME="PAT-TO-SSH-KEY"
INST_NAME="Custom Name"
INST_TAG_PURP=$(date +"BUILD_%Y_%m_%d_%H_%M_%S")
#Instance type
TYPE=r5a.4xlarge
#IMAGE_ID="ami-03a0c45ebc70f98ea" #18.04 server
IMAGE_ID="ami-0fb653ca2d3203ac1" #20.04 server
#Volume size
EBS_SIZE=300

#1IP Grigory in CICD
SG_IDS="sg-015e1322224c272 sg-015e1322224c272 "

#Public sandbox
#SUBNET="subnet-015e1322224c272"

#Public build in build-vpc
SUBNET="subnet-0b185244443a4c9"

echo "Instance creation...."

R=$(\
    aws ec2 run-instances    \
    --image-id $IMAGE_ID \
    --count 1 \
    --instance-type $TYPE \
    --key-name $KEY_NAME \
    --security-group-ids $SG_IDS \
    --subnet-id $SUBNET \
    --associate-public-ip-address \
    --tag-specifications 'ResourceType=instance,Tags=[{Key=purpose,Value="'"$INST_TAG_PURP"'"},{Key=Name, Value="'"$INST_NAME"'"},{Key=Project, Value="'"$PROJECT_NAME"'"}]' \
    --block-device-mappings 'DeviceName=/dev/sda1,Ebs={VolumeSize="'"$EBS_SIZE"'",DeleteOnTermination=true}'
    )
echo $R

insID=$(aws ec2 describe-instances --filters Name=tag:purpose,Values="'"$INST_TAG_PURP"'" --query 'Reservations[*].Instances[*].{Instance:InstanceId}' --output text)
echo "Instance is created[$insID]"
echo "DONE"
comments powered by Disqus