create EC2 instance with NetworkInterfaceId as Ref( trying to preserve ENI on AWS EC2 instances)

Viewed 12

I am trying to create a EC2 instance with NetworkInterfaceId as Ref using cloud formation template and getting below error

Instance AWS::EC2::Instance CREATE_FAILED Network interfaces and an instance-level subnet ID may not be specified on the same request (Service: AmazonEC2; Status Code: 400; Error Code: InvalidParameterCombination; Request ID: 51ad619b-d1cb-47dd-86c8-0f81ffd02f8f; Proxy: null)

here is the template I'm using.

Resources:
  Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: !Ref ImageId
      InstanceType: !Ref InstanceType
      KeyName: !Ref KeyName
      SecurityGroupIds:
      - Ref: SecurityGroups
      SubnetId:
        Ref: SubnetId
      NetworkInterfaces:
        - NetworkInterfaceId: !Ref NetworkInterface
          DeviceIndex: 0
          AssociatePublicIpAddress: 'false'

And also I tried removing SecurityGroupIds and SubnetId, like below

Resources:
  Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: !Ref ImageId
      InstanceType: !Ref InstanceType
      KeyName: !Ref KeyName
      NetworkInterfaces:
        - NetworkInterfaceId: 'eni-0ba240df5e1a859e2'
          DeviceIndex: 0
          AssociatePublicIpAddress: 'false'

Instance AWS::EC2::Instance CREATE_FAILED No default VPC for this user. GroupName is only supported for EC2-Classic and default VPC. (Service: AmazonEC2; Status Code: 400; Error Code: VPCIdNotSpecified; Request ID: c834e2ef-c09a-4560-9451-1ff6b7190180; Proxy: null)

Can someone help, what is missing here? Thanks in advance.

1 Answers

I found a way to implement this, using AWS::EC2::NetworkInterfaceAttachment.

    NetworkInterfaceAttachment:
      Type: AWS::EC2::NetworkInterfaceAttachment
      Properties:
        InstanceId:
          Ref: Instance
        NetworkInterfaceId: eni-0xxx
        DeviceIndex: 1     
Related