I have a terraform module and .yaml docker-compose with service configuration. Yaml have variables such as:
version: '3.2'
services:
cadvisor:
image: gcr.io/google-containers/cadvisor:${var.image_version}
container_name: cadvisor
volumes:
- /:/rootfs:ro
- /var/lib/docker:/var/lib/docker:ro
mem_limit: ${var.limit}
mem_reservation: ${var.reservation}
ports:
- 8080:8080
and my resource:
resource "local_file" "compose" {
content = file("${var.compose_path}/docker-compose.yml")
filename = "${path.module}/foo.bar"
}
The values is declared in my module. I'm going to use this docker-compose.yml in my project e.g. send to remote server and run there. How can I pass variable values from my module to yaml?
I tried to declare values and do some actions with my yaml such as resource "local_file"
to save as file, but .yaml remains without values.
CodePudding user response:
You can achieve what you want by using the templatefile
built-in function [1]:
resource "local_file" "compose" {
content = templatefile("${var.compose_path}/docker-compose.yml", {
image_version = var.image_version
limit = var.limit
reservation = var.reservation
})
filename = "${path.module}/docker-compose.yml"
}
This will also require a minor change to the docker-compose.yml
file:
version: '3.2'
services:
cadvisor:
image: gcr.io/google-containers/cadvisor:${image_version}
container_name: cadvisor
volumes:
- /:/rootfs:ro
- /var/lib/docker:/var/lib/docker:ro
mem_limit: ${limit}
mem_reservation: ${reservation}
ports:
- 8080:8080
[1] https://www.terraform.io/language/functions/templatefile