Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform modules using a git branch as a source

Tags:

git

terraform

Using Terraform modules with a git branch as a source,
I am referring to:

git::ssh://private_server:myport/kbf/my_repository.git//ecs-cluster?ref=v0.0.1

In my module source parameter, this works great and provides me with my module at tag v0.0.1 on master.

However I'd like to specify a branch, not a tag, but am not sure how to do this.

like image 711
krystan honour Avatar asked Sep 27 '18 14:09

krystan honour


People also ask

What type of source Cannot be used for a Terraform module?

We don't recommend using absolute filesystem paths to refer to Terraform modules, because it will tend to couple your configuration to the filesystem layout of a particular computer.

How do you correctly reference a private registry module source?

The syntax for referencing private modules in the module block source argument is <HOSTNAME>/<ORGANIZATION>/<MODULE_NAME>/<PROVIDER_NAME> . Hostname: For the SaaS version of Terraform Cloud, use app.terraform.io . In Terraform Enterprise, use the hostname for your instance or the generic hostname.

Where are Terraform modules stored?

Note: When installing a remote module, Terraform will download it into the . terraform directory in your configuration's root directory. When installing a local module, Terraform will instead refer directly to the source directory.

Can Terraform modules use other modules?

A Terraform module (usually the root module of a configuration) can call other modules to include their resources into the configuration.


6 Answers

As mentioned in the Terraform documentation here:

git::ssh://private_server:myport/kbf/my_repository.git//ecs-cluster?ref=myBranch
like image 116
rahuljain1311 Avatar answered Oct 17 '22 01:10

rahuljain1311


Exactly the same way. You have a generic ref there that Git will work out what you mean by context assuming no collisions.

If you do have 2 refs that are ambiguous then Git will error and tell you that it's an ambiguous ref and force you to specify the full ref using refs/heads/branch-name or refs/tags/tag-name.

like image 25
ydaetskcoR Avatar answered Oct 17 '22 02:10

ydaetskcoR


You can use something like this:

module "test_module" {
  source = "git::ssh://bitbucketURL/my_repo.git?ref=BranchName"
}

bitbucketURL: Go to bitbucket UI, check clone URL, copy from it.

If you are using something other then bitbucket, please refer to: https://www.terraform.io/docs/modules/sources.html

like image 42
Bharathi Anbazhagan Avatar answered Oct 17 '22 01:10

Bharathi Anbazhagan


The value of the ref argument can be any reference that would be accepted by the git checkout command, including branch and tag names.

Sample code to use the module using git tag and branch. reference_link

In simple language: just after ref= add the tag or branch as required.

module "vpc" {
    source = "git::https://example.com/vpc.git?ref=v1.2.0"
}

module "vpc" {
    source = "git::https://example.com/vpc.git?ref=develop"
}
like image 36
Raghu Reddy Avatar answered Oct 17 '22 02:10

Raghu Reddy


Just like mentioned before to reference a branch in your terraform module. All you need to do is after the ref= .. instead of mentioning the tag, mention the branch name.

So.. instead of

module "vpc" {
source = "git::https://example.com/vpc.git?ref=v1.2.0"
}

which is what you use to reference a tag in that repo... use the one below

module "vpc" {
source = "git::https://example.com/vpc.git?ref=YourBranchName"
}

Hope it is clear and it makes sense.

like image 1
ApotiEri Avatar answered Oct 17 '22 01:10

ApotiEri


I know this question is about private server repo, but in case someone is looking for unprefixed github.com urls, this works:

module "foo" {
  source = "github.com/org/repo//path/to/module?ref=branch-name"
}
like image 1
iamolegga Avatar answered Oct 17 '22 02:10

iamolegga