Cloud Formation Template - AWS Cloud Formation Template

 Cloud Formation Template - AWS Cloud Formation Template 

This Cloud Formation template creates a VPC with a CIDR block of 10.0.0.0/16, an Internet Gateway, and two subnets: a public subnet with a CIDR block of 10.0.1.0/24 and a private subnet with a CIDR block of 10.0.2.0/24. The public subnet is associated with a route table that directs all Internet traffic to the Internet Gateway, while the private subnet does not have a direct route to the Internet.

The template then creates a security group that allows incoming traffic on ports 80 and 443, and an EC2 instance that is launched in the private subnet using the specified AMI and instance type, and is associated with the security group. The user data for the instance includes a bash script that writes the instance's ID and private IP address to an HTML file and starts the Apache web server.


Cloud Formation



AWSTemplateFormatVersion: '2010-09-09'

Resources:

  VPC:

    Type: 'AWS::EC2::VPC'

    Properties:

      CidrBlock: 10.0.0.0/16

      InstanceTenancy: default

      EnableDnsSupport: true

      EnableDnsHostnames: true

  InternetGateway:

    Type: 'AWS::EC2::InternetGateway'

  VPCGatewayAttachment:

    Type: 'AWS::EC2::VPCGatewayAttachment'

    Properties:

      VpcId: !Ref VPC

      InternetGatewayId: !Ref InternetGateway

  PublicSubnet:

    Type: 'AWS::EC2::Subnet'

    Properties:

      VpcId: !Ref VPC

      CidrBlock: 10.0.1.0/24

      AvailabilityZone: !Select [0, !GetAZs '']

  PrivateSubnet:

    Type: 'AWS::EC2::Subnet'

    Properties:

      VpcId: !Ref VPC

      CidrBlock: 10.0.2.0/24

      AvailabilityZone: !Select [1, !GetAZs '']

  RouteTable:

    Type: 'AWS::EC2::RouteTable'

    Properties:

      VpcId: !Ref VPC

  PublicRoute:

    Type: 'AWS::EC2::Route'

    DependsOn: VPCGatewayAttachment

    Properties:

      RouteTableId: !Ref RouteTable

      DestinationCidrBlock: 0.0.0.0/0

      GatewayId: !Ref InternetGateway

  PublicSubnetRouteTableAssociation:

    Type: 'AWS::EC2::SubnetRouteTableAssociation'

    Properties:

      SubnetId: !Ref PublicSubnet

      RouteTableId: !Ref RouteTable

  SecurityGroup:

    Type: 'AWS::EC2::SecurityGroup'

    Properties:

      GroupDescription: Allow http and https traffic

      VpcId: !Ref VPC

  SecurityGroupIngress:

    Type: 'AWS::EC2::SecurityGroupIngress'

    Properties:

      GroupId: !Ref SecurityGroup

      IpProtocol: tcp

      FromPort: 80

      ToPort: 80

      CidrIp: 0.0.0.0/0

  SecurityGroupIngress2:

    Type: 'AWS::EC2::SecurityGroupIngress'

    Properties:

      GroupId: !Ref SecurityGroup

      IpProtocol: tcp

      FromPort: 443

      ToPort: 443

      CidrIp: 0.0.0.0/0

  EC2Instance:

    Type: 'AWS::EC2::Instance'

    Properties:

      ImageId: ami-0ff8a91507f77f867

      InstanceType: t2.micro

      SubnetId: !Ref PrivateSubnet

      SecurityGroupIds:

        - !Ref SecurityGroup

      UserData:

   LaunchTemplate:

        Type: 'AWS::EC2::LaunchTemplate'

        Properties:

          LaunchTemplateName: MyLaunchTemplate

          Version: '1'

          LaunchTemplateData:

            BlockDeviceMappings:

              - DeviceName: /dev/xvda

                Ebs:

                  VolumeSize: 8

                  VolumeType: gp2

            ImageId: ami-0ff8a91507f77f867

            InstanceType: t2.micro

            SecurityGroupIds:

              - !Ref SecurityGroup

            UserData:

              'Fn::Base64': !Sub |

                #!/bin/bash

                echo ${EC2Instance.InstanceId} > /var/www/html/index.html

                echo ${EC2Instance.PrivateIp} >> /var/www/html/index.html

                service apache2 start

                service apache2 status




FAQ :

1 .What is a CloudFormation template?

2. How do I create a template in CloudFormation?

3. What are the main parts of a CloudFormation template?

4. How does CloudFormation work?

5. How do I create a stack in CloudFormation?




Post a Comment

Previous Post Next Post

Contact Form