2

I'm using terraform to automate creating an EC2 instance and installing LAMP stack. I'm using cloud-init for user data. AWS console shows the user data is rendered. However, the system log shows the commands are not executed. The system log shows different issues randomly it seems. Any insight is appreciated!

terraform 0.10.8 Amazon Linux AMI latest version

See below error sometimes during config phase.

Starting cloud-init: Cloud-init v. 0.7.6 running 'modules:config' 
Loaded plugins: priorities, update-motd, upgrade-helper
/dev/fd/11: line 1: /sbin/plymouthd: No such file or directory

Sometimes see below warnings during cloud-init "init" phase.

cloud-init[2442]: util.py[WARNING]: Failed loading yaml 
blob-init[2442]: util.py[WARNING]: Failed loading yaml blob
cloud-init[2442]: util.py[WARNING]: Failed at merging in cloud config part from part-001

Code below

resource "aws_instance" "ec2_min" {
  ami                         = "${var.ami}"
  instance_type               = "${var.instance_type}"
  vpc_security_group_ids      = ["${data.aws_security_group.ec2_sg.id}"]
  iam_instance_profile        = "${var.instance_profile}"
  associate_public_ip_address = "${var.associate_public_ip_address}"
  subnet_id                   = "${data.aws_subnet.subnet.id}"
  key_name                    = "${aws_key_pair.ssh_key.key_name}"
  user_data                  = "${data.template_file.lamp_install.rendered}"

  tags {
    Name                      = "${var.instance_name}"
  }
}

data "template_file" "lamp_install" {
  template = "${file("${var.cloud_init_conf}")}"
}

#cloud-config
repo_update: true
repo_upgrade: all

packages:
 - httpd24
 - php70
 - mysql56-server
 - php70-mysqlnd

runcmd:
 - service httpd start
 - chkconfig httpd on
 - [ sh, -c, "usermod -a -G apache ec2-user" ]
 - [ sh, -c, "chown -R ec2-user:apache /var/www" ]
 - chmod 2775 /var/www
 - [ find, /var/www, -type, d, -exec, chmod, 2775, {}, + ]
 - [ find, /var/www, -type, f, -exec, chmod, 0664, {}, + ]
 - service mysqld start
 - chkconfig mysqld on
  • Are those all the error messages? If not, log into the instance and look at the cloud-init logs. If you don't see any, try adding this line to the commands: `output : { all : '| tee -a /var/log/cloud-init-output.log' }` – KJH Nov 22 '17 at 16:40
  • thanks! those are the error messages relevant. Did check cloud-init log, and additional logging, did not see anything more helpful than syslog, – TooEarlyTooSoon Nov 23 '17 at 18:05
  • 1
    Have you tried running this against a different AMI, e.g. CentOS/Ubuntu/etc.? – KJH Nov 25 '17 at 18:46
  • Replacing some unknown variables (specifically AMI ID) with real values or at least telling us roughly what these are would increase the chance of getting a relevant answer. – Radek Simko Nov 25 '17 at 19:12
  • You may need to quote `{}` in the list of arguments to `find`. It has the meaning of an empty block in yaml and could confuse the parser. – Valentin Nov 28 '17 at 23:15
  • thanks for all the responses. The problem appears to be at the cloud-init init and the beginning of the config phase. my code was not executed because an earlier phase failed. It works fine when I manually created the EC2 instance with the same user data. It seems the network cache for the repo was not ready when EC2 was created via terraform. – TooEarlyTooSoon Nov 30 '17 at 18:15

0 Answers0