0

I am trying to autoScale this ec2 instance please guide me how to do it. Any template that might be helpful so that I can get started with the autoscaling. I am attaching only ec2 instance template which I want to autoScale.

---
AWSTemplateFormatVersion: 2010-09-09
Parameters:
  SourceStackName:
    Description: "Source stack name"
    Type: String
    AllowedPattern: "^[a-zA-Z][-a-zA-Z0-9]*$"
    Default: "shifa-vpc"
Resources:
  webserver:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: webserver-sg
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0
          Description: For traffic from Internet
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: 0.0.0.0/0
          Description: For traffic from Internet
      GroupDescription: Security Group for demo server
      VpcId:
        Fn::ImportValue:
          Fn::Sub: "${SourceStackName}-VpcID"
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      AvailabilityZone: us-east-1a
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            DeleteOnTermination: "true"
            VolumeSize: "8"
            VolumeType: gp2
      ImageId: ami-09d95fab7fff3776c # ami-0bdcc6c05dec346bf
      InstanceType: t2.micro
      #IamInstanceProfile: !Ref ListS3BucketsInstanceProfile
      #KeyName: ky-webserver
      NetworkInterfaces:
        - Description: Primary network interface
          DeviceIndex: 0
          SubnetId:
            Fn::ImportValue:
              Fn::Sub: "${SourceStackName}-PublicSubnet"
          GroupSet:
            - !Ref webserver
Outputs:
  ec2:
    Description: ec2
    Value: !Ref EC2Instance
    Export:
      Name:
        Fn::Sub: "${AWS::StackName}-server"

  sgGroupId:
    Description: ec2
    Value: !GetAtt webserver.GroupId
    Export:
      Name:
        Fn::Sub: "${AWS::StackName}-sgid"        

I am new to cloudformation and I am in training.

noob
  • 3
  • 1
  • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-autoscaling.html – jordanm Jul 06 '20 at 17:11
  • @jordanm I have seen the documentation having trouble references the ec2 instance properties. Can you please give a template of how I can autoscale this ec2 instance – noob Jul 06 '20 at 17:13

1 Answers1

0

Amazon have some examples for AutoScaling instances.

Importantly EC2 instance resources are not part of the autoscaling configuration within CloudFormation.

Instead you would use either a Launch Template or a Launch Configuration resource. The Launch Template is newer so preferably you should use this. These will define the instance configuration such as volumes, instance type etc.

The other component is the Autoscaling Group this will reference either one of previous components and define how the instance should scale.

If you're trying to scale an existing instance you will need to make an AMI from it first and the reference it.

AWS has an example template with autoscaling groups here.

Chris Williams
  • 32,215
  • 4
  • 30
  • 68