This is a terraform data source, called a template_file. This will allow us to pass in variables from terraform. Note: the data-source declaration here does not have access to variables already defined in variables.tf (or elsewhere) - you have to pass them in.
Update: the templatefile data source has been replaced with the templatefile function
data "template_file" "install_programs" {
template = file("${path.module}/scripts/install-programs.yaml")
vars = {
cluster_stack_name = var.cluster_stack_name,
region = data.aws_region.current.name,
instance_name = var.instance_name,
}
}
User data can come is several formats - the format I’m using here is cloud-config syntax. Here is the module reference.
This is a way to specify how cloud-config YAML (user-data) are merged together when there are multiple YAML files. Previously the merging algorithm was very simple and would only overwrite and not append lists, or strings. There are a few different ways to do this, in the example below we’re including merge-type header for every cloud-config YAML file. (see: merge-types docs)
#cloud-config
merge_how:
- name: list
settings: [append]
- name: dict
settings: [no_replace, recurse_list]
runcmd:
- echo
- echo "*** Schedule Docker Cleanup"
- echo "--- Set up 4 am docker system prune job"
- touch /var/log/docker-prune.out
- chown ec2-user:ec2-user /var/log/docker-prune.out
- echo
See: Terraform cloud_config data source
# Bringing it all together into a single, multi-part cloud-init configuration
data "cloudinit_config" "user_data" {
part {
content_type = "text/cloud-config"
content = data.template_file.install_programs.rendered
}
part {
content_type = "text/cloud-config"
content = data.template_file.configure_docker.rendered
}
}
/var/lib/cloud/instances/[instance-id]/user-data.txt
less /var/log/cloud-init.log
less /var/log/cloud-init-output.log
There are five stages to boot: