Skip to content

Initialising Terraform

Before we can begin to actually execute the Terraform code we have to initialise Terraform itself. This means we instruct Terraform to download all the providers we need, and modules we may have referenced in our code, and finally to initialise the remote back end which in our case is GitLab.

Initialising the State

Now we can initialise our remote Terraform state.

1
terraform init

You should get back something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$ terraform init

Initializing the backend...

Successfully configured the backend "http"! Terraform will automatically
use this backend unless the backend configuration changes.
2021/06/13 17:15:26 [DEBUG] GET https://gitlab.com/api/v4/projects/26667064/terraform/state/production

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v3.45.0...
- Installed hashicorp/aws v3.45.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

...

That's the out out you'll see. Let's breakdown it down and get a better understanding of what we're seeing.

HTTPS GET

We can see a line like this:

1
2021/06/13 17:15:26 [DEBUG] GET https://gitlab.com/api/v4/projects/26667064/terraform/state/production

This is talking to the GitLab HTTPS API. It's instructing the API that we want a Terraform state (terraform/state/) called production on project ID 26667064.

Plug-ins

These lines:

1
2
3
4
Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v3.45.0...
- Installed hashicorp/aws v3.45.0 (signed by HashiCorp)

Are Terraform downloading the plug-ins we'll need. At this point we just need the one: AWS.

Lock File

1
Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above

This is an important line. Make sure to add the lock file to your repository:

1
git add .terraform.lock.hcl

This ensures that when you download this repository on another system, or another engineer engages with it, they will pull the correct provider plug-ins down to the correct version.

We can see what this file is doing if we look at it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.

provider "registry.terraform.io/hashicorp/aws" {
  version = "3.45.0"
  hashes = [
    "h1:LKU/xfna87/p+hl5yTTW3dvOqWJp5JEM+Dt3nnvSDvA=",
    "zh:0fdbb3af75ff55807466533f97eb314556ec41a908a543d7cafb06546930f7c6",
    "zh:20656895744fa0f4607096b9681c77b2385f450b1577f9151d3070818378a724",
    "zh:390f316d00f25a5e45ef5410961fd05bf673068c1b701dc752d11df6d8e741d7",
    "zh:3da70f9de241d5f66ea9994ef1e0beddfdb005fa2d2ef6712392f57c5d2e4844",
    "zh:65de63cc0f97c85c28a19db560c546aa25f4f403dbf4783ac53c3918044cf180",
    "zh:6fc52072e5a66a5d0510aaa2b373a2697895f51398613c68619d8c0c95fc75f5",
    "zh:7c1da61092bd1206a020e3ee340ab11be8a4f9bb74e925ca1229ea5267fb3a62",
    "zh:94e533d86ce3c08e7102dcabe34ba32ae7fd7819fd0aedef28f48d29e635eae2",
    "zh:a3180d4826662e19e71cf20e925a2be8613a51f2f3f7b6d2643ac1418b976d58",
    "zh:c783df364928c77fd4dec5419533b125bebe2d50212c4ad609f83b701c2d981a",
    "zh:e1279bde388cb675d324584d965c6d22c3ec6890b13de76a50910a3bcd84ed64",
  ]
}

It's recording the hashes for version 3.45.0 of the hashicorp/aws provider.

Warning

Pay attention to the comment at the top of the file: don't edit the file manually. It's managed via terraform init.

Next

Now let's run a plan against our new state and determine what needs to be created.


Last update: August 25, 2021