Jor-El
Jor-El

Reputation: 437

Cloud Formation template is getting invalid KeyName error while creating instance.How to fix that?

error template

When I am deploying the instance with security groups and keypair already created.

I am getting error as Encountered unsupported property Keyname. Even I changed the reference and set static value, but still same issue.

multiple times I tried but same issue. Even with static or reference value.

My template is shown below:

---
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
  SecurityGroupDescription:
    Description: Security Group Descrption
    Type: String
  SecurtyGroupPort:
    Description: Port group to be entered say with max and min values
    Type: Number     # optional
    MinValue: 1150
    MaxValue: 65535
  InstanceType:
    Description: ec2 instance Type
    Type: String     # optional
    Default: t2.small     # optional
    AllowedValues:
      - t1.micro
      - t2.nano
      - t2.micro
      - t2.small
    ConstraintDescription: must be a valid EC2 type allowed by your project
  DBPwd:
    NoEcho: true
    Description: database password
    Type: String
  KeyName:
    Description: Name of the keypair to connect instance
    Type: AWS::EC2::KeyPair::KeyName
    ConstraintDescription: must be a valid keypair name
  SecurityGroupIngressCIDR:
    Description: IP range to allow for ec2
    Type: String
    MinLength: '9'
    MaxLength: '18'
    Default: 0.0.0.0/0
    AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})
    ConstraintDescription: must be a valid CIDR range
  MyVPC:
    Description: vpc to create instance in
    Type: AWS::EC2::VPC::Id
  MySubnetIDs:
    Description: subnet ids list
    Type: "List<AWS::EC2::Subnet::Id>"
  DBSubnetIpBlocks:
    Description: "comma separated ip addresses CIDR blocks"
    Type: CommaDelimitedList
    Default: "172.31.120.0/20,172.31.160.0/20,172.31.210.0/20"
Resources:
  MyEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref InstanceType
      Keyname: !Ref KeyName
      ImageId: ami-0742b4e673072066f
      SubnetId: !Ref DbSubnet1
  MySecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: !Ref SecurityGroupDescription
      SecurityGroupIngress:
        - CidrIp: !Ref SecurityGroupIngressCIDR
          FromPort: !Ref SecurtyGroupPort
          ToPort: !Ref SecurtyGroupPort
          IpProtocol: tcp
      VpcId: !Ref MyVPC
  DbSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: !Select [0, !Ref DBSubnetIpBlocks]
  DbSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: !Select [1, !Ref DBSubnetIpBlocks]
  DbSubnet3:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: !Select [2, !Ref DBSubnetIpBlocks]

Upvotes: 0

Views: 140

Answers (1)

Mickael B.
Mickael B.

Reputation: 5205

It's KeyName and not Keyname (case sensitive) in your resource MyEC2Instance.

See the doc https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-keyname

Upvotes: 2

Related Questions