Milind Yadav
Milind Yadav

Reputation: 19

Launch ec2 using cloudformation which should use launch template

I am trying to create a ec2 instance using the launch template : So I have created a launch template with below data.

LaunchTemplateVerybasic: img1

When I am trying to run a cloud formation template like below :

AWSTemplateFormatVersion: 2010-09-09
Resources:
  TestTemplate:
    Type: 'AWS::EC2::Instance'
    Properties:
      LaunchTemplate:
        LaunchTemplateSpecification:
          LaunchTemplateId: lt-00d9f13eea240e524
          LaunchTemplateName: Testtemplate
          Version: '1'

I get this error:

Encountered unsupported property LaunchTemplateSpecification, whereas in designer it shows that instance can be created.

What is that I am missing? I checked the documentation and this is a property supported by AWS::EC2::instance..

Let me know if there is something I am missing in understanding and in yaml

Upvotes: 2

Views: 6163

Answers (2)

crakama
crakama

Reputation: 825

Since the EC2 is not being launched from the launch template via auto-scaling group, rather its via a resource definition, you need first to remove the and have the config as follows

  HostA:
    Type: AWS::EC2::Instance
    Properties:
      LaunchTemplate:
        LaunchTemplateId: !Ref HostALaunchTemplate
        Version: !GetAtt HostALaunchTemplate.LatestVersionNumber

Launch Template example

When launching a launch template via auto-scaling group, usually there is no need to specify a network interface within the launch template because the auto-scaling group will take care of it.

Inside your launch template, remove the SecurityGroupIds at the LaunchTemplateData

  HostALaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateName: HostALaunchTemplate
      LaunchTemplateData:
        SecurityGroupIds:
          - !ImportValue MyASG

And add security group via network interface like so

  HostALaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateName: HostALaunchTemplate
        NetworkInterfaces:
          - DeviceIndex: 0
            Groups:
              - !ImportValue MyASG
            SubnetId: !ImportValue MySubnet

Upvotes: 2

Pat Myron
Pat Myron

Reputation: 4628

The CloudFormation Linter catches this with:

E3002 Invalid Property Resources/TestTemplate/Properties/LaunchTemplate/LaunchTemplateSpecification template.yaml:7:9

Try removing LaunchTemplateSpecification:

AWSTemplateFormatVersion: 2010-09-09
Resources:
  TestTemplate:
    Type: 'AWS::EC2::Instance'
    Properties:
      LaunchTemplate:
        LaunchTemplateId: lt-00d9f13eea240e524
        LaunchTemplateName: Testtemplate
        Version: '1'

AWS::EC2::Instance.LaunchTemplate documentation

Upvotes: 1

Related Questions