This repository demonstrates setting up Atlantis locally for Terraform automation with GitHub integration.
When there is a PR to change our Terraform files, GitHub sends a message to Atlantis through the repository's webhook, notifying it. Since we are running Atlantis locally, we need ngrok to expose our endpoint to the internet so GitHub's events can reach it. Atlantis, in turn, will comment on the PR with the plan, and we can decide whether to apply the configurations.
Run the prereqs.sh
script to install the tools needed to complete the steps below:
chmod +x ./prereqs.sh
./prereqs.sh
- Sign up or log in to ngrok.
- Copy your auth token from the dashboard and enable the static domain feature.
- Start the ngrok proxy on port 4141:
ngrok http 4141
- Export the URL (replace
your-static-domain
with your actual ngrok domain):export URL=https://your-static-domain.ngrok-free.app
-
Generate a webhook secret. This will be configured on GitHub's webhook so that when GitHub calls Atlantis, it knows the request is authentic:
export SECRET=$(pwgen -Bs 20 1)
-
Add a webhook to your GitHub repository using the GitHub CLI:
gh api \ --method POST \ -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: 2022-11-28" \ /repos/lariskovski/atlantis-local/hooks \ -f "name=web" \ -f "active=true" \ -f "events[]=issue_comment" \ -f "events[]=push" \ -f "events[]=pull_request" \ -f "events[]=pull_request_review" \ -f "config[url]=$URL/events" \ -f "config[secret]=$SECRET" \ -f "config[content_type]=json" \ -f "config[insecure_ssl]=0"
- Create a Personal Access Token with the
repo
scope. - Export the token:
export TOKEN="your-github-token"
-
Set the required environment variables:
export GH_USERNAME=$(git config user.name) export REPO_ALLOWLIST="github.com/$GH_USERNAME/atlantis-local"
-
Start the Atlantis server:
atlantis server \ --atlantis-url="$URL" \ --gh-user="$GH_USERNAME" \ --gh-token="$TOKEN" \ --gh-webhook-secret="$SECRET" \ --repo-allowlist="$REPO_ALLOWLIST"
-
Create a pull request with Terraform changes to test the setup:
git checkout -b atlantis-test-$(pwgen -Bs 5 1) echo " " >> main.tf git add main.tf git commit -m "add change to trigger atlantis" gh pr create --title "Atlantis" -b " " -R $REPO_ALLOWLIST open https://$REPO_ALLOWLIST/pulls
-
Check that Atlantis is running:
curl ${URL}
If a lock is created, open the Atlantis dashboard:
open $URL
Click on the lock and remove it.
- Verify the ngrok tunnel is active and accessible.
- Confirm webhook deliveries in GitHub repository settings.
- Check Atlantis server logs for any errors.