Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AWSTemplateFormatVersion: 2010-09-09
- Description: Cloud Formation Demo Stack using two EC2 and ELB
- Metadata:
- Version: v1.0
- Comments: Created by Smita
- 'AWS::CloudFormation::Interface':
- ParameterGroups:
- - Label:
- default: Network Configuration
- Parameters:
- - CidrBlock
- - pubAvailabilityZone
- - pubSubnetCIDR
- - privAvailabilityZone
- - privSubnetCIDR
- - Label:
- default: EC2 Instances Configuration
- Parameters:
- - InstanceName
- - InstanceType
- - Environment
- Parameters:
- CidrBlock:
- AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
- Default: 10.10.12.0/24
- Description: VPC CIDR Block (eg 10.0.0.0/16)
- Type: String
- pubAvailabilityZone:
- Description: The AvailabilityZone to use for the first subnet
- Type: 'AWS::EC2::AvailabilityZone::Name'
- pubSubnetCIDR:
- AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
- Default: 10.10.12.0/25
- Description: VPC CIDR Block for the Public Subnet (eg 10.0.0.0/24)
- Type: String
- privAvailabilityZone:
- Description: The AvailabilityZone to use for the second subnet
- Type: 'AWS::EC2::AvailabilityZone::Name'
- privSubnetCIDR:
- AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
- Default: 10.10.12.128/25
- Description: VPC CIDR Block for the Public Subnet (eg 10.0.0.0/24)
- Type: String
- InstanceName:
- Type: String
- InstanceType:
- Description: EC2 instance type
- Type: String
- Default: t2.micro
- AllowedValues:
- - t2.micro
- ConstraintDescription: must be a valid EC2 instance type.
- KeyName:
- Description: The EC2 Key Pair to allow SSH access to the instances
- Type: 'AWS::EC2::KeyPair::KeyName'
- Default: sptest
- ConstraintDescription: must be the name of an existing EC2 KeyPair.
- Environment:
- Description: >-
- Application environment for which this network is being created. e.g.
- Development/Production.
- Type: String
- Default: UAT
- AllowedValues:
- - UAT
- - DEV
- - QA
- - PROD
- InstancePublicIP:
- Description: >-
- Specifies whether to launch instances with public IP addresses in your
- VPC.
- Type: String
- Default: 'True'
- AllowedValues:
- - 'False'
- - 'True'
- InstanceAMI:
- Description: AMI for use with the EC2 instances
- Type: String
- Default: ami-40142d25
- AllowedValues:
- - ami-40142d25
- - ami-b63769a1
- - ami-cdbdd7a2
- Mappings:
- RegionMap:
- us-east-2:
- '64': ami-40142d25
- us-east-1:
- '64': ami-b63769a1
- ap-south-1:
- '64': ami-cdbdd7a2
- Resources:
- myDemoVPC:
- Type: 'AWS::EC2::VPC'
- Properties:
- CidrBlock:
- Ref: CidrBlock
- EnableDnsHostnames: true
- EnableDnsSupport: true
- Tags:
- - Key: Name
- Value:
- Ref: 'AWS::StackName'
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: aef40142-a5d5-40e8-ae2f-34f389c51a64
- InternetGateway:
- Type: 'AWS::EC2::InternetGateway'
- Properties:
- Tags:
- - Key: Name
- Value:
- Ref: 'AWS::StackName'
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: b0fed26f-086e-4ee5-bc74-52416d716ee9
- GatewayAttachment:
- Type: 'AWS::EC2::VPCGatewayAttachment'
- Properties:
- InternetGatewayId:
- Ref: InternetGateway
- VpcId:
- Ref: myDemoVPC
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: 957098a9-f3fa-4789-b80c-a6b0b4b12acc
- rtb:
- Type: 'AWS::EC2::RouteTable'
- Properties:
- Tags:
- - Key: Name
- Value:
- Ref: 'AWS::StackName'
- VpcId:
- Ref: myDemoVPC
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: 9399c066-851d-41ef-89db-b1ae7de2cb96
- PublicRoute:
- Type: 'AWS::EC2::Route'
- Properties:
- DestinationCidrBlock: 0.0.0.0/0
- GatewayId:
- Ref: InternetGateway
- RouteTableId:
- Ref: rtb
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: 97d7eb40-faa9-4aaf-b2d1-d6c1e9faed27
- pubSubnet:
- Type: 'AWS::EC2::Subnet'
- Properties:
- AvailabilityZone:
- Ref: pubAvailabilityZone
- CidrBlock:
- Ref: pubSubnetCIDR
- MapPublicIpOnLaunch: true
- Tags:
- - Key: Name
- Value:
- 'Fn::Join':
- - '-'
- - - Ref: 'AWS::StackName'
- - Ref: pubAvailabilityZone
- VpcId: !Ref myDemoVPC
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: 26978ca6-6372-487c-8a4f-ff21b1f9df41
- privSubnet:
- Type: 'AWS::EC2::Subnet'
- Properties:
- AvailabilityZone:
- Ref: privAvailabilityZone
- CidrBlock:
- Ref: privSubnetCIDR
- MapPublicIpOnLaunch: true
- Tags:
- - Key: Name
- Value:
- 'Fn::Join':
- - '-'
- - - Ref: 'AWS::StackName'
- - Ref: privAvailabilityZone
- VpcId: !Ref myDemoVPC
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: 7b7ae4e5-5e7e-4fbf-979b-adee814cfe40
- pubSubnetAssoc:
- Type: 'AWS::EC2::SubnetRouteTableAssociation'
- Properties:
- RouteTableId:
- Ref: rtb
- SubnetId:
- Ref: pubSubnet
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: 37fe13d2-bde8-4d58-b609-b2dc39debc05
- privSubnetAssoc:
- Type: 'AWS::EC2::SubnetRouteTableAssociation'
- Properties:
- RouteTableId:
- Ref: rtb
- SubnetId:
- Ref: privSubnet
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: 7b1f07d3-6fd6-4733-89f7-83a9a4fa0a06
- WebSecGrp:
- Type: 'AWS::EC2::SecurityGroup'
- Properties:
- GroupDescription: 'Enable SSH & HTTPD access via port 22,443 & 80 respectively'
- SecurityGroupIngress:
- - IpProtocol: tcp
- FromPort: '22'
- ToPort: '22'
- CidrIp: 0.0.0.0/0
- - IpProtocol: tcp
- FromPort: '443'
- ToPort: '443'
- CidrIp: 0.0.0.0/0
- - IpProtocol: tcp
- FromPort: '80'
- ToPort: '80'
- CidrIp: 0.0.0.0/0
- VpcId: !Ref myDemoVPC
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: 33f55c22-f11b-472e-9ee9-61f67cfb05ef
- ElasticLoadBalancer:
- Type: AWS::ElasticLoadBalancing::LoadBalancer
- Properties:
- AvailabilityZones:
- - 'us-east-2a'
- Instances: [!Ref 'webServer1', !Ref 'webServer2']
- Listeners:
- - LoadBalancerPort: '80'
- InstancePort: '80'
- Protocol: HTTP
- HealthCheck:
- Target: HTTP:80/
- HealthyThreshold: '3'
- UnhealthyThreshold: '5'
- Interval: '30'
- Timeout: '5'
- webServer1:
- Type: 'AWS::EC2::Instance'
- Properties:
- KeyName: !Ref KeyName
- ImageId: !FindInMap
- - RegionMap
- - !Ref 'AWS::Region'
- - 64
- InstanceType: !Ref InstanceType
- SubnetId: !Ref pubSubnet
- SecurityGroupIds:
- - !Ref WebSecGrp
- UserData:
- 'Fn::Base64': !Sub >
- #!/bin/bash -x
- sudo yum install httpd -y
- sudo service httpd start
- groupadd www
- usermod -a -G www ec2-user
- cd /var/www/
- # Set the permissions
- chown -R root:www /var/www
- chmod 2775 /var/www
- find /var/www -type d -exec chmod 2775 {} +
- find /var/www -type f -exec chmod 0664 {} +
- echo "<h1> Welcome to EC2 - First Instance</h1>" >>
- /var/www/html/index.html
- echo "<h1> Error occurred in First Instance </h1>" >>
- /var/www/html/error.html
- sudo chkconfig httpd on
- sudo service httpd restart
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: c16510dc-49ed-42bf-8482-4984ed1321da
- webServer2:
- Type: 'AWS::EC2::Instance'
- Properties:
- KeyName: !Ref KeyName
- ImageId: !FindInMap
- - RegionMap
- - !Ref 'AWS::Region'
- - 64
- InstanceType: !Ref InstanceType
- SubnetId: !Ref pubSubnet
- SecurityGroupIds:
- - !Ref WebSecGrp
- UserData:
- 'Fn::Base64': !Sub >
- #!/bin/bash -x
- sudo yum install httpd -y
- sudo service httpd start
- groupadd www
- usermod -a -G www ec2-user
- cd /var/www/
- # Set the permissions
- chown -R root:www /var/www
- chmod 2775 /var/www
- find /var/www -type d -exec chmod 2775 {} +
- find /var/www -type f -exec chmod 0664 {} +
- echo "<h1> Welcome to EC2 - Second Instance</h1>" >>
- /var/www/html/index.html
- echo "<h1> Error occurred in Second Instance</h1>" >>
- /var/www/html/error.html
- sudo chkconfig httpd on
- sudo service httpd restart
- Metadata:
- 'AWS::CloudFormation::Designer':
- id: c16510dc-49ed-42bf-8482-4984ed1321da
- Outputs:
- WebServerPublicIPAddress1:
- Description: The public IP address of the EC2 Instance-First.
- Value: !GetAtt webServer1.PublicDnsName
- WebServerPublicIPAddress2:
- Description: The public IP address of the EC2 Instance-Second.
- Value: !GetAtt webServer2.PublicDnsName
Add Comment
Please, Sign In to add comment