
Managing a Running Container as Terraform State: Deploying Super Mario to EC2 Without Kubernetes
A walkthrough of deploying a containerized Super Mario game to EC2 using Terraform's Docker provider, so the running container lives in Terraform state alongside the instance and security group, plus the two real production issues that came up building it.
Most "deploy a game as a DevOps demo" projects reach for Kubernetes even when the actual workload is a single container that one person runs to play a game. This project, Warpstack, is an attempt to see how far Terraform and Docker alone could go instead, and specifically whether Terraform could manage a running container the same way it manages a VM or a security group, rather than provisioning a box and then leaving you to SSH in and start the application by hand afterward.
The core idea
Terraform has an official Docker provider (kreuzwerker/docker) that talks directly to a Docker daemon over its API. That daemon doesn't have to be local. It can be a remote instance reached over SSH, which is exactly what makes this project work as a single pipeline instead of two disconnected steps.
The
aws provider creates the EC2 instance and its security group. A user_data bootstrap script installs and starts Docker on that instance the moment it boots, with no manual login required. The docker provider then connects to that same instance's Docker daemon over SSH, using Docker's SSH transport, and creates the image and container as real Terraform resources. Because both providers are declared in the same configuration, one terraform apply provisions the infrastructure and starts the application, and one terraform destroy removes all of it, container included, in a single pass.What the actual configuration looks like
The provider declaration sets up both AWS and Docker side by side:
terraform { required_version = ">= 1.5.0"
required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } docker = { source = "kreuzwerker/docker" version = "~> 3.0" } }}
provider "aws" { region = var.aws_region}
provider "docker" { host = "ssh://ec2-user@${aws_instance.mario_host.public_ip}:22"
ssh_opts = [ "-i", pathexpand(var.private_key_path), "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null" ]}
The
docker provider's host is where the two systems actually meet. Instead of pointing at a local Unix socket the way most Terraform-plus-Docker examples do, it points at a remote instance's SSH endpoint, and that public IP isn't known until Terraform has already created the instance. Terraform resolves that dependency automatically because the value is a direct attribute reference, not something wired together manually after the fact.The EC2 instance bootstraps Docker on first boot:
resource "aws_instance" "mario_host" { ami = var.ami_id instance_type = "t2.micro" key_name = var.key_name
vpc_security_group_ids = [aws_security_group.mario_sg.id] associate_public_ip_address = true
user_data = <<-EOF #!/bin/bash set -eux dnf update -y dnf install -y docker systemctl enable docker systemctl start docker usermod -aG docker ec2-user EOF
tags = { Name = "mario-docker-host" }}
And the application itself is two Terraform resources, not a shell command run after the fact:
resource "docker_image" "mario" { name = "sevenajay/mario:latest" depends_on = [aws_instance.mario_host]}
resource "docker_container" "mario" { name = "mario-game" image = docker_image.mario.image_id
ports { internal = 80 external = 8080 }
restart = "unless-stopped" depends_on = [docker_image.mario]}
Because the container is a resource in the same state file as the instance and the security group,
terraform plan will report drift on it exactly the way it would for any other resource, and terraform destroy tears down all three together.
What actually breaks the first time you run it
Two things went wrong while building this, and both are worth documenting because neither is a bug in Terraform, they're timing and account hygiene issues that only show up against a real AWS account.
The Docker provider connects before Docker is ready. On a first apply, the
docker provider tries to open an SSH connection to the instance and talk to its Docker daemon at roughly the same time the instance is still running its user_data script. The result is an error like:Error: failed to create Docker client: Error pinging Docker server...ssh: connect to host <ip> port 22: Network is unreachable
This looks alarming the first time you see it, but it isn't a real failure. Re-running
terraform apply once the instance has finished bootstrapping resolves it, since the instance and security group are already created and Terraform only needs to retry the Docker provider connection.Expired credentials mid-project. Separately,
terraform destroy failed partway through the project with:Error: Retrieving AWS account details: validating provider credentials:retrieving caller identity from STS: ... InvalidClientTokenId: The securitytoken included in the request is invalid
This wasn't a Terraform issue at all, it was expired local AWS credentials. The important operational detail is what this means while it's happening: the EC2 instance was still running, and still billing, for the entire time it took to notice the credentials had expired and reconfigure them. A destroy failure doesn't pause billing, so if this happens to you and you can't restore credentials immediately, terminating the instance manually through the AWS CLI or console is worth doing rather than leaving it running.
Why this matters beyond one game container
The interesting part of this project was never Super Mario, it's a convenient application specifically because it's visually easy to verify and requires no build pipeline of its own. The actual point is that a lot of Terraform-plus-Docker setups stop at "Terraform builds the server," and whatever gets deployed onto that server afterward is invisible to Terraform entirely, managed by hand, undocumented, or eventually just forgotten about. Treating the container itself as a first-class Terraform resource closes that gap. If it isn't in the state file, it isn't managed, and that principle holds regardless of how small or large the workload behind it actually is.
Repository
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article