【问题标题】:Unable to delete additional ENI attached to EC2 instance through CloudFormation template无法通过 CloudFormation 模板删除附加到 EC2 实例的其他 ENI
【发布时间】:2020-10-09 12:54:49
【问题描述】:

我正在使用以下模板来配置资源:

      Resources:
        PrimaryADC:
          Type: 'AWS::EC2::Instance'
          Properties:
            ImageId:  "{{ ami_id }}"
            InstanceType:  "{{ instance_type }}"
            NetworkInterfaces:
              - AssociatePublicIpAddress: true
                DeleteOnTermination: true
                Description: "Mgmt interface"
                SubnetId:  "{{ mgmt_subnet_id }}"
                DeviceIndex: 0

        PrimaryADCClientNic:
          Type: 'AWS::EC2::NetworkInterface'
          Properties:
            Description: "Client interface"
            SubnetId: "{{ client_subnet_id }}"
          DependsOn: PrimaryADC
        PrimaryADCServerNic:
          Type: 'AWS::EC2::NetworkInterface'
          Properties:
            Description: "Server interface"
            SubnetId: "{{ server_subnet_id }}"
          DependsOn: PrimaryADC
        ClinetNetworkInterfaceAttachment:
          Type: AWS::EC2::NetworkInterfaceAttachment
          Properties:
              DeleteOnTermination: true
              InstanceId:
                Ref: PrimaryADC
              NetworkInterfaceId:
                Ref: PrimaryADCClientNic
              DeviceIndex: 1
        ServerNetworkInterfaceAttachment:
          Type: AWS::EC2::NetworkInterfaceAttachment
          Properties:
              DeleteOnTermination: true
              InstanceId:
                Ref: PrimaryADC
              NetworkInterfaceId:
                Ref: PrimaryADCServerNic
              DeviceIndex: 2
      Outputs:
        instanceid:
          Value: 
            Ref: PrimaryADC

当我删除堆栈时,它总是首先尝试删除额外的两个网络接口。这会引发错误,因为它们仍将附加到实例。我尝试为接口提供DependsOn,但这不起作用。在尝试删除任何附加的网络接口(客户端和服务器)之前,如何确保删除实例。

我正在使用 boto3 创建堆栈。并通过 GUI 删除。

堆栈部署的json文件:

{
   "Resources":{
      "PrimaryADC":{
         "Properties":{
            "InstanceType":"c5.xlarge",
            "NetworkInterfaces":[
               {
                  "AssociatePublicIpAddress":True,
                  "SubnetId":"**********",
                  "Description":"Mgmt interface",
                  "DeviceIndex":0,
                  "DeleteOnTermination":True
               }
            ],
            "ImageId":"*************"
         },
         "Type":"AWS::EC2::Instance"
      },
      "PrimaryADCServerNic":{
         "Properties":{
            "SubnetId":"******************",
            "Description":"Server interface"
         },
         "Type":"AWS::EC2::NetworkInterface",
         "DependsOn":"PrimaryADC"
      },
      "ServerNetworkInterfaceAttachment":{
         "Properties":{
            "InstanceId":{
               "Ref":"PrimaryADC"
            },
            "NetworkInterfaceId":{
               "Ref":"PrimaryADCServerNic"
            },
            "DeviceIndex":2,
            "DeleteOnTermination":True
         },
         "Type":"AWS::EC2::NetworkInterfaceAttachment"
      },
      "ClinetNetworkInterfaceAttachment":{
         "Properties":{
            "InstanceId":{
               "Ref":"PrimaryADC"
            },
            "NetworkInterfaceId":{
               "Ref":"PrimaryADCClientNic"
            },
            "DeviceIndex":1,
            "DeleteOnTermination":True
         },
         "Type":"AWS::EC2::NetworkInterfaceAttachment"
      },
      "PrimaryADCClientNic":{
         "Properties":{
            "SubnetId":"*************",
            "Description":"Client interface"
         },
         "Type":"AWS::EC2::NetworkInterface",
         "DependsOn":"PrimaryADC"
      }
   },
   "Outputs":{
      "instanceid":{
         "Value":{
            "Ref":"PrimaryADC"
         }
      }
   }
}

【问题讨论】:

    标签: amazon-web-services amazon-ec2 amazon-cloudformation


    【解决方案1】:

    我试图复制这个问题,但在我的测试中它都按预期工作。这是我使用的模板,因为您的模板不完整:

    Parameters:
    
      amiid:
        Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
        Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
    
      mgmtsubnetid:
        Type: AWS::EC2::Subnet::Id
    
      clientsubnetid:
        Type: AWS::EC2::Subnet::Id
    
      serversubnetid:
        Type: AWS::EC2::Subnet::Id    
    
    Resources:
    
      PrimaryADC:
        Type: 'AWS::EC2::Instance'
        Properties:
          ImageId:  !Ref amiid
          InstanceType:  t3.medium
          NetworkInterfaces:
            - AssociatePublicIpAddress: true
              DeleteOnTermination: true
              Description: "Mgmt interface"
              SubnetId:  !Ref mgmtsubnetid
              DeviceIndex: 0
    
      PrimaryADCClientNic:
        Type: 'AWS::EC2::NetworkInterface'
        Properties:
          Description: "Client interface"
          SubnetId: !Ref clientsubnetid
        DependsOn: PrimaryADC
    
      PrimaryADCServerNic:
        Type: 'AWS::EC2::NetworkInterface'
        Properties:
          Description: "Server interface"
          SubnetId: !Ref serversubnetid
        DependsOn: PrimaryADC
    
      ClinetNetworkInterfaceAttachment:
        Type: AWS::EC2::NetworkInterfaceAttachment
        Properties:
            DeleteOnTermination: true
            InstanceId:
              Ref: PrimaryADC
            NetworkInterfaceId:
              Ref: PrimaryADCClientNic
            DeviceIndex: 1
    
      ServerNetworkInterfaceAttachment:
        Type: AWS::EC2::NetworkInterfaceAttachment
        Properties:
            DeleteOnTermination: true
            InstanceId:
              Ref: PrimaryADC
            NetworkInterfaceId:
              Ref: PrimaryADCServerNic
            DeviceIndex: 2
    Outputs:
      instanceid:
        Value: 
          Ref: PrimaryADC
    

    【讨论】:

    • 好吧,这很奇怪。我会再试一次,看看是否能找到可重现的场景。谢谢。
    • 仍然面临同样的问题 :( 你能想到我们的环境之间可能会有所不同吗?我已经把从 cloudformation 控制台显示给我的 JSON 文件放在了问题中
    • @code_monk_666 您是否尝试过我在答案中提供的模板?它也不起作用?
    • 这就像一个魅力......我会尝试找出我的模板有什么问题。谢谢
    • @code_monk_666 很高兴它成功了。所以现在你可以比较两者,看看有什么问题。但是我没有改变你的模板,只是给它添加了参数。
    猜你喜欢
    • 2018-03-17
    • 2021-04-23
    • 1970-01-01
    • 2019-07-24
    • 2018-10-06
    • 2021-03-10
    • 2016-12-29
    • 2013-02-22
    • 1970-01-01
    相关资源
    最近更新 更多