Search code examples
amazon-ec2cloud-initterraform

User data not being execute by cloud-init


I am trying to get an Ubuntu Cloud Image (specifically ami-311a1a5b) to bootstrap docker after being launched by terraform. I have the following configuration for doing this:

resource "aws_instance" "app" {
  count = 1
  ami = "${lookup(var.amis, var.region)}"
  instance_type = "t2.micro"
  subnet_id = "${aws_subnet.private.id}"
  vpc_security_group_ids = ["${aws_security_group.default.id}"]
  key_name = "${aws_key_pair.deployer.id}"
  source_dest_check = false
  # user_data = ${file("cloud-config/app.yml")}
  user_data = "${template_file.cloud_config.rendered}"


  depends_on = ["aws_instance.nat"]


  tags = {
    Name = "airpair-example-app-${count.index}"
  }


  root_block_device {
    volume_type = "standard"
    volume_size = 20
  }
}

After the instance has started, docker is not installed and it seems that the cloud configuration has not been applied. The cloud configuration looks like:

# Cloud config for app servers
runcmd:
  # Install docker
  - curl -sSL https://get.docker.com | sudo sh
  # Run nginx
  - docker run -d -p 80:80 nginx

If I query the user-data it appears to be there:

services/ubuntu@ip-10-128-1-213:~$ curl http://169.254.169.254/latest/user-data
# Cloud config for app servers
runcmd:
  # Install docker
  - curl -sSL https://get.docker.com | sudo sh
  # Run nginx
  - docker run -d -p 80:80 nginx

My understanding is that it should be executed, is there a reason it isn't?


Solution

  • If you cloud config file there is exactly that, then you just need to edit the first line to read

    #cloud-config
    

    Otherwise cloud-init won't parse it as a cloud config file. It does look like terraform is correctly setting the user-data for you. The rest of the docs for cloud-init are at https://cloudinit.readthedocs.io/en/latest/, just be aware that the version on your AMI might be different from the docs so some features might not be available.