1

I am trying to provision multiple files via cloudinit_config in Terraform

data "cloudinit_config" "userdata" {
  gzip          = true
  base64_encode = true
    
  part {
    content_type = "text/cloud-config"
    content = yamlencode({
      write_files = [
        {
           content     = data.aws_ssm_parameter.certificate.value
           path        = "/root/certificate.pem.crt"
           owner       = "root:root"
           permissions = "0644"
        },
      ]
    })
  }
  
  part {
    content_type = "text/cloud-config"
    content = yamlencode({
      write_files = [
        {
           content     = data.aws_ssm_parameter.config.value
           path        = "/root/configuration.conf"
           owner       = "root:root"
           permissions = "0644"
        },
      ]
    })
  }
}

resource "aws_instance" "ec2_ubuntu" {
  ...
  user_data_base64 = data.cloudinit_config.userdata.rendered
  ...
}

Only the last one appears on the file system. I tried to change the order but only last one appears. I need both files What am I missing in this configuration?

UPD: Tried this config, and see the same issue

  part {
    content_type = "text/cloud-config"
    content = yamlencode({
      write_files = [
        {
           content     = data.aws_ssm_parameter.certificate.value
           path        = "/root/certificate.pem.crt"
           owner       = "root:root"
           permissions = "0644"
    
           content     = data.aws_ssm_parameter.config.value
           path        = "/root/configuration.conf"
           owner       = "root:root"
           permissions = "0644"
          },
        ]
      })
    }
Marko E
  • 13,362
  • 2
  • 19
  • 28
Murad
  • 89
  • 1
  • 9
  • Did you check the cloud init logs,`/var/log/cloud-init-output.log` and `/var/log/cloud-init.log`? – Marko E Jun 30 '23 at 12:33

1 Answers1

2

In HCL (and JSON and several other things) the [ ] represents a list and { } represents a single item in that list. write_files takes a list of files. Each file would be in a separate { } block. Like this:

  part {
    content_type = "text/cloud-config"
    content = yamlencode({
      write_files = [
        {
           content     = data.aws_ssm_parameter.certificate.value
           path        = "/root/certificate.pem.crt"
           owner       = "root:root"
           permissions = "0644"
        },
        {
           content     = data.aws_ssm_parameter.config.value
           path        = "/root/configuration.conf"
           owner       = "root:root"
           permissions = "0644"
        },
      ]
    })
  }
Mark B
  • 183,023
  • 24
  • 297
  • 295