ECHOKE
Guides

What is Terraform

Engin Can Höke
#terraform#infrastructure as code#devops

Terraform is an open-source infrastructure as code (IaC) software tool that provides a consistent CLI workflow to manage hundreds of cloud services. It codifies cloud APIs into declarative configuration files, allowing you to define and provision data center infrastructure using a high-level configuration language.

Why Use Terraform?

While it’s possible to deploy resources using cloud providers’ user interfaces or command-line interfaces, using Terraform offers several advantages:

Setting Up Terraform with AWS

To get started with Terraform on AWS, follow these steps:

1. Install Terraform

Download and install Terraform from the official website.

2. Configure AWS Credentials

Ensure that your AWS credentials are configured. You can set them up using the AWS CLI:

aws configure

3. Create an S3 Bucket for State Management

Terraform uses a state file to keep track of your infrastructure. It’s recommended to store this file in a secure, remote location. For AWS, you can use an S3 bucket:

aws s3api create-bucket --bucket your-terraform-bucket --region your-region

4. Configure the Backend in Terraform

In your Terraform configuration file, specify the backend as the S3 bucket you created:

terraform {
  backend "s3" {
    bucket = "your-terraform-bucket"
    key    = "path/to/your/key"
    region = "your-region"
  }
}

5. Define the AWS Provider

Specify the AWS provider and pin the version to avoid unexpected changes:

provider "aws" {
  version = "~> 3.0"
  region  = "your-region"
}

6. Initialize Terraform

Initialize your working directory to prepare it for use:

terraform init

7. Define Resources

Create a Terraform configuration file to define the resources you want to provision. For example, to create an EC2 instance:

resource "aws_instance" "web" {
  ami           = "ami-08c148bb835696b45"
  instance_type = "t2.micro"

  tags = {
    Name = "hello-world"
  }
}

8. Plan and Apply

Review the changes Terraform will make to your infrastructure:

terraform plan

If everything looks good, apply the changes:

terraform apply

Understanding Data Blocks

In Terraform, a resource block defines a resource to be created, while a data block references an existing resource. For example, to get the latest Amazon Linux 2 AMI:

data "aws_ami" "amazonlinux2" {
  most_recent = true

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["amazon"]
}

resource "aws_instance" "second-ec2" {
  ami           = data.aws_ami.amazonlinux2.id
  instance_type = "t2.micro"

  tags = {
    Name = "hello-world-2"
  }
}

This configuration retrieves the latest Amazon Linux 2 AMI and uses it to launch an EC2 instance.

For more detailed information, refer to the official Terraform documentation.

I hope this guide helps you understand the basics of Terraform and how to get started with it.

← Blog'a Dön