[AWS CloudFormation] CloudFormation에서 Stack 생성시 나타나는 Network interfaces and an instance-level subnet ID may not be specified on the same request 에러 해결

안녕하세요 클래스메소드 김재욱(Kim Jaewook) 입니다. 이번에는 CloudFormation에서 Stack 생성시 나타나는 Network interfaces and an instance-level subnet ID may not be specified on the same request 에러를 해결 하는 방법에 대해서 정리해 봤습니다.

에러 발생

Network interfaces and an instance-level subnet ID may not be specified on the same request

가끔 CloudFormation으로 Stack을 생성하면 다음과 같은 에러가 나타납니다.

AWS 콘솔화면에서 살펴보면, EC2 인스턴스 생성을 하자마자, 에러가 발생하고 바로 롤백 당하는 모습을 볼 수 있습니다.

에러 해결

MyLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: MyLaunchTemplate
LaunchTemplateData:
IamInstanceProfile:
Arn:
Fn::ImportValue: !Sub SSMInstanceProfile
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeType: gp2
VolumeSize: 20
DeleteOnTermination: true
Encrypted: true
DisableApiTermination: true
NetworkInterfaces:
- AssociatePublicIpAddress: true
DeviceIndex: 0
Groups:
- Fn::ImportValue: !Sub EC2SecurityGroup1
ImageId: !Ref LinuxLatestAmi
InstanceType: !Ref Ec2InstanceType
KeyName: !Ref KeyPairName

먼저 CloudFormation 코드를 확인해 보면, MyLaunchTemplate에 NetworkInterfaces가 설정되어 있는 것을 확인할 수 있습니다.

EC2Instance:
Type: AWS::EC2::Instance
Properties:
LaunchTemplate:
LaunchTemplateId: !Ref MyLaunchTemplate
Version: !GetAtt MyLaunchTemplate.LatestVersionNumber
DisableApiTermination: true
SubnetId: { "Fn::ImportValue": !Sub "PublicSubnetA" }

EC2 인스턴스를 생성하는 코드에는 NetworkInterfaces 없이 Subnet만 설정되어 있는 것을 확인할 수 있습니다.

여기서 문제의 원인은 NetworkInterfaces를 설정했기 때문에 Subnet 또 한 NetworkInterfaces에서 설정할 필요가 있지만, 해당 코드에서는 별개로 EC2 인스턴스에서 Subnet을 설정했기 때문에 에러가 발생한 것 입니다.

MyLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: MyLaunchTemplate
LaunchTemplateData:
IamInstanceProfile:
Arn:
Fn::ImportValue: !Sub SSMInstanceProfile
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeType: gp2
VolumeSize: 20
DeleteOnTermination: true
Encrypted: true
DisableApiTermination: true
NetworkInterfaces:
- AssociatePublicIpAddress: true
DeviceIndex: 0
Groups:
- Fn::ImportValue: !Sub EC2SecurityGroup1
SubnetId: { "Fn::ImportValue": !Sub "PublicSubnetA" }
ImageId: !Ref LinuxLatestAmi
InstanceType: !Ref Ec2InstanceType
KeyName: !Ref KeyPairName

다시 소스 코드를 수정해 보면, NetworkInterfaces에 Subnet을 설정하고, EC2 인스턴스에는 설정한 Subnet을 삭제하면 됩니다.

다시 Stack을 생성해 보면 아무 문제없이 Stack이 생성된 것을 확인할 수 있습니다.

클래스메소드코리아에 문의사항이 있으신 분들은

​info@classmethod.kr 로 연락 주시면 빠른 시일 내 담당자가 회신 드릴 수 있도록 하겠습니다 !

--

--