From 86bd4ad85ed3170ae147317a40b5205dd6108323 Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Tue, 29 Apr 2025 22:39:13 +0200 Subject: [PATCH] chore(aws): revise commands --- snippets/aws/ec2.fish | 43 +++++++++++++++++++++++++++++--- snippets/aws/other commands.fish | 37 ++++++++++++++------------- 2 files changed, 59 insertions(+), 21 deletions(-) diff --git a/snippets/aws/ec2.fish b/snippets/aws/ec2.fish index d621415..d057e7c 100644 --- a/snippets/aws/ec2.fish +++ b/snippets/aws/ec2.fish @@ -17,6 +17,13 @@ aws ec2 describe-images --output 'yaml' \ aws ec2 describe-images --image-ids 'ami-01234567890abcdef' aws ec2 describe-images --image-ids 'ami-01234567890abcdef' --query 'Images[].Description' +# List AMIs with a specific name tag by LastLaunchedTime in descending order +aws ec2 describe-images --filters 'Name=tag:Name,Values=[GitLabRunnerBaseline]' \ + --query 'reverse(sort_by(Images, &LastLaunchedTime))[]' + +# Delete AMIs +aws ec2 deregister-image --dry-run --image-id 'ami-0123456789abcdef0' + # Check instances are available for use with SSM aws ssm get-connection-status --query "Status=='connected'" --output 'text' --target 'i-0915612ff82914822' @@ -62,10 +69,10 @@ aws ec2 describe-instances --output 'text' \ --filters 'Name=tag:Name,Values=Prometheus' 'Name=instance-state-name,Values=running' \ --query 'Reservations[].Instances[0].BlockDeviceMappings[*].Ebs.VolumeId' -# Change volume type +# Change volumes' type aws ec2 modify-volume --volume-type 'gp3' --volume-id 'vol-0123456789abcdef0' -# Migrate gp2 volumes to gp3 +# Migrate all gp2 volumes to gp3 aws ec2 describe-volumes --filters "Name=volume-type,Values=gp2" --query 'Volumes[].VolumeId' --output 'text' \ | xargs -pn '1' aws ec2 modify-volume --volume-type 'gp3' --volume-id @@ -77,7 +84,7 @@ aws ec2 create-snapshot --volume-id 'vol-0123456789abcdef0' --description 'Manua aws ec2 describe-snapshots --snapshot-ids 'snap-0123456789abcdef0' \ --query 'Snapshots[].{"State": State,"Progress": Progress}' --output 'yaml' -# Wait for snapshots to finish. +# Wait for snapshots to finish aws ec2 wait snapshot-completed --snapshot-ids 'snap-0123456789abcdef0' # Take snapshots of EC2 volumes and wait for them to finish @@ -90,6 +97,9 @@ aws ec2 describe-instances --output 'text' \ --volume-id \ | xargs -t aws ec2 wait snapshot-completed --snapshot-ids +# Delete snapshots +aws ec2 delete-snapshot --snapshot-id 'snap-0123456789abcdef0' --dry-run + # Retrieve the security credentials for an IAM role named 's3access' from instances # IMDSv2 @@ -126,6 +136,31 @@ aws ec2 stop-instances --instance-ids 'i-0123456789abcdef0' # Terminate instances aws ec2 terminate-instances --instance-ids 'i-0123456789abcdef0' + +# Get launch templates information +aws ec2 describe-launch-templates --launch-template-names 'Gitlab Runners' + +# Get launch template versions information +aws ec2 describe-launch-template-versions --launch-template-name 'GitlabRunners' --versions '16' '$Default' '$Latest' +aws ec2 describe-launch-template-versions --launch-template-name 'GitlabRunners' --min-version '10' --max-version '12' + # Delete launch template versions aws ec2 delete-launch-template-versions --launch-template-id 'lt-0123456789abcdef0' --versions '1' --dry-run -aws ec2 delete-launch-template-versions --launch-template-name 'GitLab Runners' --versions (seq 1 10) --dry-run +aws ec2 delete-launch-template-versions --launch-template-name 'GitLabRunners' --versions (seq 1 10) --dry-run + + +# Get the snapshot ID of all AMIs with specific tags but the one used in the default version of a launch template. +CURRENT_AMI=( \ + aws ec2 describe-launch-template-versions --launch-template-name 'GitlabRunners' --versions '$Default' \ + --query 'LaunchTemplateVersions[].LaunchTemplateData.ImageId' --output 'text' \ +) \ +aws ec2 describe-images \ + --filters \ + "Name=tag:Application,Values=['GitLab']" \ + "Name=tag:Component,Values=['Runner']" \ + "Name=tag:CreatedBy,Values=['EC2 Image Builder']" \ + --query "Images[?ImageId!='$CURRENT_AMI'].ImageId" --output 'text' \ +| xargs \ + aws ec2 describe-images \ + --query "Images[].[ImageID,BlockDeviceMappings[?DeviceName=='/dev/xvda'].Ebs.SnapshotId]" --output 'text' \ + --image-ids diff --git a/snippets/aws/other commands.fish b/snippets/aws/other commands.fish index 9adfbb4..0547380 100644 --- a/snippets/aws/other commands.fish +++ b/snippets/aws/other commands.fish @@ -55,10 +55,12 @@ aws cloudfront get-cache-policy --id '01234567-89ab-cdef-0123-456789abcdef' # List available metrics aws cloudwatch list-metrics --namespace 'AWS/EC2' aws cloudwatch list-metrics --namespace 'AWS/EC2' --metric-name 'CPUUtilization' -aws cloudwatch list-metrics --namespace 'AWS/EC2' --dimensions 'Name=InstanceId,Value=i-1234567890abcdef0' --query 'Metrics[].MetricName' +aws cloudwatch list-metrics --namespace 'AWS/EC2' --dimensions 'Name=InstanceId,Value=i-1234567890abcdef0' \ + --query 'Metrics[].MetricName' # Show alarms information -aws cloudwatch describe-alarms-for-metric --metric-name 'CPUUtilization' --namespace 'AWS/EC2' --dimensions 'Name=InstanceId,Value=i-1234567890abcdef0' +aws cloudwatch describe-alarms-for-metric --metric-name 'CPUUtilization' --namespace 'AWS/EC2' \ + --dimensions 'Name=InstanceId,Value=i-1234567890abcdef0' ### @@ -74,15 +76,6 @@ aws cognito-idp list-user-pools --max-results '10' --query 'UserPools' aws cognito-idp list-users --user-pool-id 'eu-west-1_lrDF9T78a' --query "Users[?Username=='john']" -### -# ECS -# ------------------ -### - -# Execute commands in containers -aws ecs execute-command --cluster 'staging' --task '0123456789abcdefghijklmnopqrstuv' --container 'pihole' \ - --interactive --command "dd if=/dev/zero of=/spaceHogger count=16048576 bs=1024" - ### # ECR # ------------------ @@ -125,14 +118,18 @@ aws ecs list-tasks --query 'taskArns' --output 'text' --cluster 'testCluster' -- aws ecs list-tasks --output 'text' --query 'taskArns' --cluster 'testCluster' --family 'testService' \ | xargs -t aws ecs wait tasks-running --cluster 'testCluster' --tasks -while [[ $$(aws ecs list-tasks --query 'taskArns' --output 'text' --cluster 'testCluster' --service-name 'testService') == "" ]]; do sleep 1; done +while [[ $$( \ + aws ecs list-tasks --query 'taskArns' --output 'text' --cluster 'testCluster' --service-name 'testService' \ +) == "" ]]; do sleep 1; done aws ecs list-task-definitions --family-prefix 'testService' --output 'text' --query 'taskDefinitionArns' \ | xargs -pn '1' aws ecs deregister-task-definition --task-definition aws ecs list-tasks --query 'taskArns' --output 'text' --cluster 'testCluster' --service-name 'testService' \ | tee \ -| xargs -t aws ecs describe-tasks --query "tasks[].attachments[].details[?(name=='privateIPv4Address')].value" --output 'text' --cluster 'testCluster' --tasks \ +| xargs -t -I '%%' \ + aws ecs describe-tasks --cluster 'testCluster' --tasks '%%' \ + --query "tasks[].attachments[].details[?(name=='privateIPv4Address')].value" --output 'text' \ | tee \ | xargs -I{} curl -fs "http://{}:8080" @@ -170,6 +167,8 @@ aws ecs list-tasks --cluster 'staging' --service-name 'mimir' --query 'taskArns' # Execute commands in tasks aws ecs execute-command --cluster 'staging' --task 'e242654518cf42a7be13a8551e0b3c27' --container 'echo-server' \ --interactive --command 'nc -vz 127.0.0.1 28080' +aws ecs execute-command --cluster 'staging' --task '0123456789abcdefghijklmnopqrstuv' --container 'pihole' \ + --interactive --command "dd if=/dev/zero of=/spaceHogger count=16048576 bs=1024" # Stop tasks given a service name aws ecs list-tasks --cluster 'staging' --service-name 'mimir' --query 'taskArns' --output 'text' \ @@ -201,7 +200,9 @@ aws efs describe-mount-targets --query 'MountTargets[].IpAddress' --output 'text # Get mount targets' IP address from the filesystem's name. aws efs describe-mount-targets --query 'MountTargets[].IpAddress' --output 'json' \ - --file-system-id (aws efs describe-file-systems --query 'FileSystems[].FileSystemId' --output 'text' --creation-token 'fs-name') + --file-system-id ( \ + aws efs describe-file-systems --creation-token 'fs-name' --query 'FileSystems[].FileSystemId' --output 'text' \ + ) # Mount volumes. mount -t 'nfs' -o 'nfsvers=4.0,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport' \ @@ -423,7 +424,8 @@ aws rds describe-db-parameters --db-parameter-group-name 'default.postgres15' \ --query "Parameters[?ParameterName=='shared_preload_libraries']" --output 'table' aws rds describe-db-parameters --db-parameter-group-name 'default.postgres15' \ --query "Parameters[?ParameterName=='shared_preload_libraries'].ApplyMethod" --output 'text' -aws rds describe-db-parameters --db-parameter-group-name 'default.postgres15' --output 'json' --query "Parameters[?ApplyType!='dynamic']" +aws rds describe-db-parameters --db-parameter-group-name 'default.postgres15' \ + --output 'json' --query "Parameters[?ApplyType!='dynamic']" ### @@ -446,7 +448,7 @@ aws s3 cp 's3://my-first-bucket/test.txt' 's3://my-other-bucket/' aws s3api list-objects-v2 --bucket 'backup' aws s3api list-objects-v2 --bucket 'backup' --query "Contents[?LastModified>='2022-01-05T08:05:37+00:00'].Key" -aws s3api list-buckets --output 'text' --query 'Buckets[].Name' | xargs -pn '1' aws s3api list-multipart-uploads --bucket +aws s3api list-buckets --output 'text' --query 'Buckets[].Name' | xargs -n '1' aws s3api list-multipart-uploads --bucket aws --profile 'someProfile' s3api head-bucket --bucket 'someBucket' @@ -467,4 +469,5 @@ aws sns list-subscriptions --query 'Subscriptions' aws sns list-subscriptions-by-topic --topic-arn 'arn:aws:sns:eu-west-1:012345678901:aSucculentTopic' # Get information about subscriptions -aws sns get-subscription-attributes --subscription-arn 'arn:aws:sns:eu-west-1:012345678901:aSucculentTopic:abcdef01-2345-6789-abcd-ef0123456789' +aws sns get-subscription-attributes \ + --subscription-arn 'arn:aws:sns:eu-west-1:012345678901:aSucculentTopic:abcdef01-2345-6789-abcd-ef0123456789'