iTranslated by AI
Automate Terraform Execution with Atlantis
Introduction
Hello! I'm tatsuo48 from Hamee Corp.
I'm going to write about Atlantis, an automation tool for Terraform.
I will mainly cover the following:
- What is Atlantis?
- Basic usage and operational tips
Conversely, I won't cover:
- Internal specifications of Atlantis
- How to set up Atlantis
- Please refer to the Installation Guide.
- Using a GitHub App[1] is recommended.
What is Atlantis?
Atlantis is a tool that makes it possible to perform Terraform operations (plan and apply) based on comments in a Pull Request (PR).
The top image on the official site is easy to understand, so I'll explain while looking at that.
Create a PR

Create a PR that changes Terraform resources!
Automatically execute terraform plan

Atlantis, having detected the PR creation via webhooks or other means, git clones the repository and executes terraform plan!
It then returns the execution results as a comment on the PR.
Execute terraform apply

After reviewing the execution results and ensuring there are no issues, you comment atlantis apply.
Again, Atlantis detects this comment via webhooks and this time executes terraform apply.
Then, it returns those execution results as a comment on the PR as well.
If everything looks good, merge the PR to complete the changes 🙌
Couldn't we do this with GitHub Actions or CircleCI?
Yes! You can automate terraform plan and terraform apply using GitHub Actions as well.
There are GitHub Actions like the one below:
And tools for notifying terraform plan results:
Compared to automation using a combination of such tools, Atlantis offers the following advantages. [2]
PR-level Locking Mechanism
Atlantis has a feature to lock at the PR level.
This allows changes to be applied one by one in order, even when multiple people create PRs concurrently.
Terraform itself has a locking mechanism, but that is intended to lock the tfstate to prevent concurrent Terraform executions. Therefore, if multiple people create PRs simultaneously, and the timing of terraform plan or terraform apply differs, there's a risk of unintentionally overwriting other people's changes without noticing them.
With Atlantis, it will inform you via comment if "automatic terraform plan could not be executed because another PR holds the lock," allowing you to notice the conflict.

terraform apply via comments
In Atlantis, you perform Terraform CLI operations based on PR comments.
If you've actually used Terraform, you probably know how often a terraform apply works perfectly on the first try, especially when creating AWS resources.
Doesn't it often fail due to things like "restrictions on character types for resource names" or "character limit constraints"?
When automating with something like GitHub Actions, terraform apply is often triggered by merging the PR. In that case, if terraform apply fails, you have to create a new PR to fix it.
By using Atlantis, you can iterate through trial and error on a single PR, and combined with the locking mechanism mentioned above, you can safely run terraform apply as many times as needed.
Avoid passing infrastructure resource operation permissions to CI services
When using GitHub Actions to create infrastructure resources like AWS via Terraform, you typically pass IAM credentials with relatively strong permissions to the CI service.
Even if you try to use the principle of least privilege, it's difficult to anticipate every AWS service you might need to create. Furthermore, even if you could, it's a hassle to adjust permissions every time you start using a new service.
As a result, people tend to pass broad, strong permissions (like being able to create IAM roles).
If you build the Atlantis infrastructure within AWS, you can grant permissions using IAM roles. This way, IAM credentials are only used within AWS, which is more secure compared to passing them to a CI service. [3]
Usage
Basically, once you have set it up, it can be used as is, but you can also modify its behavior by placing a configuration file named atlantis.yaml in the repository.
When there are multiple tfstates in one repository
You can use Atlantis separately for each project by dividing them as follows:
version: 3
projects:
- name: ProjectA
dir: terraform/ProjectA
- name: ProjectB
dir: platform/ProjectB
Automatically execute terraform plan when there are changes in a module
You might have a module directory within a single repository.
├── module
│ └── alb
│ └── main.tf
├── projectA
│ └── main.tf
└── projectB
└── main.tf
In such cases, by specifying relative paths in when_modified as follows, automatic plans will be executed even when there are changes under the module directory.
version: 3
projects:
- name: ProjectA
dir: terraform/ProjectA
autoplan:
when_modified:
[
"*.tf",
"*.tfvars",
"../modules/*/*.tf",
]
enabled: true
- name: ProjectB
dir: platform/ProjectB
autoplan:
when_modified:
[
"*.tf",
"*.tfvars",
"../modules/*/*.tf",
]
enabled: true
Merge automatically after terraform apply is complete
Adding it to the previous example looks like this:
version: 3
automerge: true
projects:
- name: ProjectA
dir: terraform/ProjectA
autoplan:
when_modified:
[
"*.tf",
"*.tfvars",
"../modules/*/*.tf",
]
enabled: true
- name: ProjectB
dir: platform/ProjectB
autoplan:
when_modified:
[
"*.tf",
"*.tfvars",
"../modules/*/*.tf",
]
enabled: true
Summary
There aren't many articles in Japanese, and it makes me a bit sad that it doesn't seem to be very popular in Japan yet.
I hope this article helps with its adoption 🙌
Tomorrow, for Day 13 of the Hamee Advent Calendar 2021, it will be yamakona's "Retrospective methods I want to try someday from the 'Retrospective Guidebook'"!
-
https://www.runatlantis.io/docs/access-credentials.html#github-app ↩︎
-
While the parts that make up Atlantis's advantages could probably be built yourself if you tried hard enough, it would likely be quite difficult. ↩︎
-
Recently, there are mechanisms for securely passing permissions using OIDC, so it's possible to grant permissions to GitHub Actions more safely than before.
https://github.blog/changelog/2021-10-27-github-actions-secure-cloud-deployments-with-openid-connect/ ↩︎
Discussion