Using Terraform to Deploy EC2, VPC w/Public and Private Subnets, Aurora RDS Mysql instance and Load Balancer
In this publication I will be completing the following tasks:
- Deploy a VPC w/ CIDR 10.0.0.0/16 w/ 2 public subnets w/ CIDRs or 10.0.1.0/24 and 10.0.2.0/24.
- Create a private subnet w/ CIDR 10.0.3.0/24 w/ an RDS MySQL instance (nano).
- A Load Balancer that will direct traffic to the public subnet.
You will need the following to complete these tasks:
- AWS Account
- Terraform installed
- Fork my Github repo — https://github.com/Eric5825/Terraform.git
- After forking repo you will have access to my code with the following files being created; provider.tf, vpc.tf and variable.tf .
To start my project I first had to create a working directory to setup all the files mentioned above.
You can use any IDE of your choice in my case I am using Cloud9. The first file I will create inside of the directory called “Terraform”; provider.tf and it can be viewed below.
Above the Terraform Amazon Web Services (AWS) provider is a necessary file to interact with the many resources supported by AWS. You must configure the provider with the proper credentials before you can use it.
Next, I will create the file shown in the directory tree above called, variable.tf.
Variables in Terraform are used to define centrally controlled reusable values. The information in Terraform variables is saved independently from the deployment plans, which makes the values easy to read and edit from a single file. And there are two different types of variables input/output.
The last file I will create is the vpc.tf file which is holding the resource code. And it appears as follows below.
Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records. Resource Blocks documents the syntax for declaring resources.
Now that we have all the code created in individual files I will initiate the Terraform init command to start up terraform.
Next, we will run a terraform apply to see the IaC that will be constructed. You will notice that Terraform will not build anything until I provided my AWS credentials Username and Password.
The build was successful using Terraform IaC as shown with the terraform show command above. Below you will see the results of IaC as it has built my environment consisting of EC2, VPC, IGW, w/both Public and Private Subnets, as well as, an Aurora RDS instance and a (ALB) or Application Load Balancer.
Terraform is user friendly tool that will make your efforts of building IaC a little less cumbersome. So, do not be afraid to pick Terraform and play around it can be a lot of fun.