Reputation: 11
I have an "aws_instance" resource (in the source module) that worked fine to bootstrap either centos, coreos or ubuntu instances via user_data = data.template_file.user-data.rendered
by using the following data block;
data "template_file" "user-data" {
template = file("${path.module}/bootstrap-${var.os_distro}.sh")
vars = {
access_port = var.access_port
service_port1 = var.service_port1
docker_api_port = var.docker_api_port
}
}
The associated file (e.g. bootstrap-centos.sh) was then loaded and rendered depending on the value for the $os_distro variable in the root module.
All was well until i switched from coreos for fedora coreos... the issue being that I now need to call a different datasource first (ct_config) to transpile my bootstrap-fcos.yaml file for ignition.
Is there any logic I can use in the source module to use a different datasource when i want to deploy a fedora coreos AMI? Seems totally against the power of terraform modules to take the easy way and create a new source module just for this new OS.
The salient parts from the source and root modules are;
SOURCE MODULE
resource ` "my-ec2-instance" {
count = var.node_count
availability_zone = element(var.azs, count.index)
subnet_id = var.aws_subnet_id
private_ip = length(var.private_ips) > 0 ? element(var.private_ips, count.index) : var.private_ip
ami = var.machine_ami
instance_type = var.aws_instance_type
vpc_security_group_ids = [aws_security_group.my-sg-group.id]
key_name = var.key_name
user_data = data.template_file.user-data.rendered
monitoring = false
ebs_optimized = false
associate_public_ip_address = var.public_ip
root_block_device {
volume_type = var.root_volume_type
volume_size = var.root_volume_size
delete_on_termination = true
}
}
data "template_file" "user-data" {
template = file("${path.module}/bootstrap-${var.os_distro}.sh")
vars = {
access_port = var.access_port
service_port1 = var.service_port1
docker_api_port = var.docker_api_port
}
}
variable "user_data" {
type = string
description = "userdata used to bootstrap the node"
}
variable "os_distro" {
type = string
description = "choose centos coreos or ubuntu to load either bootstrap-centos.sh, bootstrap-ubuntu.sh or bootstrap-coreos.sh from this module"
}
ROOT MODULE
module "demo_coreos_stg_ec2" {
source = ".../aws/ec2" # as per source module code above
node_count = local.node_count
azs = local.azs
aws_subnet_id = "subnet-c18c0fbb"
private_ips = ["172.31.16.20"]
machine_ami = data.aws_ami.fcos-stable-latest.id # latest stable fedora coreos release
aws_instance_type = "t2.micro"
key_name = "keys-2020"
user_data = data.ct_config.boot_config.rendered # convert the boot config in yaml to the ignition config in json via ct (config transpiler)
os_distro = var.os_distro # enables either bootstrap-centos.sh, bootstrap-ubuntu.sh or bootstrap-coreos.sh from this module
data "ct_config" "boot_config" {
content = data.template_file.fcos.rendered
strict = true
pretty_print = true
}
data "template_file" "fcos" {
template = file("${path.module}/bootstrap-fcos.yaml")
vars = {
access_port = var.access_port
service_port1 = var.service_port1
docker_api_port = var.docker_api_port
}
}
Notice that the root module needs to be able to first use the ct_config datasource before using the template_file datasource for loading the bootstrap-fcos.yaml for interpolation. Previously all 3 OS's could use template_file to load their .sh file.
Upvotes: 1
Views: 75