AWS/CLI

From Christoph's Personal Wiki
Revision as of 22:40, 12 March 2021 by Christoph (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This article will contain a (random) collection of Amazon Web Services - Command Line Interface (AWS CLI) tips-and-tricks. It will also have examples using boto and other miscellaneous tips-and-tricks.

  • Find all AWS resources that do not have a tag with a key of "project" assigned to them:
$ aws resourcegroupstaggingapi get-resources --tags-per-page 100 --output json |\
    jq '.ResourceTagMappingList[] | select(contains({Tags: [{Key: "project"} ]}) | not)'
{
  "ResourceARN": "arn:aws:ec2:us-west-2:0000000:security-group/sg-1aaaaaaa",
  "Tags": [
    {
      "Value": "xtof-aws-dev-sg",
      "Key": "Name"
    }
  ]
}
  • If you have AWS Config setup, you can do advanced SQL-like queries:
SELECT
  resourceId,
  resourceType,
  configuration.instanceType,
  configuration.placement.tenancy,
  configuration.imageId,
  tags,
  availabilityZone
WHERE
  resourceType = 'AWS::EC2::Instance'
  AND tags.key NOT LIKE 'owner'
  • Tagging:
$ aws ec2 run-instances \
    --image-id ami-abc12345 \
    --count 1 \
    --instance-type t2.micro \
    --key-name my-ssh-key \
    --subnet-id subnet-aaaaaaaa \
    --tag-specifications 'ResourceType=instance,Tags=[{Key=webserver,Value=production}]' 'ResourceType=volume,Tags=[{Key=cost-center,Value=cc123}]'
  • CloudFormation:
$ export MY_STACK_NAME=<your_stack_name>
$ aws cloudformation create-stack \
    --template-body file://path/to/your/template.json \
    --stack-name=${MY_STACK_NAME}

# Creating a stack with tags
$ aws cloudformation create-stack \
    --template-body file://path/to/your/template.json \
    --stack-name=${MY_STACK_NAME} \
    --tags="Key=env,Value=dev"

# Updating a stack with tags
$ aws cloudformation update-stack \
    --template-body file://path/to/your/template.json \
    --stack-name=${MY_STACK_NAME} \
    --tags="Key=env,Value=dev"

See: update-stack for details.

External links