Atlantis 란? (Terraform Pull Request Automation)
IT/DevOps

Atlantis 란? (Terraform Pull Request Automation)

반응형

Intro

terraform 소스를 형상 관리하고 배포를 하기위해서 어떤 툴을 사용하시나요? 아마도 형상관리 툴은 대부분 git서비스를 사용할 것입니다. terraform 소스 배포는 별도 툴을 사용하지 않고 로컬 PC에서 terraform apply 를 하는 경우도 있을 것이고, 젠킨스를 사용하는 경우가 대부분일 것입니다.

만약 로컬 PC에서 terraform 소스를 배포할 경우 이력관리와 공유가 안돼기 때문에 여러사람들과 협업을 할 수가 없습니다. 또 젠킨스를 사용할 경우 terraform 소스를 git pull request 이후에 다시 젠킨스로 돌아와서 배포를 해야하는 번거로움이 있으며, 젠킨스 파이프 라인 스크립트를 별도 구성해야 한다는 단점이 있습니다.

오늘은 이러 점단으들 해결할수있는는 trraform pull request 자동화 툴인 Atlantis를 소개하고자 합니다.

Atlantis란?

Atlantis는 pull request를 통해 Terraform을 자동화하는 오픈소스 애플리케이션입니다. Terraform 소스를 github로 pull request를 한뒤 comment에 atlantis plan 또는 atlantis apply 와 같은 명령어를 통해 terraform cli 명령어를 사용할 수 있습니다.

Atlantis 예시 영상

https://www.youtube.com/watch?v=TmIPWda0IKg

Atlantis는 어떻게 동작하나요?

  • 개발자는 Github,GitLab 또는 Bitbucket Pull Request를 날립니다.
  • Git 서비스에 설정해놓은 Atlantis 웹훅서버에서 Git 서비스에 Comment에 입력된 atlantis 명령어를 전달받아서 terraform을 실행하게 됩니다.


그림 참조 : https://blog.amis.com/infrastructure-as-code-day-2-collaboration-242bd82a102c

Atlantis는 어떻게 구축하나요?

Atlantis 서버를 구축하는 방법은 여러가지가 있습니다. 이는 아래 Atlantis 공식 홈페이지에 자세히 설명이 되어있으니 참고 하시면됩니다.

참조 URL : https://www.runatlantis.io/docs/deployment.html#architecture-overview

Atlantis 를 구축해보자

저는 형상관리로 github로 하고, docker를 사용하여 구축을 진행하였습니다.

사전준비 1 : github token 생성 (https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-token)

사전준비 2 : webhook에 사용할 secret key 생성 (https://www.browserling.com/tools/random-string)

사전준비 3 : public ip또는 도메인으로 접근할 수 있는 가상서버 (저는 EC2를 public subnet에 생성하여 사용하였습니다.)

위에 사전준비 1,2,3를 진행하면 90%는 끝났는데요 이어서 다음을 진행합니다.

가상서버에 docker로 Atlantis 서버 실행

docker run -p 80:4141 ghcr.io/runatlantis/atlantis server --gh-user={YOUR_GIT_USER_ID} --gh-token {YOUR_GIT_TOKEN} --repo-allowlist='{YOUR_GIT_REPO_URL}'

YOUR_GIT_REPO_URL 은 astarisk를 사용하여 github.com/my-repo/* 와 같이 사용할 수 있습니다.

git webhook 설정

  • github설정에서 Webhooks or Hooks 선택
  • Add webhook 클릭
  • Payload 설정URL to http://$URL/events (or https://$URL/events )
  • 다시한번 위 URL에 /events 를 붙혔는지 확인
  • Content type to application/json
  • 사전준비2에서 생성한 Secret 설정
  • individual events 선택
  • 아래 항목 체크
    • Pull request reviews
    • Pushes
    • Issue comments
    • Pull requests
  • Active 체크박스 선택
  • Add webhook 클릭

참고 URL : https://www.runatlantis.io/docs/configuring-webhooks.html#github-github-enterprise

Github webhook 설정 확인하기

Github webhook 설정이 완료 되면 아래와 같이 초록색 체크 표시로 Atlantis 웹훅서버로 통신이 잘되고 있다고 보여지게됩니다.

만약 빨간색 엑스(x) 표시가 나오면 Atlantis 서버와 통신이 안되고 있는 것이기 때문에 통신이 안돼는 원인을 찾아보면 됩니다.

Atlantis 테스트 하기

이제 Atlantis로 terraform pull request 자동화 준비가 완료가 되었으니 테스트를 진행해보도록 하겠습니다.

먼저 git feature branch를 생성하고 git push를 하고 pull request를 생성해보겠습니다. 그리고 comment에 atlantis help 를 입력하면

다음과같이 atlantis 명령어가 실행되는것을 확인할 수 있습니다.

마찬가지로 atlantis plan ,atlantis apply 를 통해 terraform 명령어를 수행하고 화면에 출력할 수 도 있습니다.

반응형