In golang, I’m building an application that will return info about any Terraform Public Registry module. The application needs to see the files themselves. For instance, consider this module. Version 3.8.2 will resolve to this git repo and ref.
The user provides:
source = "terraform-aws-modules/s3-bucket/aws"
version = "3.8.2"
And Terraform knows where to get the files. Is there a public API (or other stable solution) that will allow code to map a module source/version to a repo+ref?
I’m using go-getter to fetch the repo, so anything that produces go-getter compatible source would also be welcome.
>Solution :
You can use the terraform API to get the repo source of any given module. An example in bash might be
[almalinux@vps-f116ed9f ~]$ source="terraform-aws-modules/s3-bucket/aws"
[almalinux@vps-f116ed9f ~]$ version="3.8.2"
[almalinux@vps-f116ed9f ~]$ url=https://registry.terraform.io/v1/modules/${source}/${version}/download
[almalinux@vps-f116ed9f ~]$ curl -s -i $url | grep x-terraform-get | cut -f2- -d':'
git::https://github.com/terraform-aws-modules/terraform-aws-s3-bucket?ref=v3.8.2
Also as noted in the docs the string may be go-getter comatible
A successful response has no body, and includes the location from which the module version’s source can be downloaded in the X-Terraform-Get header. Note that this string may contain special syntax interpreted by Terraform via go-getter. See the go-getter documentation for details.