ðïļ Getting Started with Terraform
âïļ Introduction
During the last 10 years, cloud providers have become the de facto standard in the software development world because almost every modern application needs:
Scalability
Cost Effisiency
High Availability and Reliability
Security
These can be achieved in AWS, GCP, or Azure relatively easily. While these cloud providers offer comprehensive CLI and UI tools to manage infrastructure, it's still challenging to use them because there is no way to historically track changes and quickly roll back to previous versions.
In this post, I will highlight the concept of Infrastructure as Code and how to get started with Terraform.
ð Infrastructure As Code
An approach to managing infrastructure through Cloud Provider UIs is called "ClickOps" because all infrastructure is managed through clicks in the UI. This approach has many disadvantages:
Lack of Version Control.
Error-Prone and Inconsistent.
Limited Automation and Efficiency.
The solution is the Infrastructure as Code (IaC) concept, which proposes declaring infrastructure in code, similar to how we develop applications in Java, Go, or Python. IaC allows us to code our infrastructure, add it to a Git repository, and gain all the benefits of code: code review, rollback, and historical tracking of changes. There are many IaC tools today:
Puppet
Terraform
AWS CloudFormation
Ansible
and others...
One of the best IaC tools is Terraform.
ð Terraform
Terraform is an open-source product from HashiCorp and can be downloaded from the website: terraform.io.
The main features of Terraform are:
Infrastructure as Code: Define infrastructure using declarative configuration files.
Provisioning: Supports multiple providers like AWS, Azure, and Google Cloud.
State Management: Maintain a state of deployments locally or in remote state files.
Modules: Reusable configurations to simplify complex infrastructure.
Plan and Apply: Preview changes before applying them.
These features make Terraform a widely adopted IaC tool in many companies, so knowledge of it is highly valuable in the current market.
Let's see an example of simple provisioning of an AWS EC2 host with Terraform.
ðĪŠ AWS EC2 host
Here is a super simple example of EC2 host provisioning with a "Hello, World" HTTP web server:
1
2provider "aws" {
3 region = "us-east-2"
4}
5
6resource "aws_instance" "webserver" {
7 ami = "ami-0fb653ca2d3203ac1"
8 instance_type = "t2.micro"
9 user_data_replace_on_change = true
10 vpc_security_group_ids = [aws_security_group.webserver.id]
11
12 user_data = <<-EOF
13 #!/bin/bash
14 echo "Hello, World" > index.html
15 nohup busybox httpd -f -p ${var.server_port} &
16 EOF
17
18 tags = {
19 "Name" = "webserver"
20 }
21}
22
23resource "aws_security_group" "webserver" {
24 name = "terraform-webserver"
25 ingress {
26 from_port = 8080
27 to_port = 8080
28 protocol = "tcp"
29 cidr_blocks = ["0.0.0.0/0"]
30 }
31}
32
33variable "server_port" {
34 type = number
35 description = "The port the server will use for HTTP requests"
36}
37
38output "ip_address" {
39 value = aws_instance.webserver.public_ip
40 description = "The public IP address of the web server"
41}
To run this example need just copy-paste the code and:
Run:
terraform init
.Run:
terraform apply
.Specify server port and wait for the public ip address output.
Don't forget to destroy resources after testing them ð.
ð Resources to Learn Terraform
I learned Terraform from the Terraform documentation, Udemy courses, and books. Here is a list of useful resources I recommend:
Book: Terraform: Up and Running: Writing Infrastructure as Code - highly recommended.
Docs: Terraform documentation.
ð Conclusions
I'm actively using Terraform at work to manage infrastructure on AWS, and I can say that it's the best tool for the job. Here are the benefits of Terraform listed again:
Infrastructure as Code (IaC): Version control of infrastructure and all other benefits from code using.
Multi-Cloud Support: Flexibility to use different providers (AWS, Azure, GCP, etc.).
Modular and Reusable Configurations: Promotes DRY (Don't Repeat Yourself) principles.
Resource Graph and Dependency Management: Plans and executes resource creation in the correct order.
State Management: Provides a single source of truth.