问题
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:
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:
- use a
terraform_remote_state
data source pointed at the remote state of your dependencies configuration. - employ a naming convention so the dependencies can be discovered using specific data sources
- 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.
来源:https://stackoverflow.com/questions/64811362/how-to-use-terraform-modules-for-code-re-use