How to download specific module as zip file?

Hello,

I run terraform in restricted environment with no internet access.
Because if that I cannot download terraform providers and modules from the registry.

I found similar topic where explained how to download specific provider: Error: Failed to install provider - #2 by apparentlymart

Is there a similar instruction to download specific module?

I don’t think Terraform has any support for local caches or alternative download locations for modules, unlike what it supports for providers.

The registry does not store providers as zip files either - it just tells Terraform where on GitHub to go to, to get the module - e.g.:

$ curl -sSi https://registry.terraform.io/v1/modules/terraform-aws-modules/vpc/aws/5.0.0/download | grep x-terraform-get
x-terraform-get: git::https://github.com/terraform-aws-modules/terraform-aws-vpc?ref=v5.0.0

You would have to:

  • Ask the registry where on GitHub the specific module lives
  • Download it from GitHub yourself (your choice whether you use git CLI yourself, or find the URL format GitHub provides to allow direct download of a given Git tag as a zip file
  • Modify all the source attributes of module blocks in your Terraform configurations to reference the module at whichever alternate location you decide to host it at
1 Like

Yes, it’s correct that Terraform treats modules differently than providers in these ways.

The reason for that difference is that providers are effectively “global” to a Terraform configuration – all modules need to be able to share the same instance of a provider, and so they must agree on what provider they are using. That means that there needs to be some way to separate the addresses Terraform uses to “talk about” different providers from the physical location where they are stored, so you can then use a mixture of modules you wrote and modules other people wrote and have them still agree even though you’ve made a particular provider get installed from a secondary location (a “mirror”, in Terraform’s terminology).

On the other hand, every module block is independent of any others, so the address given in source can just be the physical location where the source code comes from and doesn’t also need to act as an identifier used to match calls between modules. You can publish an existing module package in a new location, change your calling module block to refer to that location, and it will be otherwise equivalent from Terraform’s perspective.

As @maxb said then: if you find the source repository of the module you want to use and copy it into a repository that you own and control then you can call the module from your repository instead of its original repository and, as long as you don’t change the module in other ways, it should behave equivalently to when you call it in its original location.

1 Like

Great! Thanks for the info!