smi25

2EC2-ELB-Stack

Aug 7th, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 9.29 KB | None | 0 0
  1. AWSTemplateFormatVersion: 2010-09-09
  2. Description: Cloud Formation Demo Stack using two EC2 and ELB
  3. Metadata:
  4.   Version: v1.0
  5.   Comments: Created by Smita
  6.   'AWS::CloudFormation::Interface':
  7.     ParameterGroups:
  8.       - Label:
  9.           default: Network Configuration
  10.         Parameters:
  11.          - CidrBlock
  12.           - pubAvailabilityZone
  13.           - pubSubnetCIDR
  14.           - privAvailabilityZone
  15.           - privSubnetCIDR
  16.       - Label:
  17.           default: EC2 Instances Configuration
  18.         Parameters:
  19.          - InstanceName
  20.           - InstanceType
  21.           - Environment
  22. Parameters:
  23.   CidrBlock:
  24.     AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
  25.     Default: 10.10.12.0/24
  26.     Description: VPC CIDR Block (eg 10.0.0.0/16)
  27.     Type: String
  28.   pubAvailabilityZone:
  29.     Description: The AvailabilityZone to use for the first subnet
  30.     Type: 'AWS::EC2::AvailabilityZone::Name'
  31.   pubSubnetCIDR:
  32.     AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
  33.     Default: 10.10.12.0/25
  34.     Description: VPC CIDR Block for the Public Subnet (eg 10.0.0.0/24)
  35.     Type: String
  36.   privAvailabilityZone:
  37.     Description: The AvailabilityZone to use for the second subnet
  38.     Type: 'AWS::EC2::AvailabilityZone::Name'
  39.   privSubnetCIDR:
  40.     AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
  41.     Default: 10.10.12.128/25
  42.     Description: VPC CIDR Block for the Public Subnet (eg 10.0.0.0/24)
  43.     Type: String
  44.   InstanceName:
  45.     Type: String
  46.   InstanceType:
  47.     Description: EC2 instance type
  48.     Type: String
  49.     Default: t2.micro
  50.     AllowedValues:
  51.      - t2.micro
  52.     ConstraintDescription: must be a valid EC2 instance type.
  53.   KeyName:
  54.     Description: The EC2 Key Pair to allow SSH access to the instances
  55.     Type: 'AWS::EC2::KeyPair::KeyName'
  56.     Default: sptest
  57.     ConstraintDescription: must be the name of an existing EC2 KeyPair.
  58.   Environment:
  59.     Description: >-
  60.       Application environment for which this network is being created. e.g.
  61.       Development/Production.
  62.     Type: String
  63.     Default: UAT
  64.     AllowedValues:
  65.      - UAT
  66.       - DEV
  67.       - QA
  68.       - PROD
  69.   InstancePublicIP:
  70.     Description: >-
  71.       Specifies whether to launch instances with public IP addresses in your
  72.       VPC.
  73.     Type: String
  74.     Default: 'True'
  75.     AllowedValues:
  76.      - 'False'
  77.       - 'True'
  78.   InstanceAMI:
  79.     Description: AMI for use with the EC2 instances
  80.     Type: String
  81.     Default: ami-40142d25
  82.     AllowedValues:
  83.      - ami-40142d25
  84.       - ami-b63769a1
  85.       - ami-cdbdd7a2
  86. Mappings:
  87.   RegionMap:
  88.     us-east-2:
  89.       '64': ami-40142d25
  90.     us-east-1:
  91.       '64': ami-b63769a1
  92.     ap-south-1:
  93.       '64': ami-cdbdd7a2
  94. Resources:
  95.   myDemoVPC:
  96.     Type: 'AWS::EC2::VPC'
  97.     Properties:
  98.       CidrBlock:
  99.         Ref: CidrBlock
  100.       EnableDnsHostnames: true
  101.       EnableDnsSupport: true
  102.       Tags:
  103.         - Key: Name
  104.           Value:
  105.             Ref: 'AWS::StackName'
  106.     Metadata:
  107.       'AWS::CloudFormation::Designer':
  108.         id: aef40142-a5d5-40e8-ae2f-34f389c51a64
  109.   InternetGateway:
  110.     Type: 'AWS::EC2::InternetGateway'
  111.     Properties:
  112.       Tags:
  113.         - Key: Name
  114.           Value:
  115.             Ref: 'AWS::StackName'
  116.     Metadata:
  117.       'AWS::CloudFormation::Designer':
  118.         id: b0fed26f-086e-4ee5-bc74-52416d716ee9
  119.   GatewayAttachment:
  120.     Type: 'AWS::EC2::VPCGatewayAttachment'
  121.     Properties:
  122.       InternetGatewayId:
  123.         Ref: InternetGateway
  124.       VpcId:
  125.         Ref: myDemoVPC
  126.     Metadata:
  127.       'AWS::CloudFormation::Designer':
  128.         id: 957098a9-f3fa-4789-b80c-a6b0b4b12acc
  129.   rtb:
  130.     Type: 'AWS::EC2::RouteTable'
  131.     Properties:
  132.       Tags:
  133.         - Key: Name
  134.           Value:
  135.             Ref: 'AWS::StackName'
  136.       VpcId:
  137.         Ref: myDemoVPC
  138.     Metadata:
  139.       'AWS::CloudFormation::Designer':
  140.         id: 9399c066-851d-41ef-89db-b1ae7de2cb96
  141.   PublicRoute:
  142.     Type: 'AWS::EC2::Route'
  143.     Properties:
  144.       DestinationCidrBlock: 0.0.0.0/0
  145.       GatewayId:
  146.         Ref: InternetGateway
  147.       RouteTableId:
  148.         Ref: rtb
  149.     Metadata:
  150.       'AWS::CloudFormation::Designer':
  151.         id: 97d7eb40-faa9-4aaf-b2d1-d6c1e9faed27
  152.   pubSubnet:
  153.     Type: 'AWS::EC2::Subnet'
  154.     Properties:
  155.       AvailabilityZone:
  156.         Ref: pubAvailabilityZone
  157.       CidrBlock:
  158.         Ref: pubSubnetCIDR
  159.       MapPublicIpOnLaunch: true
  160.       Tags:
  161.         - Key: Name
  162.           Value:
  163.             'Fn::Join':
  164.              - '-'
  165.               - - Ref: 'AWS::StackName'
  166.                 - Ref: pubAvailabilityZone
  167.       VpcId: !Ref myDemoVPC
  168.     Metadata:
  169.       'AWS::CloudFormation::Designer':
  170.         id: 26978ca6-6372-487c-8a4f-ff21b1f9df41
  171.   privSubnet:
  172.     Type: 'AWS::EC2::Subnet'
  173.     Properties:
  174.       AvailabilityZone:
  175.         Ref: privAvailabilityZone
  176.       CidrBlock:
  177.         Ref: privSubnetCIDR
  178.       MapPublicIpOnLaunch: true
  179.       Tags:
  180.         - Key: Name
  181.           Value:
  182.             'Fn::Join':
  183.              - '-'
  184.               - - Ref: 'AWS::StackName'
  185.                 - Ref: privAvailabilityZone
  186.       VpcId: !Ref myDemoVPC
  187.     Metadata:
  188.       'AWS::CloudFormation::Designer':
  189.         id: 7b7ae4e5-5e7e-4fbf-979b-adee814cfe40
  190.   pubSubnetAssoc:
  191.     Type: 'AWS::EC2::SubnetRouteTableAssociation'
  192.     Properties:
  193.       RouteTableId:
  194.         Ref: rtb
  195.       SubnetId:
  196.         Ref: pubSubnet
  197.     Metadata:
  198.       'AWS::CloudFormation::Designer':
  199.         id: 37fe13d2-bde8-4d58-b609-b2dc39debc05
  200.   privSubnetAssoc:
  201.     Type: 'AWS::EC2::SubnetRouteTableAssociation'
  202.     Properties:
  203.       RouteTableId:
  204.         Ref: rtb
  205.       SubnetId:
  206.         Ref: privSubnet
  207.     Metadata:
  208.       'AWS::CloudFormation::Designer':
  209.         id: 7b1f07d3-6fd6-4733-89f7-83a9a4fa0a06
  210.   WebSecGrp:
  211.     Type: 'AWS::EC2::SecurityGroup'
  212.     Properties:
  213.       GroupDescription: 'Enable SSH & HTTPD access via port 22,443 & 80 respectively'
  214.       SecurityGroupIngress:
  215.         - IpProtocol: tcp
  216.           FromPort: '22'
  217.           ToPort: '22'
  218.           CidrIp: 0.0.0.0/0
  219.         - IpProtocol: tcp
  220.           FromPort: '443'
  221.           ToPort: '443'
  222.           CidrIp: 0.0.0.0/0
  223.         - IpProtocol: tcp
  224.           FromPort: '80'
  225.           ToPort: '80'
  226.           CidrIp: 0.0.0.0/0
  227.       VpcId: !Ref myDemoVPC
  228.     Metadata:
  229.       'AWS::CloudFormation::Designer':
  230.         id: 33f55c22-f11b-472e-9ee9-61f67cfb05ef
  231.  
  232.   ElasticLoadBalancer:
  233.     Type: AWS::ElasticLoadBalancing::LoadBalancer
  234.     Properties:
  235.       AvailabilityZones:
  236.       - 'us-east-2a'
  237.       Instances: [!Ref 'webServer1', !Ref 'webServer2']  
  238.       Listeners:
  239.       - LoadBalancerPort: '80'
  240.         InstancePort: '80'
  241.         Protocol: HTTP
  242.       HealthCheck:
  243.         Target: HTTP:80/
  244.         HealthyThreshold: '3'
  245.         UnhealthyThreshold: '5'
  246.         Interval: '30'
  247.         Timeout: '5'
  248.  
  249.   webServer1:
  250.     Type: 'AWS::EC2::Instance'
  251.     Properties:
  252.       KeyName: !Ref KeyName
  253.       ImageId: !FindInMap
  254.         - RegionMap
  255.         - !Ref 'AWS::Region'
  256.         - 64
  257.       InstanceType: !Ref InstanceType
  258.       SubnetId: !Ref pubSubnet
  259.       SecurityGroupIds:
  260.        - !Ref WebSecGrp
  261.       UserData:
  262.         'Fn::Base64': !Sub >
  263.          #!/bin/bash -x
  264.  
  265.           sudo yum install httpd -y
  266.  
  267.           sudo service httpd start
  268.  
  269.           groupadd www
  270.  
  271.           usermod -a -G www ec2-user
  272.  
  273.           cd /var/www/
  274.  
  275.           # Set the permissions
  276.  
  277.           chown -R root:www /var/www
  278.  
  279.           chmod 2775 /var/www
  280.  
  281.           find /var/www -type d -exec chmod 2775 {} +
  282.  
  283.           find /var/www -type f -exec chmod 0664 {} +
  284.  
  285.           echo "<h1> Welcome to EC2 - First Instance</h1>" >>
  286.           /var/www/html/index.html
  287.  
  288.           echo "<h1> Error occurred in First Instance </h1>" >>
  289.           /var/www/html/error.html
  290.  
  291.           sudo chkconfig httpd on
  292.  
  293.           sudo service httpd restart
  294.     Metadata:
  295.       'AWS::CloudFormation::Designer':
  296.         id: c16510dc-49ed-42bf-8482-4984ed1321da
  297.  
  298.   webServer2:
  299.     Type: 'AWS::EC2::Instance'
  300.     Properties:
  301.       KeyName: !Ref KeyName
  302.       ImageId: !FindInMap
  303.         - RegionMap
  304.         - !Ref 'AWS::Region'
  305.         - 64
  306.       InstanceType: !Ref InstanceType
  307.       SubnetId: !Ref pubSubnet
  308.       SecurityGroupIds:
  309.        - !Ref WebSecGrp
  310.       UserData:
  311.         'Fn::Base64': !Sub >
  312.          #!/bin/bash -x
  313.  
  314.           sudo yum install httpd -y
  315.  
  316.           sudo service httpd start
  317.  
  318.           groupadd www
  319.  
  320.           usermod -a -G www ec2-user
  321.  
  322.           cd /var/www/
  323.  
  324.           # Set the permissions
  325.  
  326.           chown -R root:www /var/www
  327.  
  328.           chmod 2775 /var/www
  329.  
  330.           find /var/www -type d -exec chmod 2775 {} +
  331.  
  332.           find /var/www -type f -exec chmod 0664 {} +
  333.  
  334.           echo "<h1> Welcome to EC2 - Second Instance</h1>" >>
  335.           /var/www/html/index.html
  336.  
  337.           echo "<h1> Error occurred in Second Instance</h1>" >>
  338.           /var/www/html/error.html
  339.  
  340.           sudo chkconfig httpd on
  341.  
  342.           sudo service httpd restart
  343.     Metadata:
  344.       'AWS::CloudFormation::Designer':
  345.         id: c16510dc-49ed-42bf-8482-4984ed1321da
  346.  
  347. Outputs:
  348.   WebServerPublicIPAddress1:
  349.     Description: The public IP address of the EC2 Instance-First.
  350.     Value: !GetAtt webServer1.PublicDnsName
  351.   WebServerPublicIPAddress2:
  352.     Description: The public IP address of the EC2 Instance-Second.
  353.     Value: !GetAtt webServer2.PublicDnsName
Add Comment
Please, Sign In to add comment