Terraform: Windows cloud-init is unable to provision files from Parameter Store

229 views Asked by At

I have this configuration where I get files from AWS SSM Parameter Store and save them

data "aws_ssm_parameter" "certificate" {
  name = "/project/certificate"
}
    
data "aws_ssm_parameter" "private" {
  name = "/project/private"
}
    
data "aws_ssm_parameter" "config" {
  name = "/project/config"
}
    
data "cloudinit_config" "userdata" {
  gzip          = false
  base64_encode = false
    
  part {
    filename     = "init.ps1"
    content_type = "text/x-shellscript"
    content      = file("${path.module}/scripts/init.ps1")
  }

  part {
    content_type = "text/cloud-config"
    content = yamlencode({
      write_files = [
        {
           content     = data.aws_ssm_parameter.certificate.value
           path        = "C:\\aws-iot-device-client\\certs\\certificate.pem.crt"
           permissions = "0744"
        },
        {
           content     = data.aws_ssm_parameter.private.value
           path        = "C:\\aws-iot-device-client\\certs\\private.pem.key"
           permissions = "0744"
        },
        {
           content     = data.aws_ssm_parameter.config.value
           path        = "C:\\aws-iot-device-client\\aws-iot-device-client.conf"
           permissions = "0744"
        },
      ]
    })
  }
}
    
resource "aws_instance" "win_device_agent_ec2" {
  ami                  = var.ec2_ami
  subnet_id            = var.ec2_network
  key_name             = var.ec2_ssh_key
  instance_type        = var.instance_type
  iam_instance_profile = var.ec2_iam_role
    
  vpc_security_group_ids = [
     var.ec2_sg,
  ]
    
  user_data = data.cloudinit_config.userdata.rendered
    
  tags = {
     Name = var.instance_name
  }
}

The issue is that powershell script init.ps1 works fine, but second part with cloud-config is not working. Trying to understand what is missing and why the values are not being picked up and stored. Tested on linux, works fine.

1

There are 1 answers

2
Brett Holman On

but second part with cloud-config is not working. Trying to understand what is missing and why the values are not being picked up and stored. Tested on linux, works fine.

cloud-init supports many different operating systems, but not Windows. Do not expect cloud-init userdata to work on Windows.