smi25

2EC2-ELB-Stack-Subnets

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