amazon-ec2terraformcloud-init

Terraform: Unable to provision more than one file to EC2 via cloud-init


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"
          },
        ]
      })
    }

Solution

  • 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"
            },
          ]
        })
      }