Deploying a LAMP stack on AWS (part 3): Deploy Infrastructure and Applications with Terraform

In the previous tutorial, three AMIs were built. This step will deploy those AMIs with Terraform.

Using Terraform to provision infrastructure

Terraform by HashiCorp is a tool for creating, combining, and modifying infrastructure. In the Atlas workflow Terraform reads from the artifact registry and provisions infrastructure. In this LAMP tutorial we use Terraform to deploy the three AMIs that were created by Packer in the previous step.

General setup

To finish this tutorial, you will need to:

  1. Clone this repository
  2. Create an Atlas account
  3. Generate an Atlas token and save as environment variable. export ATLAS_TOKEN=<your_token>
  4. In the Terraform files replace all instances of <username>, YOUR_SECRET_HERE, and YOUR_KEY_HERE with your Atlas username, Atlas token, and AWS keys. Using “find and replace” in your text editor is probably the fastest way to do this.

Terraform configuration overview

There are four key types in this Terraform configuration. The first is the aws provider, which authorizes Terraform to interact with resources on AWS. The second type is atlas_artifact, which is the reference to the AMI created in the previous step. Atlas artifacts are not limited to just AMIs, you can build and store Google Cloud images, OpenStack images, VMware images, and more. The aws_security_group resource type creates a security group on AWS. Finally, the aws_instance type creates instances on AWS. It’s important to note the AMI fields on the aws_instances: ami = "${atlas_artifact.php.metadata_full.region-us-east-1}". As you can see, the AMI id is taken from the atlas_artifact defined above. Terraform interpolates those values and calculates resource dependencies. For example, the artifacts and security group must be created before the instances, since the instances rely on those resources.

For more information on Terraform and its configurations, read through the Terraform documenation.

Step 1: Setup Terraform to save state in Atlas

Terraform stores the infrastructure state in order to calculate what changes need to be made on the next deploy. It’s possible to store that state locally, or you can store that state in Atlas so multiple operators can collaborate on the same infrastructure. To store state in Atlas: 1. Run terraform remote config -backend-config="name=ATLAS_USERNAME/example" in the terraform directory. Of course replace ATLAS_USERNAME with your personal username.

Step 2: Push the Terraform configuration to Atlas

Just as we pushed the Packer template to Atlas to be run remotely, we will push the Terraform configuration to Atlas. 1. Run terraform push -name="ATLAS_USERNAME/example" in the terraform directory. Of course replace ATLAS_USERNAME with your personal username. 1. This will create a Terraform plan in Atlas. To view the plan output, go to the Environments tab in your Atlas account and click on the newly created environment. Then click on the “changes” option in the left navigation to view the proposed changes.

Step 3: Deploy infrastructure

  1. If the plan looks good in Atlas, click “Confirm & Apply” to deploy the infrastructure!
  2. Navigate to the public IP of the PHP instance to see your application.
  3. Click on “Status” in the left navigation to view the real-time status of the newly deployed nodes

Review

In the Packer step of this tutorial, you built 3 AMIs. Those AMIs were then deployed with Terraform. A Consul agent was configured in the Packer phase on each AMI.The Consul agent reports the service running on each node, as well as the health of the node.

Atlas combines Packer, Terraform, and Consul to make the process of turning application code into a deployed application a auditable, repeatable process.

Next Steps

To learn more about Atlas, read the getting started guide or experiment with more community examples.