DEV Community

Cover image for Running Atlantis on AWS: GitOps for Terraform
Salaudeen O. Abdulrasaq
Salaudeen O. Abdulrasaq

Posted on Edited on AI-assisted

Running Atlantis on AWS: GitOps for Terraform

Putting Terraform behind pull requests

Running terraform apply from laptops leaves no shared record of who changed what. It also means cloud credentials live on every engineer's machine.

One way to fix this is to move every Terraform change onto pull requests. Atlantis runs the plan, posts it as a PR comment, and only applies after a review

Atlantis is an open-source server that listens for GitHub webhooks. When a PR touches Terraform, it runs terraform plan and comments the output. When a reviewer approves, someone comments atlantis apply and it applies from the same server, with the same credentials, every time.

This post covers how I run Atlantis v0.28.5 on a single EC2 instance. It manages two environments (dev and prod) that include an EKS cluster, Aurora PostgreSQL Serverless, SQS, ECR and a VPC. I'll also cover the three bugs that cost me the most time.

Architecture: one container, one role

Atlantis runs as a single Docker container on an EC2 instance, next to our Jenkins server. GitHub sends it signed webhooks. It reaches AWS by assuming a cross-account role, and keeps Terraform state in S3.

Atlantis on EC2 turns GitHub pull requests into Terraform runs in AWS

The only public piece is the Atlantis endpoint that receives webhooks. Everything Atlantis can touch is defined by the role it assumes.

The four files that make it work

The whole setup is four files in the infrastructure repo. Each one has a single job.

  • Dockerfile.atlantis --> Custom image: official Atlantis + AWS CLI + a pinned Terraform

  • docker-compose.atlantis.yml --> Runs the container on EC2: env vars, volumes, health check

  • repos.yaml --> Server-side policy: which repo, which branch, what a repo may override

  • atlantis.yaml --> Repo-level config: projects per environment and their workflows

1. A custom image
The official image has no AWS CLI. Our Kubernetes, Helm and kubectl providers authenticate to EKS with aws eks get-token, so plans failed without it.

FROM ghcr.io/runatlantis/atlantis:v0.28.5
USER root

RUN apk add --no-cache --upgrade expat python3 py3-pip curl unzip && \
    pip3 install --break-system-packages --no-cache-dir awscli

# Pre-install Terraform so Atlantis never downloads it at runtime
RUN curl -sSLO https://releases.hashicorp.com/terraform/1.10.0/terraform_1.10.0_linux_amd64.zip \
    && unzip -o terraform_1.10.0_linux_amd64.zip -d /usr/local/bin \
    && rm terraform_1.10.0_linux_amd64.zip \
    && chmod +x /usr/local/bin/terraform

USER atlantis
Enter fullscreen mode Exit fullscreen mode

The --upgrade expat and pip-installed AWS CLI v1 are deliberate. This would be explained later.

2. Running it with Docker Compose
The key server flags, all set as environment variables:

ATLANTIS_REPO_ALLOWLIST: "github.com/${GITHUB_ORG}/${GITHUB_REPO}"
ATLANTIS_REQUIRE_APPROVAL: "true"
ATLANTIS_REQUIRE_MERGEABLE: "true"
ATLANTIS_CHECKOUT_STRATEGY: "merge"
ATLANTIS_PARALLEL_POOL_SIZE: "2"
ATLANTIS_DEFAULT_TF_VERSION: "v1.10.0"
ATLANTIS_ENABLE_DIFF_MARKDOWN_FORMAT: "true"
ATLANTIS_REPO_CONFIG: "/etc/atlantis/repos.yaml"
Enter fullscreen mode Exit fullscreen mode

• CHECKOUT_STRATEGY: merge plans against the PR merged into main, not the branch alone. A stale branch can't hide changes that landed on main since.
• ENABLE_DIFF_MARKDOWN_FORMAT renders plans as diff blocks, so GitHub shows additions green and deletions red.
• network_mode: host lets the container reach the EC2 instance metadata service.
• A /healthz health check and capped json-file logs (3 × 10 MB) keep the box tidy.

3. Server-side policy in repos.yaml
This file is the guardrail that a PR can't edit. It lives on the server, not in the repo.

repos:
  - id: github.com/<org>/cb-terraform-infrastructure
    branch: /main/
    apply_requirements: [approved, mergeable]
    allowed_overrides: [apply_requirements, workflow, delete_source_branch_on_merge]
    allowed_workflows: [terraform-default, terraform-production]
    allow_custom_workflows: true
Enter fullscreen mode Exit fullscreen mode

4. Projects and workflows in atlantis.yaml
One project per environment directory. Dev autoplans on every change. Prod never autoplans, so someone has to ask for it with atlantis plan -p cb-prod.

version: 3
automerge: false
parallel_plan: true
parallel_apply: false   # one environment at a time

projects:
  - name: cb-dev
    dir: terraform/environments/dev
    autoplan:
      enabled: true
      when_modified: ["*.tf", "*.tfvars", "../../modules/**/*.tf"]
    apply_requirements: [approved, mergeable]
    workflow: terraform-default

  - name: cb-prod
    dir: terraform/environments/prod
    autoplan:
      enabled: false
    apply_requirements: [approved, mergeable]
    workflow: terraform-production
Enter fullscreen mode Exit fullscreen mode

The ../../modules/**/*.tf pattern matters. Without it, a change to a shared module would not trigger a plan in the environments that use it.
The production workflow adds terraform fmt -check -recursive and terraform validate before the plan, so formatting and syntax errors fail fast:

terraform-production:
  plan:
    steps:
      - env:
          name: TF_VAR_aurora_master_password
          command: 'echo "$PROD_AURORA_MASTER_PASSWORD"'
      - init:
          extra_args: [-upgrade]
      - run: terraform fmt -check -recursive
      - run: terraform validate
      - plan:
          extra_args: [-var-file=terraform.tfvars]
Enter fullscreen mode Exit fullscreen mode

State lives in S3 with native lockfiles (use_lockfile = true, new in Terraform 1.10). We keep a DynamoDB lock table alongside it while we migrate.

Security: who can apply, and with what credentials
Atlantis holds the keys to your AWS accounts, so the security work is mostly about narrowing who can make it act.

Gate the trigger. Three settings decide whether an apply can happen at all:
• ATLANTIS_REPO_ALLOWLIST accepts webhooks from exactly one repository. A PR on any other repo is ignored.
• ATLANTIS_GH_WEBHOOK_SECRET makes GitHub sign every payload, so Atlantis rejects forged events.
• apply_requirements: [approved, mergeable] is set in both repos.yaml and atlantis.yaml. A plan can't be applied until the PR has an approval and passes branch protection.

Keep secrets out of the repo. Database and Grafana passwords never appear in terraform.tfvars. They are environment variables on the container (loaded from an .env.atlantis file that is git-ignored). Each workflow maps them into TF_VAR_* with an env step, with separate DEV_* and PROD_* values:

- env:
    name: TF_VAR_aurora_master_password
    command: 'echo "$DEV_AURORA_MASTER_PASSWORD"'
Enter fullscreen mode Exit fullscreen mode


The matching Terraform variables are marked sensitive, so they show as (sensitive value) in the plan comment instead of plain text.

The EC2 instance hosting the atlantis container assumes the roles needed. The host's ~/.aws/config is mounted read-only into the container. It defines a profile that assumes a cross-account role into the target account. That keeps permissions scoped to what Terraform needs in that account.

allow_custom_workflowsis true here, so workflows defined in the repo's atlantis.yaml can run arbitrary run: commands on the server. That includes workflows changed in an unmerged PR. It's acceptable for a private repo with a small team. For anything wider, define workflows only in server-side repos.yaml and set this to false.

A change, start to finish
This is what a typical dev change looks like from the engineer's side:

  1. Open a PR that edits terraform/environments/dev/ or a shared module.
  2. Atlantis autoplans cb-dev and comments the plan as a coloured diff.
  3. A reviewer reads the plan, not just the code, and approves the PR.
  4. The author comments atlantis apply -p cb-dev. Atlantis re-checks approved and mergeable, applies, and comments the result.

  1. The author merges. automerge: false keeps that step a deliberate human action.

Production differs in two ways. Plans are manual (atlantis plan -p cb-prod), so a dev-only PR never shows a prod plan. And parallel_apply: false means a PR that touches both environments applies them one at a time, never together.
Atlantis also locks each project directory to the PR that planned it. A second PR touching cb-dev can't plan until the first is applied or unlocked. That stops two people from stepping on each other's plans.

Two general takeaways:
• Pin and pre-install your toolchain. Anything Atlantis fetches at runtime can break without any change on your side. Bake it into the image and test the image before rollout (aws --version, aws sts get-caller-identity, aws eks get-token, terraform version).
• Build for the right architecture. The host is linux/amd64. An image built on an Apple Silicon laptop without --platform linux/amd64won't run there.

What I'd do next
Atlantis on one EC2 instance was cheap to stand up, and it gave us reviewed, auditable Terraform changes for both environments. If you're starting out, this setup is enough. Here's what I plan to improve:
• Drop static AWS keys. The compose file still passes AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY next to the instance profile. Removing them leaves the EC2 role plus the assumed cross-account role as the only credentials.
• Move secrets to AWS Secrets Manager or SSM Parameter Store. env steps can call aws secretsmanager get-secret-value instead of reading a .env file on disk.
• Turn off allow_custom_workflows. Move the workflows into repos.yaml so a PR can't change what runs on the server.
• Add policy checks. Atlantis supports Open Policy Agent (OPA) / Conftest policy checks between plan and apply, for example to block public S3 buckets.

The config is short. Most of the effort went into the container image and the credential path, so test those first.

Have questions or a different approach? Let me know in the comments.

Top comments (7)

Collapse
 
michael_oyedele_e9bf7bb31 profile image
Michael Oyedele •

This is very detailed and direct. Great one

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

If the guardrail lives in repos.yaml, a PR can just move it. The review is the enforcement then, not a separate check.

Collapse
 
sirlawdin profile image
Salaudeen O. Abdulrasaq • • Edited

Thanks for your feedback @robertpaschal.

Note that Atlantis loads repos.yaml from /etc/atlantis/repos.yaml, which is mounted read-only. PR branches are checked out into Atlantis's data directory, so a PR can't change that copy.

Atlantis uses the atlantis.yaml from the pull request, not from main, so repos.yaml may allow that file to do an override.

To make repos.yaml an independent guardrail:

  1. Remove apply_requirements and workflow from allowed_overrides.
  2. Set allow_custom_workflows: false.
  3. Move both workflows, including the env steps, into repos.yaml. Keep atlantis.yaml to project definitions only.
  4. Keep the server's repos.yaml outside the repo checkout, or deploy it separately. Add a CODEOWNERS rule so changes to atlantis.yaml and repos.yaml need your approval.
Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Read-only mount plus allow_custom_workflows false. That pair closes it.

Collapse
 
allison_okikiola_a77b1190 profile image
Allison Okikiola •

Thanks for sharing.

Collapse
 
lanreadeniji profile image
Olanrewaju Adeniji •

This is an insightful piece. The security part was detailed.

Collapse
 
fouty7 profile image
Aminu Ndakun •

I've found a guide to my next setup. 👏