简体   繁体   中英

Unable to delete additional ENI attached to EC2 instance through CloudFormation template

I'm using the following template to provision resources:

      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

When I delete the Stack, it always tries to delete the extra two.network interfaces first. This throws an error because they would still be attached to the instance. I have tried giving DependsOn for the interfaces but this is not working. How can I ensure that the instance is deleted before trying to delete any of the.network interfaces attached (Client and Server).

I am using boto3 to create the stack. And deleting through GUI.

The json file from stack deployment:

{
   "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"
         }
      }
   }
}

I tried to replicate the issue, but it all works as expected in my tests. Here is the template I used, as yours is incomplete:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM