

 AWS Cloud9 不再提供給新客戶。 AWS Cloud9 的現有客戶可以繼續正常使用該服務。[進一步了解](https://aws.amazon.com/blogs/devops/how-to-migrate-from-aws-cloud9-to-aws-ide-toolkits-or-aws-cloudshell/)

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 為環境使用的 Amazon EBS 磁碟區調整大小
<a name="move-environment-resize"></a>

此步驟說明如何調整 Amazon EBS 磁碟區的大小。

1. 針對您要調整大小的 Amazon EBS 磁碟區開啟與 Amazon EC2 執行個體相關聯的環境。

1. 在環境的 AWS Cloud9 IDE 中，使用下列內容建立檔案，然後使用副檔名儲存檔案 `.sh`（例如 `resize.sh`)。
**注意**  
此指令碼適用於連線至執行 AL2023、Amazon Linux 2、Amazon Linux 或 Server 且設定為使用 IMDSv2 的 EC2 執行個體的 Amazon EBS 磁碟區。 Ubuntu IMDSv2  
指令碼也會為在 Nitro 型執行個體上經公開為 NVMe 區塊型儲存設備的 Amazon EBS 磁碟區調整大小。如需以 Nitro 系統為基礎的執行個體清單，請參閱《*Amazon EC2 使用者指南*》中的 [Nitro型執行個體](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html#ec2-nitro-instances)。

   ```
   #!/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
   ```

1. 從 IDE 的終端機工作階段中，切換至包含 `resize.sh` 檔案的目錄。然後執行以下其中一個命令，將 `20` 取代為您要為 Amazon EBS 磁碟區調整的大小 (以 GiB 為單位)：
   + 

     ```
     bash resize.sh 20
     ```
   + 

     ```
     chmod +x resize.sh
     ./resize.sh 20
     ```