Difference between revisions of "AWS/CLI"
From Christoph's Personal Wiki
(Created page with "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 <code>b...") |
|||
| (2 intermediate revisions by the same user not shown) | |||
| Line 30: | Line 30: | ||
AND tags.key NOT LIKE 'owner' | AND tags.key NOT LIKE 'owner' | ||
</pre> | </pre> | ||
| + | |||
| + | * Tagging: | ||
| + | <pre> | ||
| + | $ 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}]' | ||
| + | </pre> | ||
| + | |||
| + | * CloudFormation: | ||
| + | <pre> | ||
| + | $ 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" | ||
| + | </pre> | ||
| + | |||
| + | See: [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/update-stack.html update-stack] for details. | ||
| + | |||
| + | ==External links== | ||
| + | * [https://docs.aws.amazon.com/ARG/latest/userguide/find-resources-to-tag.html Find resources to tag] | ||
| + | * [https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Filtering.html List and filter your resources] | ||
[[Category:AWS]] | [[Category:AWS]] | ||
Latest revision as of 22:40, 12 March 2021
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.