It depends on the parameter!
Many of the parameters you will expose as “pass through” parameters in your own module. For example, most modules expose aws_account_id
and aws_region
parameters, as those are set differently in every environment. If you are adding a “wrapper” module called foo
, then in infrastructure-modules/foo/vars.tf
, you will add these as input variables:
variable "aws_account_id" {
description = "The ID of the AWS Account in which to create resources."
}
variable "aws_region" {
description = "The AWS regioniin which the ECS Service will be created."
}
And pass them through to the ecs-service-with-alb
module:
module "service" {
source = "../services/ecs-service-with-alb"
aws_account_id = "${var.aws_account_id}"
aws_region = "${var.aws_region}"
}
Some of the parameters you may want to hard-code for your module. For example, perhaps for the foo
service, the image
is always acme/foo
and the command
is always ./bin/run-app.sh
:
module "service" {
source = "../services/ecs-service-with-alb"
aws_account_id = "${var.aws_account_id}"
aws_region = "${var.aws_region}"
image = "acme/foo"
command = ["./bin/run-app.sh"]
}
Finally, some of the parameters will be fetched from the terraform_remote_state data source. For example, to create the custom aws_alb_listener_rule
, you need the ARNs of the ALB listeners. Since the ALB is shared by multiple services, it is deployed in a separate module, so you have to fetch its remote state data:
data "terraform_remote_state" "alb" {
backend = "s3"
config {
region = "${var.terraform_state_aws_region}"
bucket = "${var.terraform_state_s3_bucket}"
key = "${var.aws_region}/${var.vpc_name}/networking/${var.is_internal_alb ? "alb-internal" : "alb-public"}/terraform.tfstate"
}
}
Now you can use that data in your aws_alb_listener_rule
:
resource "aws_alb_listener_rule" "http_host_rule" {
listener_arn = "${lookup(data.terraform_remote_state.alb.listener_arns, 80)}"
priority = 100
action {
type = "forward"
target_group_arn = "${module.service.target_group_arn}"
}
# Note how I'm using host-based routing here. You may want to make the domain name a variable so you can customize it for each environment
condition {
field = "host-header"
values = ["foo.acme.com"]
}
}
The existing modules in infrastructure-modules
, including ecs-service-with-alb
, have tons of examples of using terraform_remote_state
data sources.