'Different cloud init command based on AMI

I have a question about terraform and cloud init. I wonder if anyone could help here... Suppose I have an AWS instance that I provision like this:

resource "aws_instance" "this" {
  ami                  = "xxxxxxx"
  instance_type        = "t2.nano"
...
  user_data = data.template_cloudinit_config.init.rendered

The cloud init configuration is loaded from a file. Here is a sample:

#cloud-config
#

bootcmd: 
  - [echo, "Ubuntu"]
  - [echo, "RedHat"]
  - [echo, "AmazonLinux"]

What I would like to do is to run the first echo when I choose an AMI that is Ubuntu, the second one when it's red hat, and the last one when it's amazon linux. Is it possible to do this with cloud config? I see there is a Scripts Vendor(1) in the documentation. But I'm not sure if we could use that feature from a template file loaded from disk. Any idea? Of course I could easily create three different files and load them separately based on a variable. I only wonder if there is a nicer solution.

(1) https://cloudinit.readthedocs.io/en/latest/topics/modules.html#scripts-vendor



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source