AWS CLIでよく使うコマンド
よく使うやつ
code:ec2.bash
# VPC一覧
aws ec2 describe-vpcs | jq -r '.Vpcs[] | [(.Tags[] | select(.Key=="Name") | .Value), .VpcId, .CidrBlock, .OwnerId] | @tsv' | column -t
# EC2インスタンス検索
aws ec2 describe-vpcs --filters Name="tag:Name",Values="${vpc-name1,vpc-name2}"
aws ec2 describe-security-groups --filters Name="tag:Name",Values="${sg-name1,sg-name2}"
code:ssm-parameter.bash
aws ssm get-parameters-by-path --path "${path}" --with-decryption | jq -r '.Parameters[] | select (.Name | contains("${name1}") or contains("${name2}"))'
aws ssm get-parameters-by-path --path "${path}" | jq -r '.Parameters[] | select (.Name | contains("${name1}") or contains("${name2}")) | .Name'
aws ssm get-parameters-by-path --path "${path}" --with-decryption | jq -r '.Parameters[] | .Name, .Value, .Version | @tsv' | column -t aws ssm get-parameters-by-path --path "${path}" --with-decryption | jq -r '.Parameters[] | select (.Value == "${value}") | .Name, .Value, .Version | @tsv' | column -t aws ssm put-parameter --name "/${path}/${key_name}" --value "${value}" --type SecureString --overwrite
code:ecr.bash
## aws-cli v2
aws ecr get-login-password | docker login --username AWS --password-stdin ${registry}
docker pull ${registry}/${repository}:${tag}
docker build -t ${image_name} ${directory_path}
docker tag ${image_name} ${registry}/${repository}:${tag}
docker push ${registry}/${repository}:${tag}
sudo docker exec -it ${container_id} bash
code: cliから複数のアカウントに同じ操作を一気にやりたい場合.bash
for PROFILE in ${PROFILE_LIST@}; do export AWS_DEFAULT_PROFILE=$PROFILE
echo "----------"
echo $AWS_DEFAULT_PROFILE
# aws command xxx
done
code:S3バケット名とバージョニング有効/無効を良い感じに表示する.bash
aws s3api list-buckets | jq -r '.Buckets[].Name' | xargs -I{} sh -c 'echo {} && aws s3api get-bucket-versioning --bucket {} | jq -r '.Status''
code:elasticache.bash
aws elasticache describe-replication-groups --query "ReplicationGroups*.{_id:ReplicationGroupId,cluster_enabled:ClusterEnabled,transit_encryption:TransitEncryptionEnabled,at_rest_encryption:AtRestEncryptionEnabled}" --output table 参考
jqを使うパターン
--queryを使うパターン