Home > Software design >  AWS cloudformation error: Deploy error with ImageID
AWS cloudformation error: Deploy error with ImageID

Time:11-23

I can't get the template to work. The event in cloudformation says that is because de ImageID but I'don't see what is wrong.

This is the template in YAML.

AWSTemplateFormatVersion: 2010-09-09
Description: Cafe application

Parameters:

  InstanceTypeParameter:
    Type: String
    Default: t2.small
    AllowedValues:
      - t2.micro
      - t2.small
      - t3.micro
      - t3.small
    Description: Default is t2.small. Choose t2.micro, t2.small, t3.small or t3.micro.

  LatestAmiId:
    Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
    Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'

  CafeNetworkParameter:
    Type: String
    Default: update-cafe-network

Mappings:

  RegionMap:
    us-east-1:
      "keypair": "vockey"
    us-west-2:
      "keypair": "cafe-oregon"

Resources:

  CafeSG:
      Type: 'AWS::EC2::SecurityGroup'
      Properties:
        GroupDescription: Enable SSH, HTTP access
        VpcId: !ImportValue
          'Fn::Sub': '${CafeNetworkParameter}-VpcID'
        Tags:
          - Key: Name
            Value: CafeSG
        SecurityGroupIngress:
          - IpProtocol: tcp
            FromPort: '80'
            ToPort: '80'
            CidrIp: 0.0.0.0/0
          - IpProtocol: tcp
            FromPort: '22'
            ToPort: '22'
            CidrIp: 0.0.0.0/0
  
  CafeInstance:
    Type: 'AWS::EC2::Instance'
    InstanceType: !Ref InstanceTypeParameter
    Properties:
      ImageID: !Ref LatestAmiId
      IamInstanceProfile: "CafeRole"
      KeyName: !FindInMap [RegionMap, !Ref "AWS::Region", keypair]
      NetworkInterfaces:
        - DeviceIndex: '0'
          AssociatePublicIpAddress: true
          SubnetId: !ImportValue
            'Fn::Sub': '${CafeNetworkParameter}-SubnetID'
          GroupSet:
            - !Ref CafeSG
      Tags:
      - Key: Name
        Value: Cafe Web Server

      UserData:
          Fn::Base64:
            !Sub |
              #!/bin/bash
              yum -y update
              yum install -y httpd mariadb-server wget
              amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
              systemctl enable httpd
              systemctl start httpd
              systemctl enable mariadb
              systemctl start mariadb
              wget https://aws-tc-largeobjects.s3.us-west-2.amazonaws.com/CUR-TF-200-ACACAD-2-16750/14-lab-mod10-challenge-CFn/s3/cafe-app.sh
              chmod  x cafe-app.sh
              ./cafe-app.sh

Outputs:
  WebServerPublicIP:
    Value: !GetAtt 'CafeInstance.PublicIp'

I'm learnign AWS for 2 months so try to explain a bit. Thanks

I tried changing the format of the template because and trying to "debug" with this command:

aws cloudformation validate-template --template-body file:///home/ec2-user/environment/CFTemplatesRepo/templates/cafe-app.yaml

CodePudding user response:

It should be ImageId, not ImageID. Also InstanceType: !Ref InstanceTypeParameter should be under Properties:

  CafeInstance:
    Type: 'AWS::EC2::Instance'
    
    Properties:
      ImageId: !Ref LatestAmiId
      IamInstanceProfile: "CafeRole"
      InstanceType: !Ref InstanceTypeParameter
      KeyName: !FindInMap [RegionMap, !Ref "AWS::Region", keypair]
      NetworkInterfaces:
        - DeviceIndex: '0'
          AssociatePublicIpAddress: true
          SubnetId: !ImportValue
            'Fn::Sub': '${CafeNetworkParameter}-SubnetID'
          GroupSet:
            - !Ref CafeSG
      Tags:
      - Key: Name
        Value: Cafe Web Server

      UserData:
          Fn::Base64:
            !Sub |
              #!/bin/bash
              yum -y update
              yum install -y httpd mariadb-server wget
              amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
              systemctl enable httpd
              systemctl start httpd
              systemctl enable mariadb
              systemctl start mariadb
              wget https://aws-tc-largeobjects.s3.us-west-2.amazonaws.com/CUR-TF-200-ACACAD-2-16750/14-lab-mod10-challenge-CFn/s3/cafe-app.sh
              chmod  x cafe-app.sh
              ./cafe-app.sh

  • Related