Deploy LAMP Stack On AWS EC2 Instance Using CloudFormation

In this guide , We will learn to deploy LAMP stack on AWS EC2 Instances using CloudFormation templates.

What Is AWS CloudFormation?

AWS CloudFormation is a service which helps us to setup AWS resources such as EC2 , RDS instances in a very less time So that we can focus more on applications.

We have to just create a template with the details of AWS resources such as EC2 , RDS , SNS etc .that needs to be launched to setup applications.

Once the template is created , We can import it to Cloudformation and AWS CloudFormation will take care of provisioning those resources , Configure them and map them if required.

AWS CloudFormation helps us to,

  • Quickly replicate the exiting Infrastructure.
  • Simplify infrastructure management.
  • Easily control and track changes to the infrastructure.

Core Concepts Of CloudFormation

In CloudFormation , We mostly work with these components.

  • Templates

Templates in cloudformation are written in JSON or YAML format.In this template , You can describe the resources with their properties that needs to be created.For example : EC2 Instance with t2.medium ,Then using this template the cloudformation will create those resources.

  • Stacks

A stack is a collection of AWS resources which we can manage as a single unit.All the resources in a stack are defined by cloudformation template.

  • Change Sets

If any changes required in the existing infrastructure , We can update the stack.Before making changes we can create a change set (a summary of proposed changes) and the change set will let us know how the changes will impact the existing resources.

Deploying LAMP Stack Using CloudFormation Template

In the absence of Cloudformation , What we do is , We will launch an EC2 instance and then install required packages such as Apache , MySQL Database and PHP. Which is actually a time consuming process.

With the help of cloudformation template , We can quickly launch the instances and install the required package and dependencies by using cfn helper scripts.

Using CFN helper scripts , Apache , MySQL , PHP will be installed and configured.

To deploy LAMP stack , Login to CloudFormation console.Click Create stack

In the Create Stack page , Choose Use a sample template

Here , Under Select a sample template , From the drop down , Choose LAMP Stack

Before proceeding further , Let us go ahead and see how the cloudformation template looks like.

Click View in Designer , You should see the following design for the LAMP stack created based on the template.

In the bottom , You can check the template for LAMP stack.

In the template , You can see the find the parameters to be passed for MySQL Database , key pair and SSH access.

Parameters are used to define custom values each time we create or update a stack.

In the below template , DBName , DBUser , DBPassword , DBRootPassword, InstanceType and SSH Location are defined as parameters , So that we can use the required values which creating the stack.

Within the template , For Resources type , The resources is EC2 Instance.

And we are installing the packages on the instance using Cloud Init scripts.

You can set a name for the template by clicking Edit icon.

Once done , Click the Create Stack icon.

Click Next , In Specify stack details page , Provide a name for the stack.

Under Parameters , provide the required values for the MySQL Database.

Choose the instance type , key Name and SSH Location.

Once all the required details are provided , Click Next and then click Next

Review the stack settings and then click Create stack

Under Events , You can check all the progress of the Stack.

Under Resources , You can check all the resources created by this stack.

Once the stack is successfully created , Go to Outputs section , You can find the link to access the Web page.

You will get the similar response.

Now head over to the EC2 console , You can see the EC2 instance created by the Stack with the Public IP Address.

You can SSH into the EC2 instance using the Key pair you have chosen while creating the stack.

After logging into the server , You can list the services running in the server using the below command.

netstat -nltp

You can see that PHP , MySQL and Apache are installed and running fine.

Also You should be able to login to MySQL database using the credentials that you have defined while creating the stack.

We have successfully deployed LAMP stack in a short period of time using the Cloudformation stack.

Terminating Cloudformation Stack

If you want to terminate the whole stack (LAMP stack) , Go to Cloudformation console , Choose the stack and then click Delete , On the confirmation page , Click Delete stack

It will delete the stack as well as the resources created by that stack.

The same way , We can write templates for the different infrastructure setup with the applications.

Thanks for reading.Hope you find it helpful.

Please do check out my other articles.