Skip to content

AWS

Description

AWS est un des leaders du cloud public

Voici quelques commandes et tips utiles.

Vrac

Lister les AMI disponibles

aws ec2 describe-images \
 --owners self amazon \
 --filters \
  "Name=root-device-type,Values=ebs" \
  "Name=platform,Values=windows" \
  "Name=name,Values=Windows_Server-2016-English-Full-SQL_2019_Enterprise-*" \
 --query 'Images[*].[CreationDate, ImageId, ImageLocation]' \
 --region eu-west-3 \
 --output text | sort

Copier des fichiers depuis S3 en utilisant le SDK AWS

Copy-S3Object \
 -BucketName bucket1 \
 -Key filename.zip \
 -LocalFile c:\filename.zip \
 -AccessKey TOTO6D5PPQQ2LPZPLJEL \
 -SecretKey TOTOLweteQa6ySAeNtc38AzXOm3PMxFOgAyj2ZVo \
 -SessionToken TOTOZXIvYXdzEN7//////////wEaDJYbMm98ZG4UTbBjkCKVAsCQNmz0918n3FWOACMWBhewnRZimYCM/iRXAc0z5gQ75tzYnnB8VoYCoG6rG5ozhsdR8dgJsWL/8MgTIhcH5a71y9cnZwzrX+PmuAVfCWdue1PPWy9h4lYDAnFC0vkhTg5uyRibZsD7MoGkD8wC1WDCqSeke1DrIjGQ6nR5OmoxjPgYqO3Usz/26IifvKqbVx1N3PSZ/cuWobHht2SiF2kwwJJNy85rJxRvAMhm+2LbfOo9Dbkp0giCgkxLUPB57hoyOK2VrTH1fbqS3BWrMqEK1+jEbFXALRRCyKX6LxDQh6GajBuU6lfqk090SDe3haYODgrlqcJ1Pa3obFYYJ3Y/drWUXmLZxFRkHiHtQypKS/QxB3gonJzgkgYyMye1LBEZ3bT1V1Apiqm7YyA2F9A6I0vTwhpBp1hbyrbi0POzmhShhvuJm9oeyFl3cqjk8w== \
 -Region eu-west-3

Lister les EC2 de toutes les régions avec leur état

for region in $(aws ec2 describe-regions --query "Regions[*].RegionName" --output text); do echo "Region: $region"; aws ec2 describe-instances --region $region --query "Reservations[*].Instances[*].{InstanceID:InstanceId,State:State.Name,RunPeriod:Tags[?Key=='RunPeriod'].Value | [0]}" --output table; done

Lister les EC2 de toutes les régions en état Running

for region in $(aws ec2 describe-regions --query "Regions[*].RegionName" --output text); do echo "Region: $region"; aws ec2 describe-instances --region $region --filters "Name=instance-state-name,Values=running" --query "Reservations[*].Instances[*].{InstanceID:InstanceId,State:State.Name,RunPeriod:Tags[?Key=='RunPeriod'].Value | [0]}" --output table; done

Envoyer une requete d'invalidation de cache à une distribution CloudFront

Exemple pour une distribution dont le commentaire = 'toto.arcanexus.com' :

aws cloudfront create-invalidation --distribution-id $(aws cloudfront list-distributions --query "DistributionList.Items[?Comment=='toto.arcanexus.com'].Id | [0]" --output text) --paths "/" "/*"

Se connecter à une instance EC2 via SSM

Exemple pour une VM ayant un tag "Name" = "awsproxy" sur eu-west-3 :

aws ssm start-session --target $(aws ec2 describe-instances --filters "Name=tag:Name,Values=awsproxy" "Name=instance-state-name,Values=running" --region eu-west-3 --query "Reservations[*].Instances[*].InstanceId" --output text) --region eu-west-3

Exécution de commandes via SSM

#!/bin/bash
TIMEOUT=600
AWS_DEFAULT_REGION=eu-west-3
INSTANCETAGNAME="awsproxy"
INSTANCEIDS=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=${INSTANCETAGNAME}" "Name=instance-state-name,Values=running" --region ${AWS_DEFAULT_REGION} --query "Reservations[*].Instances[*].InstanceId" --output text)

SSMCOMMANDS="echo 'Hello World'; echo 'Hello World 2'; echo 'Hello World 3'"

EXECUTION=$(aws ssm send-command --instance-ids $INSTANCEIDS --document-name "AWS-RunShellScript" --comment "Running a script" --parameters "commands=[\"$SSMCOMMANDS\"]" --region eu-west-3 --timeout-seconds $TIMEOUT)
EXECUTIONID=$(echo $EXECUTION | jq -r '.Command.CommandId')

TRY=1
while true; do
  RESULT=$(aws ssm list-command-invocations --command-id $EXECUTIONID --details --region ${AWS_DEFAULT_REGION})
  STATUS=$(echo $RESULT | jq -R '.' | jq -s '.' | jq -r 'join("")' | jq -r '.CommandInvocations[0].Status')
  if [[ "$STATUS" == "Success" ]]; then
    echo "Success"
    echo "Output: $(echo $RESULT | jq -R '.' | jq -s '.' | jq -r 'join("")' | jq -r '.CommandInvocations[0].CommandPlugins[0].Output')"
    break
  elif [[] $TRY -gt $TIMEOUT ]]; then
    echo "Timeout"
    exit 1
  fi
  sleep 5
  TRY=$((TRY+1))
done

Last update: January 13, 2025
Created: September 25, 2023