Puppet for Cloud and Containers: Terraform, Cloud-Init, Docker, Kubernetes and Image Builds

Puppet Training Course 22 min min read Updated: Nov 17, 2026 Advanced
Puppet for Cloud and Containers: Terraform, Cloud-Init, Docker, Kubernetes and Image Builds
Advanced Topic 2 of 2

Where Puppet Fits Today

Modern teams use different layers: Terraform creates infrastructure, Docker/Kubernetes run applications, and Puppet configures the operating system layer: users, packages, hardening, agents, certificates, monitoring and compliance. Puppet is also valuable for long-lived virtual machines and bare metal.

LayerTypical tool
Cloud resources (networks, VMs, load balancers)Terraform, CloudFormation
Machine image buildPacker + puppet apply (optional)
OS configuration, compliance and drift correctionPuppet (agent) or Bolt
Application packagingDocker images, packages
Application orchestrationKubernetes, Nomad, systemd

Bootstrap a Cloud Server with cloud-init

yaml #cloud-config # Pass this as user data when the VM is created (Terraform, AWS, Azure, GCP) write_files: - path: /etc/puppetlabs/puppet/csr_attributes.yaml permissions: '0644' content: | --- custom_attributes: challengePassword: "${autosign_secret}" extension_requests: pp_role: webserver pp_environment: production pp_instance_id: "${instance_id}" runcmd: - rpm -Uvh https://yum.example.com/puppet-release.rpm # your internal repo or the official release package - dnf install -y puppet-agent - /opt/puppetlabs/bin/puppet config set server puppet.example.com --section main - /opt/puppetlabs/bin/puppet config set certname $(hostname -f) --section main - /opt/puppetlabs/bin/puppet resource service puppet ensure=running enable=true
hcl # Terraform: render the user data and start the server resource "aws_instance" "web" { count = 3 ami = data.aws_ami.rocky9.id instance_type = "t3.medium" subnet_id = aws_subnet.private.id user_data = templatefile("${path.module}/cloud-init.yaml", { autosign_secret = var.puppet_autosign_secret instance_id = "web-${count.index}" }) tags = { Name = "web-${count.index}", Role = "webserver" } }

The flow: Terraform creates the VM, cloud-init writes csr_attributes.yaml and installs the agent, the server autosigns the request (secret check), the node gets its role from a trusted fact and configures itself on the first run.

Keep secrets out of user data when possible: User data can be read from the instance metadata service. Use a short-lived token, or fetch the secret from a vault with the instance identity, and rotate it.

Node Cleanup: Decommissioning

When a VM is destroyed, its certificate and data stay on the server. Clean them up, or the old name can block a new server using the same name, and PuppetDB fills with dead nodes.

bash # On the Puppet server, when a node is removed sudo puppetserver ca clean --certname web03.example.com # PuppetDB: deactivate the node (the puppetdb-termini does this on "puppet node deactivate") sudo puppet node deactivate web03.example.com # Or purge automatically with a module like puppetlabs-puppet_agent / puppet_autosign cleanup jobs, # and set node-ttl in PuppetDB so nodes without reports expire # /etc/puppetlabs/puppetdb/conf.d/database.ini # node-ttl = 7d # node-purge-ttl = 14d

In auto-scaling groups, call a lifecycle hook or a small cleanup job that runs puppetserver ca clean through the CA API when an instance terminates.

Packer: Build Golden Images with Puppet

Instead of running Puppet on every new instance, bake most of the configuration into the machine image. At boot only small changes remain. This gives faster start-up and fewer surprises.

