跳至內容

當 AWSEBSecurityGroup 無法刪除時,如何終止或重建 Elastic Beanstalk 環境?

3 分的閱讀內容
0

當我終止或重建 AWS Elastic Beanstalk 環境時,收到以下錯誤訊息: "Stack deletion failed: The following resource(s) failed to delete: [AWSEBSecurityGroup]." (「堆疊刪除失敗:以下資源刪除失敗:[AWSEBSecurityGroup]。」)

簡短描述

AWSEBSecurityGroup 是底層 AWS CloudFormation 堆疊透過 Elastic Beanstalk 建立的 Amazon Elastic Compute Cloud (Amazon EC2) 安全群組。如果在 Elastic Beanstalk 環境之外建立的彈性網路介面或安全群組使用 AWSEBSecurityGroup,則終止或重建環境時,該環境將會失敗。

**注意:**若要管理 Elastic Beanstalk 建立的資源,最佳做法是使用以下任一工具:Elastic Beanstalk 主控台Elastic Beanstalk Command Line Interface (EB CLI)AWS Command Line Interface (AWS CLI)。如果您在執行 AWS CLI 命令時收到錯誤,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本

解決方法

若要解決此問題,請完成下列其中一項任務:

  • 使用 AWS CloudFormation 主控台或 AWS CLI 刪除 Elastic Beanstalk 環境建立的 CloudFormation 堆疊。然後,終止或重建 Elastic Beanstalk 環境。
  • 從 Elastic Beanstalk 安全群組中移除相依物件。然後,終止或重建 Elastic Beanstalk 環境。

**注意:**在下列 AWS CLI 命令中,輸入堆疊名稱、環境名稱、AWS 區域和其他特定環境變數的值。

從 CloudFormation 主控台刪除堆疊

**重要:**如果您的 Elastic Beanstalk 環境具有整合的 Amazon Relational Database Service (Amazon RDS) 資料庫,請先分離 Amazon RDS DB 執行個體

您可以使用 CloudFormation 主控台或 AWS CLI 來刪除堆疊。

CloudFormation 主控台

  1. 開啟 CloudFormation console (CloudFormation 主控台)。
  2. Stacks (堆疊) 頁面上,選擇無法刪除的堆疊。
    注意:堆疊的狀態欄會顯示 DELETE_FAILED
  3. 選擇 Delete (刪除)。
  4. Delete Stack (刪除堆疊) 快顯視窗中,選擇 AWSEBSecurityGroup,然後選擇 Delete (刪除)。
    **重要:**請務必刪除安全群組。刪除堆疊時,系統不會自動刪除 AWSEBSecurityGroup。如果環境終止因其他相依資源而失敗,請先從 Delete Stack (刪除堆疊) 快顯視窗中選擇處於 DELETE_FAILED 狀態的對應資源。然後,選擇 Delete (刪除)。
  5. 終止重建 Elastic Beanstalk 環境。

AWS CLI

  1. 若要刪除 CloudFormation 堆疊,請執行 describe-network-interface-attribute 命令:

    aws cloudformation delete-stack --stack-name awseb-e-ztrauxujck-stack --retain-resources "AWSEBSecurityGroup" --region us-east-1
  2. 若要終止 Elastic Beanstalk 環境,請執行 terminate-environment 命令:

    aws elasticbeanstalk terminate-environment --environment-name my-env --region us-east-1
  3. 若要重建 Elastic Beanstalk 環境,請執行 rebuild-environment 命令:

    aws elasticbeanstalk rebuild-environment --environment-name my-env --region us-east-1

尋找並移除 Elastic Beanstalk 安全群組的相依物件

若要尋找不允許您刪除 AWSEBSecurityGroup 的相依網路介面或相依安全群組,請完成下列步驟。您可以使用 Amazon EC2 主控台或 AWS CLI。

Amazon EC2 主控台

  1. 從 Elastic Beanstalk 事件串流的錯誤訊息中,取得 AWSEBSecurityGroup 的安全群組 ID。
  2. 開啟 Amazon EC2 console (Amazon EC2 主控台)。
  3. 在導覽窗格中,選擇 Security Groups (安全群組)。然後,選擇 AWSEBSecurityGroup ID。
  4. 選擇 Actions (動作),然後選擇 Delete Security Group (刪除安全群組) 以尋找 AWSEBSecurityGroup 上的相依性。
  5. Delete Security Groups (刪除安全群組) 快顯視窗中,完成以下步驟:
    選擇 View your associated instance (查看關聯執行個體)。
    選擇 Actions (動作),然後選擇 Networking (網路)。
    選擇 Change Security Group (變更安全群組)。
    清除 AWSEBSecurityGroup IDAssign Security Groups 核取方塊。
  6. Delete Security Groups (刪除安全群組) 快顯視窗中,選擇 View your referencing security groups (查看參考的安全群組)。
  7. 若要移除 AWSEBSecurityGroup ID 的關聯,請選擇 Actions (動作),然後選擇 Edit inbound rules (編輯傳入規則) 或 Edit outbound rules (編輯傳出規則)。
  8. 終止重建 Elastic Beanstalk 環境。

