Automation for the People Using Terraform!
Infrastructure as Code (IaC) is not only a powerful process for DevOps but a way of life!
Instead of depending on physical hardware configurations or interactive configuration tools,
We can instead use tools like Terraform to automatically:
- Provision an environment running on the Linux operating system
- Configure a web server running in the provisioned environment
This sample project aims to demonstrate provisioning resources on Amazon Web Services (AWS). Ensure you have the following before proceeding:
- AWS access and secret keys
- AWS key pair
AWS Free-Tier Usage
For this example, we'll be using resources which qualify under the AWS free-tier.
Warning! If you're not using an account that qualifies under the AWS free-tier, you may be charged to run this example. The most you should be charged is a few dollars, but I am not responsible for any charges that may be incurred.
The default Terraform plan will provision the following resources:
- VPC x 1
- Internet Gateway (IGW) x 1
- Subnet x 2
- Route x 1
- Security Group x 3
- EC2 x 2
- IAM Instance Profile x 1
- IAM Instance Role x 1
- ELB x 1
Furthermore, each EC2 instance will be provisioned with nginx as the HTTP server and a custom index page with the inspirational message: "Automation for the People!".
Execute the following steps to provision a basic two-tier architecture on AWS:
Download and install Terraform
Clone or fork this repository
terraform.templateand save it as
terraform.tfvarsin the project root
Save a copy of
launch.shin the project root
Open a terminal window in your local repository location and make the launch script executable:
$ chmod u+x launch.sh
Run the following command to examine the Terraform execution plan before deploying to AWS:
$ terraform plan
Review the proposed changes and make any modifications necessary
Repeat steps 6 and 7 until satisfied with the execution plan
Run the following command to execute the Terraform plan and deploy the infrastructure to AWS:
After the infrastructure is deployed, the public DNS of the load balancer will be displayed in the terminal:
$ Outputs: address = <resource_name>-<resource_id>.<aws_region>.elb.amazonaws.com
https://<elb-address>/ in your favorite web browser to see the new infrastructure in action.
NOTE The ELB DNS address will likely be available before all instances are registered so wait at least 10 minutes prior to visiting that address in a browser or reload the page after 10 minutes have passed.
Once you're finished with the sample infrastructure you should destroy it to avoid any unnecessary charges. From the project root, execute the following command:
$ terraform plan -destroy
which will allow you to review the changes prior to termination. Once you're satisfied, run:
$ terraform destroy
to confirm the termination and destroy your infrastructure resources.
This project is only a sample of what is possible using Terraform. For example, further enhancements could be made by provisioning Launch Configurations and Auto Scaling Groups to replace some of the variable interpolation currently present.