HA environment on AWS cloud using Terraform

Introduction

Terraform is a tool for building and managing infrastructure in an organized way. It can be used to manage a large variety of services offered by all the cloud solution providers.

In this tutorial you will deploy a secure and highly available environment with Terraform on AWS cloud  using single-tier architecture with public subnet. The servers and Elastic Load Balancer will span multiple availability zones to achieve high availability.

Prerequisites

  1. One Ubuntu 18.04 server with a sudo non-root user.
  2. An AWS account access (programmatic access) with sufficient privileges.

Procedure 

Step 1 – Installing terraform

Initially, download the appropriate package for your OS and architecture from the official terraform Downloads page and save it to the ~/Downloads directory.

$ curl https://releases.hashicorp.com/terraform/0.12.24/\
> terraform_0.12.24_linux_amd64.zip -o ~/Downloads/terraform.zip

Now, extract Terraform

$ unzip ~/Downloads/terraform.zip

In a terminal, run:

$ mv ~/Downloads/terraform /usr/local/bin/

To verify that you have installed Terraform correctly, let’s try and run it. In a terminal, run Terraform:

$ terraform

Step 2 – Configure AWS Credentials

The AWS provider offers a flexible means of providing credentials for authentication. Refer the document for more details. Here we are using the basic method. Create a file named main.tf and add the aws credentials in it.


Copy to Clipboard

Replace “my-access-key” and “my-secret-key” with your own access key and secret key.

Step 3 – Get to know and create the AWS resources

After configuring the AWS credentials, let’s create the terraform configuration files to launch each AWS resource. The main components are

  • VPC

A virtual private cloud is a virtual network dedicated to your AWS account. It is logically isolated from other virtual networks in the AWS Cloud

  • Internet Gateway

VPC component that allows communication between instances in your VPC and the Internet.

  • Subnet

subnet is a logical subdivision of an IP network.

  • Route Table

A route table contains a set of rules, called routes, that are used to determine where network traffic from your subnet or gateway is directed.

  • Security Group

A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic.

  • ELB

Elastic Load Balancing automatically distributes incoming application traffic across multiple targets.

  • Launch Configuration

A launch configuration is a template that an EC2 Auto Scaling group uses to launch EC2 instances

  • Autoscaling Group

AWS Auto Scaling lets you build scaling plans that automate how groups of different resources respond to changes in demand.

The terraform configuration file to launch a basic aws environment is provided below. Initially, Create a folder in your home directory then copy the configuration given below to a file named  main.tf.

Copy to Clipboard

.

Step 4 – Launch Infrastructure

When you create a new configuration, you need to initialize the directory with terraform init. Run:

$ terraform init

To validate your configuration, run the following. If your configuration is valid, Terraform will return a success message.

$ terraform validate

Now in the same directory, run the following. This will launch your complete infrastructure.

$ terraform apply

After terraform execution check AWS console for created resources.

Step 5 – Destroy Infrastructure

To destroy the entire infrastructure, use terraform command itself.

$ terraform destroy

Step 6 – Terraform Modules

A module is a container for multiple resources that are used together. Modules can be used to create lightweight abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of physical objects. With reusable terraform modules, terraform infrastructure management is easier. To know more about terraform modules, refer: https://www.terraform.io/docs/modules/index.html

Conclusion

In this tutorial you used Terraform to build a load-balanced web infrastructure on DigitalOcean. The example setup is simple, but demonstrates how easy it is to automate the deployment of servers. Terraform has many more features, and can work with other providers. Check out the official Terraform Documentation to learn more about how you can use Terraform to improve your own infrastructure.

References

  1. https://www.terraform.io/docs/enterprise/before-installing/reference-architecture/aws.html

 

About The Author

Ancy Paul

Cloud DevOps Engineer | CloudControl

Cloud DevOps Engineer with 3+ years of experience in cloud infrastructure management, supporting, automating, and optimizing deployments to hybrid cloud platforms using DevOps processes, CI/CD, containers and Kubernetes in both Production and Development environments.

About Cloud Control

Cloud Control simplifies cloud management with AppZ, DataZ, and ManageZ, optimizing operations, enhancing security, and accelerating time-to-market. We help businesses achieve cloud goals efficiently and reliably.

2024
GITEX
14-18 October

Dubai, UAE