0
votes

Terraform v0.12.x

I thought I understood Terraform modules for code re-use after reading the docs, but apparently not.

Say I want to build a target group+EC2 instance infrastructure. I have this directory structure.

/terraform
/terraform/green.tf
/terraform/blue.tf
/terraform/module_ec2/ec2.tf
/terraform/module_tg/tg.tf

For example, /terraform/module_ec2/ec2.tf has this

resource "aws_instance" "ec2" {
  ami               = var.ami
  availability_zone = var.availability_zone
  ....
}

and /terraform/module_tg/tg.tf has

resource "aws_lb_target_group" "tg" {
  name = var.tg_name
  ...
}

I want blue.tf and green.tf to build their respective target group+EC2 infrastructure by using module_tg and moodule_ec2 and just passing to them the respective key/value pairs each module needs. How can I do this, that is, what would be contents of blue.tf and green.tf?

1
terraform.io/docs/configuration/modules.html: module "ec2" { source="./module_ec2" ... } ...luk2302
Can I have several modules in the blue.tf and green.tf files? If so, how can I just then build the blue.tf and not green.tf, and visa-versa?Chris F
yes you can have multiple modules in each file. no, you cannot run green and blue independently from each other, they are in the same directory and therefore belong to same deployment.luk2302
Ah I see, so how can I refactor my directory structure so I can run blue and green targets separately, but still have code re-use for the other modules? Maybe you can put this in an answer, because it all makes sense. Note that I have files (non-modules) common to both blue and green deployments. Thanks!Chris F
Also what if I need an output from the green run that I need in the blue run? For example, if the green run creates an EBS volume, how can the blue run get it?Chris F

1 Answers

0
votes

Consider breaking up your terraform configurations along lifecycle boundaries. The lifecycle of a set of resources is the time starting when you run terraform apply and ending when you run terraform destroy.

In your example and comments, you bring up an EBS volume which should be available to both green and blue stacks. The EBS volume or any other dependencies that outlive either stack should be in a separate folder.

To access details of the EBS volume or other longer-lived dependencies from each of your stacks there are a few options:

  1. use a terraform_remote_state data source pointed at the remote state of your dependencies configuration.
  2. employ a naming convention so the dependencies can be discovered using specific data sources
  3. a variation on item 1, create an output module in a subfolder of your dependency configuration which includes a terraform_remote_state and exposes only the outputs of the configuration.