This folder contains a Web Server Cluster example of a Terraform file Terraform.
This Terraform file deploys a cluster of web servers on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB.
The cluster of web servers returns "Hello, World" for the URL /
. The load balancer listens on port 80.
- You must have Terraform installed on your computer.
- You must have an AWS (Amazon Web Services) account.
- It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs.
- This code was written for Terraform 0.10.x.
- You must deploy the MySQL database in data-stores/mysql BEFORE deploying the templates in this folder.
- This uses a Terraform module. See modules/services/webserver-cluster.
-
Configure your AWS access keys.
-
Initialize working directory.
The first command that should be run after writing a new Terraform configuration is the
terraform init
command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times.terraform init
-
Configure Terraform backend.
Modify the S3 bucket name, which is defined in the
bucket
attribute inbackend.tf
file.bucket = "<YOUR_BUCKET_NAME>"
-
Configure the bucket used for the database's remote state storage.
Modify the S3 bucket name which is defined in the
bucket
attribute invars.tf
file. Important! You must deploy the templates in data-stores/mysql first:variable "db_remote_state_bucket" { description = "The name of the S3 bucket used for the database's remote state storage" default = "<YOUR_BUCKET_NAME>" }
-
Input variables than manage AMI and text the User Data script return.
There are two input variables defined in
vars.tf
file in Module in order to manage AMI and text the User Data script return:variable "ami" { description = "The AMI to run in the cluster" default = "ami-785db401" } variable "server_text" { description = "The text the web server should return" default = "Hello, World" }
This deploy configure both variables:
ami = "ami-785db401" server_text = "Hello, World"
-
Validate the changes.
Run command:
terraform plan
-
Deploy the changes.
Run command:
terraform apply
-
Test the cluster of web servers.
Test the cluster of web servers. When the
apply
command completes, it will output the DNS name of the load balancer.curl http://<elb_dns_name>/
-
Clean up the resources created.
When you have finished, run command:
terraform destroy