1
votes

Hi I am working on Cloud formation template. My goal is to create auto scalling group. Below is my template.

AWSTemplateFormatVersion: "2010-09-09"

Parameters:
  AvailabilityZone:
    Default: "ap-southeast-2a"
    Type: "String"


Resources:
  MySecuritygroupLB:
    Type: AWS::EC2::SecurityGroup
    Properties:
       GroupDescription: "My security Group"
       GroupName: "MySecuritygroupLB"
       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

  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: 'LoadBalancer'
      SecurityGroups:
        - !GetAtt MySecuritygroupLB.GroupId
      Subnets:
        - subnet-b0ada9f9
        - subnet-9c7c76fb


  DefaultTargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
       Name: 'DefaultTargetGroup'
       VpcId: 'vpc-d45072b3'
       Port: 80
       Protocol: HTTP
       TargetType: 'instance'
       Targets:
        - Id: !Ref FirstLinuxEC2instance

  LoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      LoadBalancerArn: !Ref LoadBalancer
      Port: 80
      Protocol: HTTP
      DefaultActions:
        - Type: forward
          TargetGroupArn: !Ref DefaultTargetGroup

  MyAutoScallingGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
       AutoScalingGroupName: MyAutoScallingGroup
       LaunchConfigurationName: !Ref LaunchConfig
       MaxSize: 3
       MinSize: 1
       TargetGroupARNs:
         - !Ref DefaultTargetGroup
       VPCZoneIdentifier:
         - subnet-b0ada9f9
         - subnet-9c7c76fb
    UpdatePolicy:
      AutoScalingRollingUpdate:
        MinInstancesInService: 1
        MaxBatchSize: 1
        PauseTime: PT15M
        WaitOnResourceSignals: True
    CreationPolicy:
      ResourceSignal:
        Timeout: PT15M
        Count: 1

  LaunchConfig:
    Type: AWS::AutoScaling::LaunchConfiguration
    Properties:
      KeyName: 'MyNewKeyPair'
      ImageId: 'ami-064fb5ae4f772d460'
      SecurityGroups:
        - !Ref MySecuritygroupLB
      InstanceType: 't2.micro'
      LaunchConfigurationName: LaunchConfig

  FirstLinuxEC2instance:
    Type: AWS::EC2::Instance
    Properties:
      AvailabilityZone: !Ref AvailabilityZone
      ImageId: 'ami-0c1d8842b9bfc767c'
      InstanceInitiatedShutdownBehavior: 'terminate'
      InstanceType: 't2.micro'
      SecurityGroupIds:
        - !Ref MySecuritygroupLB
      KeyName: 'MyNewKeyPair'
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash -xe
          sudo yum update -y
          sudo yum install httpd -y
          sudo chmod 777 /var/www/html/ -R
          sudo service httpd start
          cat <<'EOF' >> /var/www/html/index.html
          <!DOCTYPE html>
          <html>
          <head>
          <title>Garrett's Webpage</title>
          </head>
          <body>

          <h1>Welcome to Garrett's Sweet Webpage</h1>
          <p>This is a paragraph.</p>
          </body>
          </html>
          EOF

Above template is working fine If I remove launch configuration and auto scaling group so definitely problem with either launch config or auto scalling group. I created sample AMI and I am referencing the ID in template. I am getting below error

Received 0 SUCCESS signal(s) out of 1. Unable to satisfy 100% MinSuccessfulInstancesPercent requirement

Can someone help me to figure out the issue. Any help would be appreciated. Thanks

1
Are there any logs in CloudWatch or CloudTrail that help?Pat Myron
Thanks Pat Myron. I already have image in my ec2 dashboard. I am referencing that image by id.Niranjan

1 Answers

1
votes

You need to add this your ec2 userdata:

cfn-init -v --stack ${AWS::StackName} --resource AutoScalingGroup --region ${AWS::Region} cfn-signal -e 0 --stack ${AWS::StackName} --resource AutoScalingGroup --region ${AWS::Region}