navigation

Using Terraform to provision infrastructure on AWS Cloud

Overview

In this lab, we will learn how to deploy a highly available infrastructure on AWS using Terraform, a popular infrastructure as code tool.

Architecture

ConnectPrivate

In this architecture, you can see we have a VPC with CIDR 10.10.0.0/16, which has 2 public subnets with CIDR 10.10.1.0/24 and 10.10.2.0/24. Additionally, the VPC also has an Internet Gateway attached to it.

Before we dive into the deployment of our infrastructure, let’s take a moment to understand the concept of an Auto Scaling Group, Application Load Balancer and Amazon SNS to find out its role in ensuring the availability and scalability of our applications.

We only need to manually create Amazon Simple Notification Service (Amazon SNS) using either AWS Management Console or AWS CLI after deploying the architecture using an Infrastructure as Code tool.

What is an Application Load Balancer?

Elastic Load Balancing automatically distributes your incoming traffic across multiple targets, such as EC2 instances, containers, and IP addresses, in one or more Availability Zones.

It monitors the health of its registered targets, and routes traffic only to the healthy targets.

Elastic Load Balancing scales your load balancer as your incoming traffic changes over time.

Elastic Load Balancing supports the following load balancers:

  • Application Load Balancers
  • Network Load Balancers
  • Gateway Load Balancers
  • Classic Load Balancers

We are going to find out about Application Load Balancer.

Application Load Balancer components

ConnectPrivate

  • A load balancer serves as the single point of contact for clients. The load balancer distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones. This increases the availability of your application.

  • A listener checks for connection requests from clients, using the protocol and port that you configure.

  • Each target group routes requests to one or more registered targets, such as EC2 instances, using the protocol and port number that you specify.

What is an Auto Scaling Group?

  • Amazon EC2 Auto Scaling helps you ensure that you have the correct number of Amazon EC2 instances available to handle the load for your application.

  • You create collections of EC2 instances, called Auto Scaling groups.

  • With Auto Scaling Groups, you can specify the maximum/minumum of instances in each Auto Scaling Groups, and Amazon EC2 Auto Scaling ensures that your group never goes above/below this size.

ConnectPrivate

Auto Scaling Benefits

Adding Amazon EC2 Auto Scaling to your application architecture is one way to maximize the benefits of the AWS Cloud.

  • Fault tolerance: Amazon EC2 Auto Scaling can detect when an instance is unhealthy, terminate it, and launch an instance to replace it.

  • Availability: Amazon EC2 Auto Scaling helps ensure that your application always has the right amount of capacity to handle the current traffic demand.

  • Cost management: Amazon EC2 Auto Scaling can dynamically increase and decrease capacity as needed. Because you pay for the EC2 instances you use, you save money by launching instances when they are needed and terminating them when they aren’t.

So you can see that registering your Auto Scaling group with an Elastic Load Balancing load balancer helps you set up a load-balanced application.

-> It will help increase the scalability and availability of your application.

Amazon Simple Notification Service

Amazon Simple Notification Service (Amazon SNS) is a managed service that provides message delivery from publishers to subscribers (also known as producers and consumers).

With Amazon SNS, you can configure your Auto Scaling Group to notify important events that affect your application.

For example, if you configure your Auto Scaling group to use the autoscaling:EC2_INSTANCE_LAUNCH notification type, and your Auto Scaling group launchs an instance, it sends an email notification.

SNS Notifications

Amazon EC2 Auto Scaling supports sending Amazon SNS notifications when the following events occur.

Events Description
autoscaling:EC2_INSTANCE_LAUNCH Successful instance launch
autoscaling:EC2_INSTANCE_LAUNCH_ERROR Failed instance launch
autoscaling:EC2_INSTANCE_TERMINATE Successful instance termination
autoscaling:EC2_INSTANCE_TERMINATE_ERROR Failed instance termination

The message includes the following information:

  • Event — The event.

  • AccountId — The Amazon Web Services account ID.

  • AutoScalingGroupName — The name of the Auto Scaling group.

  • AutoScalingGroupARN — The ARN of the Auto Scaling group.

  • EC2InstanceId — The ID of the EC2 instance.

Content

  1. Introduction to Terraform
  2. Define Architecture
  3. Infrastructure as Code
  4. Deploy Infrastructure and configure SNS
  5. Conclusion
  6. Clean up Resources