brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Aug 19. 2023

16. 테라폼-테라폼 클라우드와 Github.com연계

다음은 주말 CloudNet 테라폼 스터디 내용 참고하여  정리한 부분입니다.

https://gasidaseo.notion.site/gasidaseo/CloudNet-Blog-c9dfa44a27ff431dafdd2edacc8a1863




TFC(Terraform Cloud) 사용해보자.

VCS (Version Control System) = 대표적으로 Git 사용해보자.



<1> TFC + VCS 연계 : p278

<2> 워크스페이스에 VCS를 연계 - 최초 동작

<3> VCS를 통한 풀 리퀘스트 동작 확인



<1> TFC + VCS 연계 : p278


1

VCS와 직접 통합되어 별도 워크플로 작성 없이도 풀 리퀘스트에 대한 Plan 결과를 확인할 수 있으며 

지정된 브랜치에 병합이 발생하면 테라폼을 실행하는 자동화된 구성이 가능하다. 


지원되는 VCS는 아래와 같다

- Github.com / Github.com (OAuth) / Github Enterprise

- Gitlab.com / Gitlab EE와 CE

- Bitbucket Cloud / Bitbucket Server

- Azure DevOps Server / Azure DevOps Services



2

여기서는 Github.com 과 연동해보자



3

테라폼 클라우드와 VCS간 연동이 필요하다.

테라폼 클라우드와 Github.com 연동 작업해보자~


TFC Org → Setting →  Version Control > Provider ⇒ 

[Add a VCS provider] 클릭 : Github.com (Custom) 선택 후 → [register a new OAuth Application] 클릭


아래 내용 입력

register a new OAuth Application. 클릭.

Application name:Terraform Cloud (topasvga-org)

Homepage URL: https://app.terraform.io

Application description: Any description of your choice

Authorization callback URL:  https://app.terrafxxxxxxxxx  앞에 있다


 하단 Register application 클릭



4

Generagate a new client secret 클릭 후 생성 메모

맨 하단 Update application


4

Client ID와 Client Secret가 필요하다.

만들자.



5

TFC 입력 


테라폼 클라우드  앞 화면으로 가자


- Name : My Github.com

- Client ID : <xxxxxxxx>

- Client Secret : <xxxxxxx>

SSH Keypair 은 필수는 아니므로 [Skip and finish] 로 연동 마무리.


→ 맨 하단 Connect and continue ⇒ 변경 화면에서 Grant 처리하고 최종적으로 [Authorize] 




<2> 워크스페이스에 VCS를 연계 - 최초 동작


현재 사용하고 있는 워크스페이스에 VCS연계하자.



1

대상 VCS와의 인증 및 권한 처리가 완료되었으므로 다음 과정을 통해 워크스페이스에 VCS를 연계, 

워크스페이스는 Admin 권한으로 수행한다



2

워크스페이스 선택 → Setting → Version Control 선택 ⇒ [Connect to version control] 클릭

Version control workflow 선택

연동된 VCS목록에서 [My Github.com] 선택

포크된 저장소 선택

Confirm changes :**Auto Apply** 선택, **Always trigger runs** 선택, VCS branch(main 입력), 

Pull Request(Automatic… **체크**)

→ 하단 **Update VCS Setting** 클릭


3

워크스페이스와 VCS간 최초 연동되면 마지막 커밋 내용을 기반으로 Run이 실행된다.

워크스페이스 Overview에서 확인


[Run] 메뉴로 이동해 깃허브에 의해 트리거링된 정보를 확인하고 Apply를 실행한다

리소스 생성 완료 확인


4

AWS 콘솔가서 만들어진 부분을 확인하자!!!




<3> VCS를 통한 풀 리퀘스트 동작 확인


1

코드를 바꾸면 잘 동작하는지 확인하자~~


2

main.tf 파일 수정 : 

terraform.cloud 블록 주석 처리 ← VCS 연동으로 더 이상 필요하지 않음


아래 Project 내용도 주석 처리하자.

      Project = "terraform cloud workflow"    


3

cd terraform-aws-tfc-workflow/


terraform {

  # cloud {

  #   organization = "topasvga-org"

  #   hostname     = "app.terraform.io" # default

  #   workspaces {

  #     name = "terraform-aws-tfc-workflow"

  #   }

  # }

  required_providers {

    aws = {

      source  = "hashicorp/aws"

      version = "~> 4.0"

    }

  }

}

provider "aws" {

  region = var.region

  default_tags {

    tags = {

      Project = "terraform cloud workflow"

      Owner   = "jerry & tom"

    }

  }

}




4

tfc-workflow 브랜치 생성, 커밋, 푸시


git branch -M tfc-workflow

git add .

git commit -m "tfc workflow"

git push origin HEAD


결과 나오는 링크 클릭해 가자.

https://github.com/topasvga/terraform-aws-tfc-workflow/pull/new/tfc-workflow



5

나의 github로 변경하자!!!  (필수)


자신의 github 에서 rp 생성 



에러.

에러 조치


주석처리.

추가 연결 필요 없음!!


terraform {

#<<<<<<< tfc-workflow

=======

#  cloud {

#    organization = "topasvga-org"

#    hostname     = "app.terraform.io" # default

#

#    workspaces {

#      name = "terraform-aws-tfc-workflow"

#    }

#  }

# >>>>>>> main

  required_providers {

    aws = {

      source  = "hashicorp/aws"

      version = "~> 4.0"

    }

  }

}



→ Merge pull request : Confirm




5

AWS 콘솔 

VPC의 태그 변경  확인


      Project = "terraform cloud workflow"    







6

TFC에서 생성된 리소스 삭제 : 

TFC 워크스페이스 Setting → Destruction and Delete ⇒ Queue destroy plan 로 생성된 리소스 삭제




- 실습 완료 후 최종 삭제

    1. TFC Workspace 삭제

    2. TFC Provider 삭제

    3. Github Repo 삭제

    4. 로컬에 Repo 디렉터리 삭제

    5. (옵션) 임시로 AWS IAM 생성 했을 경우, AWS IAM 계정 삭제




처음부터 다시 보기

https://brunch.co.kr/@topasvga/3347



감사합니다.


매거진의 이전글 15. 테라폼 - Terraform Cloud(TFC)

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari