First steps with Terraform

What is Terraform ?

Terraform is an open source “Infrastructure as Code” tool, created by HashiCorp.

What is it for ?

This tool is used to manage infrastructure.

Is it easy to use ?

Terraform is quite easy to use.
It can be harder when you declare a complex infrastructure, but it’s really well documented.

Stop talking ! Show us how to use it !

In this example, we are going to use Terraform to create a bucket s3 on AWS.

To use Terraform, you need :

  • an AWS account (You can use a free-tier for 12 months on Amazon, more infos here :
  • to download Terraform
  • then, in your favorite editor, create a file called (terraform files have .tf extension)
    This is how you declare a ressource in Terraform :
    resource "<PROVIDER>_<TYPE>" "<NAME>" {
    [CONFIG …]
    First , the provider (for us it’s aws but it can be another provider of course) and the name of the ressource (here a bucket s3). Then, the name that we are going to use within our code.

So our declaration will look like this

provider "aws" {
  region = "eu-west-3"

resource "aws_s3_bucket" "my-test-bucket" {
  bucket = "my-first-bucket"

Then we are gonne execute some commands.

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" 
* version = "~> 2.10"

Terraform has been successfully initialized!
$ terraform apply
Terraform will perform the following actions:
  # aws_s3_bucket will be created
  + resource "aws_s3_bucket" "my-test-bucket" {
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.
Enter a value:

Validate with Yes Still creating[10s elapsed] Still creating[20s elapsed] Still creating[30s elapsed] Creation complete after 38s
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Your bucket S3 has been created !

You can go to your aws console to see it !

Wouhou ! Here is our brand new bucket s3 !

Congrats ! You have created your first ressource with Terraform !

Did you find this article valuable?

Support Sonia Manoubi by becoming a sponsor. Any amount is appreciated!