Use the Terraform config to easily automate your infrastructure
Tags: terraform, beginners, devops, cloud
Published: 2022-08-03 06:01:00

## The Terraform configuration file
Did you ever have to create the same infrastructure again and again? Terraform allows you to create an automated solution to write your system once and then deploy it as often as you want to.

You only have to write a configuration once, and Terraform then takes the hard work of creating the infrastructure from you. Today I will show you the essential content of such a Terraform configuration file. So you can get started as soon as possible and save some serious time to do more awesome stuff.

## The Terraform Block
```Bash
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.13.0"
    }
  }
}
```
For this example, I will show you how to create a simple nginx container that runs locally using terraform. The only pre-requisite is to have terraform and docker installed locally. Please follow these steps if you want to join me but don’t have everything installed. https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/docker-get-started

This configuration is the heart of every terraform project. It describes your target infrastructure and is used to plan and create the defined infrastructure. I will show you how to create a configuration for a simple docker setup.

## The Provider Block
```Bash
# The default provider configuration; resources that begin with `aws_` will use
# it as the default, and it can be referenced as `aws`.
provider "aws" {
  region = "us-east-1"
}

# Additional provider configuration for west coast region; resources can
# reference this as `aws.west`.
provider "aws" {
  alias  = "west"
  region = "us-west-2"
}
```
The first block within the configuration is the terraform block. Here you describe the terraform settings you would like to use and the providers you’ll use. The providers are like APIs interacting with your target environment to create your infrastructure. If you want to specify a specific version for the provider, you can do so. By default, terraform uses the most recent version. For our example we will use an empty provider block as we will not add any provider-level configuration.
```Bash
provider "docker" {
}
```

## Terraform Ressources
```Bash
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
  }
}
```
The resources within terraform describe specific infrastructure components (resources) you will create. When you make a terraform resource, you will have to provide two strings:
- The first string clarifies the resource type to create. In our example, this is “docker_container”. The first part describes the provider (docker), and the second is the resource type (container).
- The second string specifies the name of the resource. Here you can choose whatever name you want, and I recommend using sensible names to know what the resources are called later.

After these two strings, you can provide further configuration details. For a cloud compute instance, this would, for example, be the instance size or the network to connect. For our example, we will create two resources:
- The first resource is our image with nginx. Here we can provide some basic settings for the image and then re-use it for our containers.
- The second resource is our container. Here we tell the container to use our before-created image and the used ports.
Your final configuration should now look like this.
```Bash
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.13.0"
    }
  }
}

provider "docker" {

}

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
  }
}
```
With this, we have set up everything needed within a terraform configuration. You can now run the configuration and your infrastructure to your local docker environment.

## Conclusion
As you can see, it is straightforward to understand the basics of docker. You created a simple container for your local docker environment for this example. You can deploy the infrastructure to your favourite cloud environment. All you have to do is change the provider and add some configurations to match your requirements.