AWS CLI

  1. 從 Elastic Beanstalk 事件串流的錯誤訊息中,取得 AWSEBSecurityGroup 的安全群組 ID。

  2. 在 .ebextensions 資料夾中,根據以下範例建立一個組態檔案:

    #!/bin/bash
    SecurityGroupID=$1
    Region=$2
    DependentENI=$(aws ec2 describe-network-interfaces --filters Name=group-id,Values=[$SecurityGroupID] --region $Region | jq '.NetworkInterfaces[].NetworkInterfaceId')
    DependentSGingress=$(aws ec2 describe-security-groups --filters Name=ip-permission.group-id,Values=[$SecurityGroupID] --region $Region | jq '.SecurityGroups[] .GroupId')
    DependentSGegress=$(aws ec2 describe-security-groups --filters Name=egress.ip-permission.group-id,Values=[$SecurityGroupID] --region $Region | jq '.SecurityGroups[] .GroupId')
    echo "The dependent ENI is $DependentENI"
    echo "The security group id with a dependency in ingress rule : $DependentSGingress"
    echo "The security group id with a dependency in egress rule :  $DependentSGegress"
  3. 將 bash 指令碼儲存為 sg_dependency.sh

  4. 從 jq 網站安裝 jq 公用程式

  5. 若要取得網路介面 ID 和安全群組 ID,請執行下列命令:

    ./sg_dependency.sh sg-111aaa22 us-east-1

    **注意:**將 sg-111aaa22 替換為您的 AWSEBSecurityGroup ID。將 us-east-1 替換為您的 AWS 區域。

  6. 若要尋找附加到網路介面 ID 的安全群組,請執行 describe-network-interface-attribute 命令:

    aws ec2 describe-network-interface-attribute --network-interface-id eni-1099d901 --attribute groupSet
  7. 若要分離 AWSEBSecurityGroup ID,請執行 modify-network-interface-attribute 命令:

    aws ec2 modify-network-interface-attribute --network-interface-id eni-1099d901 --groups sg-030644d6c95b6470a sg-d34a33a8 sg-059c081ab30c3e38e sg-0e4eabc747368e6c9

    **重要:**在上述命令中,不要包含 AWSEBSecurityGroup ID。加入其他安全群組 ID 以保持與網路介面的連線。如果您在建立網路介面的 AWS 服務中包含 AWSEBSecurityGroup ID,則命令輸出將傳回下列錯誤訊息: "An error occurred (AuthFailure) when calling the ModifyNetworkInterfaceAttribute operation: You don't have permission to access the specified resource". (「呼叫 ModifyNetworkInterfaceAttribute 作業時發生錯誤 (AuthFailure) :您無權存取指定的資源。」)

  8. 執行 describe-security-groups 命令,列出相依安全群組的所有入口和出口規則。

  9. 根據步驟 7 的輸出執行 revoke-security-group-ingressrevoke-security-group-egress 命令。
    **範例:**如果 AWSEBSecurityGroup ID 為 sg-111aaa22,且步驟 8 的輸出在入口規則中包含該安全群組 ID,則執行下列命令:

    aws ec2 revoke-security-group-ingress --group-id sg-0018a2fbdcd5797ea --ip-permissions '\[{"PrefixListIds":\[\],"FromPort":0,"IpRanges":\[\],"ToPort":65535,"IpProtocol":"tcp","UserIdGroupPairs":\[{"UserId":"123456789102","GroupId":"sg-111aaa22"}\],"Ipv6Ranges":\[\]}\]'

    **注意:**UserId 是 AWS 帳號 ID。

  10. 若要終止 Elastic Beanstalk 環境,請執行 terminate-environment 命令:

aws elasticbeanstalk terminate-environment --environment-name my-env --region us-east-1
  1. 若要重建 Elastic Beanstalk 環境,請執行 rebuild-environment 命令:
aws elasticbeanstalk rebuild-environment --environment-name my-env --region us-east-1

相關資訊

疑難排解

AWS 官方已更新 2 年前