Friday, April 6, 2018

How to provision EC2 using AWS CloudFormation templates in Ubuntu

AWS comes with CloudFormation template for creating resources in AWS using code. You can create any resources using CloudFormation. Let us try to create an EC2 using AWS CLI in Ubuntu.

pre-requistes:
AWS CLI needs to be installed. It can be installed by executing below command:
sudo apt install awscli

Steps:
Login to Ubuntu instance. create the below file with content
sudo vi create-jenkins-ec2.xml

AWSTemplateFormatVersion: 2010-09-09
Resources:
  MyJenkinsSG:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupDescription: Jenkins Security Group
      GroupName: Jenkins-security-group
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 8080
          ToPort: 8080
          CidrIp: 0.0.0.0/0
  MyEc2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      KeyName: myKey
      AvailabilityZone: us-east-2b
      ImageId: ami-916f59f4
      SecurityGroupIds:
        - !Ref MyJenkinsSG
      InstanceType: t2.micro
      Tags:
        - Key: MyEc2
          Value: JenkinsEC2

After saving the file,

You may have to give AWS access key, security keys and region code by running
aws configure
AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [None]:

Then execute the below command:

aws cloudformation create-stack --stack-name myjenkinsstack --template-body file:///create-jenkins-ec2.xml

Now check in AWS Mgmt console to see if EC2 is created.




1 comment:

  1. Thank you for your valuable content , Easy to understand and follow. As said, the migration to cloud is very essential for the protection of the database.

    Cloud Migration services
    Aws Cloud Migration services
    Azure Cloud Migration services
    Vmware Cloud Migration services
    Database Migration services

    ReplyDelete