AWS - CloudFormation
AWS - CloudFormation
https://s3.us-west-2.amazonaws.com/us-west-2-aws-training/awsu-spl/spl-
81/1.4.10.prod/scripts/wordpress.template
The template contains a definition of resources that will create a WordPress blog.
4. In the AWS Management Console, on the Services menu, click
CloudFormation.
DBName:
Default: wordpressdb
Description: The WordPress database name
Type: String
MinLength: '1'
MaxLength: '64'
AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
ConstraintDescription: must begin with a letter and contain only
alphanumeric
characters.
Notice that the default name for DBName is wordpressdb. Because the template
is using the default property for DBName, wordpressdb is the default text that
appears in the parameters section when launching the template. Also, notice that
the description for DBName appears just above the entry field.
Notice that all three parameters do not have a default property. This is why the
field is empty. However, all of these parameters have constraints (MinLength,
MaxLength, AllowedPattern). These constraints are also properties of the
parameters.
InstanceType:
Description: WebServer EC2 instance type
Type: String
Default: t2.micro
AllowedValues:
- t1.micro
- t2.nano
- t2.micro
ConstraintDescription: must be a valid EC2 instance type.
Notice that the InstanceType parameter has a default value of t2.micro but it
also allows you to select t1.micro and t2.nano instance types.
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the
instances
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
The Options page allows configuration of tags, roles, and advanced settings.
● Tags: Tags are arbitrary key-value pairs that can be used to identify your
stack for purposes such as cost allocation.
● Permissions: An existing AWS Identity and Access Management (IAM)
service role that AWS CloudFormation can assume.
● Stack policy: Defines the resources that you want to protect from
unintentional updates during a stack update. By default, all resources can
be updated during a stack update.
19. Click Stack creation options
● Rollback on failure: Specifies whether the stack should be rolled back if
stack creation fails. Typically, you want to accept the default value of Yes.
Select No if you want the stack's state retained even if creation fails, such
as when you are debugging a stack template.
● Timeout: Specifies the amount of time, in minutes, that CloudFormation
should allot before timing out stack creation operations. If CloudFormation
cannot create the entire stack in the time allotted, it fails the stack creation
due to timeout and rolls back the stack. By default, there is no timeout for
stack creation. However, individual resources may have their own timeouts
based on the nature of the service they implement. For example, if an
individual resource in your stack times out, stack creation also times out
even if the timeout you specified for stack creation has not yet been
reached.
● Enable termination protection: Prevents a stack from being accidently
deleted. If a user attempts to delete a stack with termination protection
enabled, the deletion fails and the stack--including its status--remains
unchanged.
If you do not see the Template tab, it might be due to your window width. If this
is the case, you should be able to widen your window so that the template
appears.
The Parameters tab, displays the parameters and parameter values that are part
of your template.
The Events tab displays each major event in the creation of the stack sorted by
the time of each event, with the latest events on top. You can see different
events and their status, such as CREATE_IN_PROGRESS or
CREATE_COMPLETE.
WebServerSecurityGroup:
DependsOn: AttachGateway
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable HTTP access via port 80 locked down to the load
balancer
+ SSH access
VpcId: !Ref 'VPC'
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: !Ref 'SSHLocation'
This code defines a security group, which permits HTTP traffic (port 80) from the
Internet (0.0.0.0/0) and SSH connections (port 22) from the IP range that was
specified as the SSHLocation in the parameters section.
WebServer:
Type: AWS::EC2::Instance
DependsOn: WebServerSecurityGroup
Metadata:
AWS::CloudFormation::Init:
install_wordpress:
packages:
yum:
php: []
php-mysql: []
mysql: []
mysql-server: []
mysql-devel: []
mysql-libs: []
httpd: []
This code defines an Amazon EC2 instance with steps to install WordPress. This
involves installing the PHP scripting language, a MySQL database and an HTTP
web server. The above code is truncated from the original.
Properties:
ImageId: !FindInMap
- AWSRegionArch2AMI
- !Ref 'AWS::Region'
- !FindInMap
- AWSInstanceType2Arch
- !Ref 'InstanceType'
- Arch
Notice that there is an ImageId property. Your template creates an Amazon EC2
instance from a predefined image.
The ImageId property provides the unique ID of the Amazon Machine Image
(AMI) that gets assigned during registration. Notice that the ImageId is located
by using a function (!FindInMap). The function looks up your ImageId using two
maps in the template AWSRegionArch2AMI and AWSInstanceType2Arch.
These maps are in the Mappings section of your template.
Mappings:
AWSInstanceType2Arch:
t1.micro:
Arch: PV64
t2.nano:
Arch: HVM64
t2.micro:
Arch: HVM64
31. What is the value if the instance type used is t2.micro?
It should be HVM64.
AWSRegionArch2AMI:
us-east-1:
PV64: ami-2a69aa47
HVM64: ami-6869aa05
HVMG2: ami-2e5e9c43
us-west-2:
PV64: ami-7f77b31f
HVM64: ami-7172b611
HVMG2: ami-83b770e3
It should be ami-7172b611.
34. In your text editor, locate the Outputs section of the template.
Outputs:
WebsiteURL:
Value: !Sub 'http://${WebServer.PublicDnsName}/wordpress'
Description: WordPress Website
EC2IPAddress:
Value: !GetAtt WebServer.PublicIp
The optional Outputs section declares output values that you can import into
other stacks (to create cross-stack references), return in response (to describe
stack calls), or view on the AWS CloudFormation console.
The Outputs code above provides a link to the WordPress website. It also
provides the public IP Address of the EC2 instance used.
When a stack has been deployed, it can display output information. This stack
provides a link to the WordPress website.
36. Copy the value for the WebsiteURL and EC2IPAddress to your text
editor.
37. Open a new web browser tab and enter the value of WebsiteURL,
which connects you to your new WordPress website.
You should be presented with the WordPress configuration page that looks
similar to this:
You can now create a blog post to add information to your website.
Because you also opened port 22 to your EC2 instance, you should also be able
to connect to your instance.
This allows you to keep the PuTTY session open for a longer period of time.
49. To the left of the instructions you are currently reading, click
Download PEM.
50. Save the file to the directory of your choice.
51. Copy this command to a text editor:
52. Replace KEYPAIR.pem with the path to the PEM file you
downloaded.
53. Replace EC2IPAddress with the IP address that you copied to your
text editor
54. In a Terminal, cd to the directory that you downloaded the .PEM file
to.
55. Paste the updated command into the Terminal window and run it.
56. Type when prompted to allow a first connection to this remote SSH
server.
Because you are using a key pair for authentication, you will not be prompted for
a password.
57. Check the processes that are running on the system by entering
58. Close your SSH session.
In this section, you will delete the stack. This will automatically delete all of the
resources created by the stack.
The status for the stack changes to DELETE_IN_PROGRESS. In the same way
you monitored the creation of the stack, you can monitor its deletion by using the
Events tab. When AWS CloudFormation completes the deletion of the stack, it
removes the stack from the list.