hcl # image.pkr.hcl source "amazon-ebs" "rocky" { region = "ap-south-1" source_ami = "ami-0123456789abcdef0" instance_type = "t3.small" ssh_username = "rocky" ami_name = "base-hardened-{{timestamp}}" } build { sources = ["source.amazon-ebs.rocky"] provisioner "shell" { inline = [ "sudo dnf install -y https://yum.example.com/puppet-release.rpm", "sudo dnf install -y puppet-agent", ] } provisioner "file" { source = "control-repo/" destination = "/tmp/control-repo" } provisioner "shell" { inline = [ "sudo /opt/puppetlabs/bin/puppet apply --modulepath=/tmp/control-repo/site-modules:/tmp/control-repo/modules --hiera_config=/tmp/control-repo/hiera.yaml -e 'include role::base_image'", "sudo rm -rf /tmp/control-repo /etc/puppetlabs/puppet/ssl", # remove certificates so each instance enrols fresh ] } }
Remove the SSL directory: If you bake the agent's certificate into an image, every instance would share one identity. Always delete /etc/puppetlabs/puppet/ssl (and any machine specific state) before the image is saved.

Golden Image or Runtime Configuration?

ApproachStrengthWeakness
Configure at runtime with the agentAlways up to date, corrects driftSlower boot, depends on the server
Bake in the image (immutable)Fast, repeatable, no server needed at bootNeeds rebuilds for each change
Both (recommended for many teams)Image has the base, Puppet keeps it correct and handles small changesTwo things to maintain

Docker Hosts and Containers

puppet # puppetlabs-docker module class { 'docker': version => '27.3.1', } docker::image { 'nginx': image_tag => '1.27', require => Class['docker'], } docker::run { 'web': image => 'nginx:1.27', ports => ['8080:80'], volumes => ['/srv/web:/usr/share/nginx/html:ro'], env => ['TZ=Asia/Kolkata'], restart_service => true, require => Docker::Image['nginx'], }

Puppet is a good fit for installing and hardening the Docker host (engine version, daemon settings, log driver, users, firewall) and for simple container runs. For larger workloads, Kubernetes manifests, Helm charts or Compose files are the better place for application definitions.

puppet file { '/etc/docker/daemon.json': ensure => file, mode => '0644', content => stdlib::to_json({ 'log-driver' => 'json-file', 'log-opts' => { 'max-size' => '10m', 'max-file' => '3' }, 'live-restore' => true, }), notify => Service['docker'], }

Kubernetes Nodes

Puppet can build the worker and control-plane hosts under Kubernetes: kernel settings, container runtime, kubelet configuration, swap off, time sync, CIS hardening, and node monitoring agents. Puppet does not replace Kubernetes tools for applications, so use Helm or GitOps tools for them.

puppet class profile::k8s_node { # kernel prerequisites kmod::load { ['overlay', 'br_netfilter']: } sysctl { 'net.bridge.bridge-nf-call-iptables': value => '1' } sysctl { 'net.ipv4.ip_forward': value => '1' } # swap off exec { 'swapoff': command => '/sbin/swapoff -a', onlyif => '/usr/bin/test -n "$(/sbin/swapon --show)"', } file_line { 'remove swap from fstab': path => '/etc/fstab', match => '\sswap\s', match_for_absence => true, ensure => absent, multiple => true, } include profile::containerd include profile::kubelet include profile::hardening }

Avoid Drift, Not Autoscaling Speed

  • Make the first run fast: a small base image, fewer slow exec resources, a local package mirror
  • Use splay and a good runinterval so a mass scale-out does not overload the server
  • Add more compilers when thousands of nodes check in
  • Use puppet apply and serverless pipelines for short-lived nodes if a server is not needed
  • Tag nodes with the role and environment from the cloud, and read them as trusted facts

Practice Tasks

  1. Write a cloud-init file that installs the agent and sets csr_attributes.yaml
  2. Use a lab Terraform file to create two VMs that enrol and get the role automatically
  3. Build an image with Packer that runs puppet apply and removes SSL files
  4. Install Docker with the module and run a container from Puppet
  5. Write a cleanup script that removes certificates when a VM is deleted

Conclusion

Use Terraform to create machines, cloud-init to enrol them with a role, Packer to bake base images, and Puppet to keep the OS correct and secure. Clean up certificates for deleted nodes and leave application deployment to container tools.

Quick Summary: cloud-init + csr_attributes + autosign; Packer apply and remove ssl; docker module for hosts; clean certs on destroy.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators