Redimensionner un volume Amazon EBS utilisé par un environnement - AWS Cloud9

AWS Cloud9 n'est plus disponible pour les nouveaux clients. Les clients existants d’ AWS Cloud9 peuvent continuer à l’utiliser normalement. En savoir plus

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Redimensionner un volume Amazon EBS utilisé par un environnement

Cette étape montre comment redimensionner un volume Amazon EBS.

  1. Ouvrez l'environnement associé à l'instance Amazon EC2 pour le volume Amazon EBS que vous voulez redimensionner.

  2. Dans l' AWS Cloud9 IDE pour l'environnement, créez un fichier avec le contenu suivant, puis enregistrez le fichier avec l'extension .sh (par exemple,resize.sh).

    Remarque

    Ce script fonctionne pour les volumes Amazon EBS connectés à des instances EC2 qui s'exécutent, AL2023 Amazon Linux 2, Amazon Linux ou Ubuntu Server et qui sont configurés pour être utilisés. IMDSv2

    Le script redimensionne également les volumes Amazon EBS exposés sous forme de NVMe blocs sur des instances Nitro basées sur des instances. Pour obtenir la liste des instances basées sur le système Nitro, consultez la section Instances Nitro basées dans le guide de l'utilisateur Amazon EC2.

    #!/bin/bash # Specify the desired volume size in GiB as a command line argument. If not specified, default to 20 GiB. SIZE=${1:-20} # Get the ID of the environment host Amazon EC2 instance. TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60") INSTANCEID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id 2> /dev/null) REGION=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/placement/region 2> /dev/null) # Get the ID of the Amazon EBS volume associated with the instance. VOLUMEID=$(aws ec2 describe-instances \ --instance-id $INSTANCEID \ --query "Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId" \ --output text \ --region $REGION) # Resize the EBS volume. aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE # Wait for the resize to finish. while [ \ "$(aws ec2 describe-volumes-modifications \ --volume-id $VOLUMEID \ --filters Name=modification-state,Values="optimizing","completed" \ --query "length(VolumesModifications)"\ --output text)" != "1" ]; do sleep 1 done # Check if we're on an NVMe filesystem if [[ -e "/dev/xvda" && $(readlink -f /dev/xvda) = "/dev/xvda" ]] then # Rewrite the partition table so that the partition takes up all the space that it can. sudo growpart /dev/xvda 1 # Expand the size of the file system. # Check if we're on AL2 or AL2023 STR=$(cat /etc/os-release) SUBAL2="VERSION_ID=\"2\"" SUBAL2023="VERSION_ID=\"2023\"" if [[ "$STR" == *"$SUBAL2"* || "$STR" == *"$SUBAL2023"* ]] then sudo xfs_growfs -d / else sudo resize2fs /dev/xvda1 fi else # Rewrite the partition table so that the partition takes up all the space that it can. sudo growpart /dev/nvme0n1 1 # Expand the size of the file system. # Check if we're on AL2 or AL2023 STR=$(cat /etc/os-release) SUBAL2="VERSION_ID=\"2\"" SUBAL2023="VERSION_ID=\"2023\"" if [[ "$STR" == *"$SUBAL2"* || "$STR" == *"$SUBAL2023"* ]] then sudo xfs_growfs -d / else sudo resize2fs /dev/nvme0n1p1 fi fi
  3. Depuis une séance de terminal dans l'IDE, accédez au répertoire contenant le fichier resize.sh. Exécutez ensuite l'une des commandes suivantes, en remplaçant 20 par la taille souhaitée en Gio pour redimensionner le volume Amazon EBS :

    • bash resize.sh 20
    • chmod +x resize.sh ./resize.sh 20