Skip to content

Warn if your .terraform.lock.hcl is modified after running terraform init #18237

Description

@johnstcn

Background

When we run a Terraform provisioner job, we run terraform init to download modules before running the plan / apply.

When uploading a template, we encourage users to run terraform init locally to ensure that a .terraform.lock.hcl file is created. This allows Coder to improve provisioning speeds by re-using previously downloaded provider versions.

Problem

If you run terraform init to create a .terraform.lock.hcl, Terraform will initialise the lock file with provider hashes based on your OS/arch. If you run this on a different architecture to your target Coder instance, you may end up with missing hashes in your lock file.

Your only warning of this will be the following message in the provisioner job log:

- Reusing previous version of hashicorp/aws from the dependency lock file
- Installing hashicorp/aws v5.99.1...
- Installed hashicorp/aws v5.99.1 (signed by HashiCorp)
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!

What you really want to see is:

- Reusing previous version of hashicorp/aws from the dependency lock file
- Using hashicorp/aws v5.99.1 from the shared cache directory
Terraform has been successfully initialized!

The fix for this is to re-run terraform init on a version of Terraform with the same OS/arch.

Proposed Solution

  • Record the checksum of .terraform.lock.hcl before and after running terraform init.
  • If the checksum changes, warn the user that they may be downloading Terraform providers unnecessarily and direct them to re-generate the lockfile manually on the same OS/arch as Coder.
  • We can also attempt to highlight the message regarding changes to the lockfile in the UI.
  • Stretch goal: expose a provisioner method to run terraform init and update the lockfile if there are any changes.

Activity

  1. added theissue type on Jun 4, 2025
  2. johnstcn commented on Jun 9, 2025

    @johnstcn
    MemberAuthor

    More facts:

    The terraform providers lock command can be provided a number of target architectures for which to fetch the checksums, for example:

    terraform providers lock -platform=linux_amd64 -platform=linux_arm64 -platform=darwin_amd64 -platform=darwin_arm64
    

    From terraform providers lock --help:

      Normally the dependency lock file (.terraform.lock.hcl) is updated
      automatically by "terraform init", but the information available to the
      normal provider installer can be constrained when you're installing providers
      from filesystem or network mirrors, and so the generated lock file can end
      up incomplete.
    [...]
      -platform=os_arch      Choose a target platform to request package checksums
                             for.
    
                             By default Terraform will request package checksums
                             suitable only for the platform where you run this
                             command. Use this option multiple times to include
                             checksums for multiple target systems.
    

    See also: hashicorp/terraform#31435 (comment)

  3. added a commit that references this issue on Jun 9, 2025
    31ae915
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cliArea: CLI

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions