Infrastructure on AWS With Terraform

Pium Sudhara
6 min readJun 24, 2021

--

Build, change, and destroy infrastructure with Terraform

Image source parkmycloud.com

Hello forks, so in this article I’m going to talked about Infrastructure as Code(IaC), and IaC tools, how we are going to work with them. First let see what is this Infrastructure as Code or IaC.

What is Infrastructure as code?

In simple terms IaC or infrastructure as code is the managing the IT infrastructure using configuration files. The purpose of the infrastructure as code is to automatically manage, monitor, provision resources without manually configuration. The infrastructure as code similar to programming script. But it uses high-level or descriptive language to code. With IaC developers and operations teams automatically manage the tech stack for an application through software, helping to configure and deploy these components quickly and consistently.

Simple Architecture of IaC (Image source https://www.f5.com/)

How Infrastructure as Code Works ?

Image Source phoenixnap.com

According to the phoenixnap there are 3 simple steps how Infrastructure as Code works.

  • A developer defines the configuration parameters in a domain-specific language (DCL).
  • The instruction files are sent to a master server, a management API, or a code repository.
  • The IaC platform follows the developer’s instructions to create and configure the infrastructure.

Why ?

There are many advantages of Infrastructure as Code.

  • Simple code : Config infrastructure in very simple few lines. Can view and edit in any code editor. Human readable code in json or yaml so anyone can understand the what is deployed.
  • Reusability and scalability : Can use the same template and can reuse it in many systems. Upgrades are provisioned quickly, and with ease.
  • Speed and efficiency : Infrastructure as code enables you to quickly set up your complete infrastructure by running a script. No need spend same amount of time recreating it at each environment.
  • Consistency : IaC completely standardizes the setup of infrastructure so there is reduced possibility of any errors or deviations. This will decrease the chances of any incompatibility issues with your infrastructure and help your applications run more smoothly.
  • Lower Cost : IaC will help you too reduce your costs. You don’t need to spend your money on hardware. Also enables developers to spend less time on plumbing and more time developing innovative, mission-critical software solutions.

Infrastructure as Code Tools

Now let’s see what are famous infrastructure as code tools according to the nexastack.com.

  • Terraform : HashiCorp Terraform is the most popular open-source infrastructure as code software tool that enables you to safely and predictably create, change, and improve infrastructure. Terraform launched in 2014, written in Go language, now supports famous cloud providers like Amazon Web Services, Google Cloud Platform, Microsoft Azure, IBM Cloud, Oracle Cloud.
  • Ansible : Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. Launched in 2012, written in Python, PowerShell, Shell & Ruby.
  • AWS CloudFormation : AWS CloudFormation enables you to manage your complete infrastructure or AWS resources in a text file, or template. A collection of AWS resources is called a stack. AWS resources can be created or updated by using a stack.
  • Chef : Chef is an open-source configuration management tool that uses Ruby to develop essential building blocks like recipes and cookbooks. It is an automation tool that converts infrastructure to code.
  • Puppet : Puppet is an open source software configuration management and deployment tool. It’s most commonly used on Linux and Windows to pull the strings on multiple application servers at once.

Work With Terraform

So how we are going to work with Infrastructure as Code? Nowadays Terraform is the most popular open source IaC tool. With the help of Terraform Tutorials — HashiCorp Learn I’m going to show how we are going to work with Terraform and AWS in Windows operating system.

Prerequisites

  • First make sure you have an AWS account ;). If you don’t have a AWS account you can sign up through here. And you have download and install the AWS CLI. You can download the AWS CLI using this link.
  • You need to install Terraform in your machine. Either you can install it manually or you can install it through Chocolatey. I’ve installed Terraform using manually. Download the zip file and extract in new folder in C partition. Then add the environment variable path. Open the command prompt and type following command and if you got following view you have successfully installed Terraform on your machine
$ terraform

So Let’s get started.

`Step 1

  • First let’s create a new directory .
$ mkdir terraform-aws
$ cd terraform-aws
  • Open VScode and open the created directory. Then create new file named main.tf. Then paste the following code into main.tf.
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
}
}
}
provider "docker" {
host = "npipe:////.//pipe//docker_engine"
}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 8000
}
}

Then run the following command to initialize. And you will see the following output.

terraform init

Now we have being interact Terraform with docker. Now let’s Provision the Nginx server. Enter following command and type ‘Yes’.

terraform apply

Now we have successfully running container, you can check it in Docker desktop. Or you can visit localhost:8000 to check it.

We have provisioned Nginx server with Terraform. Our next step is to build the infrastructure. In next article let’s see how we are going to build it. I hope this article will help you to know about IaC basics. So let’s meet in next article. If you need any help follow me on GitHub, LinkedIn or Twitter.

Also check HashiCorp Learn Tutorial from this link. I wrote this article with help of those tutorials and with my enthusiasm and experience.

That’s it, thank you all..! don’t forget to leave a 👏✌️❤️

--

--

Pium Sudhara

Senior Cloud Operations Engineer at LSEG☁️💻 | AWS Community Builder | pium.karunasena@gmail.com