How to update terraform modules

5 min read | by Jordi Prats

When we change the location of any terraform module we need to run terraform init again to be able to pick up the right version:

$ terraform plan
Acquiring state lock. This may take a few moments...
Releasing state lock. This may take a few moments...
╷
│ Error: Module source has changed
│ 
│   on main.tf line 17, in module "terraform-module":
│   17:   source = "git::ssh://git@github.com/pet2cattle/terraform-module.git?ref=1.0.2"
│ 
│ The source address was changed since this module was installed. Run "terraform init" to install all modules required by this configuration.
╵

If we are using terraform modules from a git repository this includes changing it's tag:

module "terraform-module" {
  source = "git::ssh://git@github.com/pet2cattle/terraform-module.git?ref=1.0.2"

  (...)
}

To avoid downloading everything again, we can use the -upgrade flag to fetch the latest changes instead:

$ terraform init -upgrade
Upgrading modules...
Downloading git::ssh://git@github.com/pet2cattle/terraform-module.git?ref=1.0.2 for terraform-module...
- terraform-module in .terraform/modules/terraform-module
(...)

Initializing the backend...

Initializing provider plugins...
(...)

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

If the repository is big, it can save us from downloading all over again. On the other hand, if we want to update the providers we are using, we can also use terraform init -upgrade:

$ terraform init
Initializing modules...
(...)

Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

Initializing provider plugins...
- terraform.io/builtin/terraform is built in to Terraform
- Reusing previous version of hashicorp/helm from the dependency lock file
- Reusing previous version of hashicorp/archive from the dependency lock file
- Reusing previous version of carlpett/sops from the dependency lock file
- Reusing previous version of hashicorp/kubernetes from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file
- Installing hashicorp/helm v1.3.2...
- Installed hashicorp/helm v1.3.2 (signed by HashiCorp)
- Installing hashicorp/archive v2.2.0...
- Installed hashicorp/archive v2.2.0 (signed by HashiCorp)
- Installing carlpett/sops v0.6.3...
- Installed carlpett/sops v0.6.3 (self-signed, key ID 1468AC14E6819667)
- Installing hashicorp/kubernetes v1.13.4...
- Installed hashicorp/kubernetes v1.13.4 (signed by HashiCorp)
- Installing hashicorp/aws v3.23.0...
- Installed hashicorp/aws v3.23.0 (signed by HashiCorp)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
$ terraform init -upgrade
Upgrading modules...
(...)

Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

Initializing provider plugins...
- terraform.io/builtin/terraform is built in to Terraform
- Finding carlpett/sops versions matching "~> 0.5"...
- Finding latest version of hashicorp/archive...
- Finding hashicorp/helm versions matching "< 2.0.0"...
- Finding hashicorp/kubernetes versions matching "< 2.0.0"...
- Finding hashicorp/aws versions matching "< 5.0.0"...
- Installing hashicorp/kubernetes v1.13.4...
- Installed hashicorp/kubernetes v1.13.4 (signed by HashiCorp)
- Installing hashicorp/aws v4.13.0...
- Installed hashicorp/aws v4.13.0 (signed by HashiCorp)
- Installing carlpett/sops v0.7.0...
- Installed carlpett/sops v0.7.0 (self-signed, key ID 1468AC14E6819667)
- Installing hashicorp/archive v2.2.0...
- Installed hashicorp/archive v2.2.0 (signed by HashiCorp)
- Installing hashicorp/helm v1.3.2...
- Installed hashicorp/helm v1.3.2 (signed by HashiCorp)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html

Terraform has made some changes to the provider dependency selections recorded
in the .terraform.lock.hcl file. Review those changes and commit them to your
version control system if they represent changes you intended to make.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Posted on 26/04/2022

Categories