[Terraform] Terraform Cloud와 AWS Code 시리즈 VCS 연동

gweowe·2023년 9월 25일
1

Terraform Cloud와 AWS CodeCommit Registry의 VCS (Version Control System) 연동은 공식적으로 지원되지 않습니다. 하지만 AWS Code 시리즈의 기능을 이용하면 수동으로 VCS 연동이 가능합니다. 이번 글에서는 Terraform Cloud와 Code 시리즈를 활용한 VCS 연동 방법을 알아보겠습니다.

환경 구성도

AWS에서 제공하는 서비스를 이용하여 환경을 다음 사진과 같이 구성하였습니다.

scrstc

AWS 환경 구축

1. Terraform을 사용하여 환경 구축

다음 링크에서 코드를 받아 루트 모듈의 변수를 변경하여 사용하시면 됩니다.

https://github.com/gweowe/Terraform_Cloud_VCS_using_Code_Pipeline

VCS 연동 테스트

1. Terraform으로 생성한 CodeCommit Repository 링크 복사하여 가져오기

스크린샷 2023-09-22 오후 4.01.08

git clone [Repository 주소]
Username for '' : [User name 입력]
Password for '' : [User password 입력]
  • UsernamePasswordterraform.state 파일에서 확인하실 수 있습니다.

2. 테스트 코드 작성

cd [Repository 폴더]
vi main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.12.0"
    }
  }
}

provider "aws" {
	region = "ap-northeast-2"
	access_key = ""
	secret_key = ""
}

resource "aws_vpc" "gweowe_vpc" {
  cidr_block = "192.168.0.0/16"
  enable_dns_support = true
  enable_dns_hostnames = true

  tags = {
    Name = "gweowe-vpc"
  }
}

Workspace에 기재되어 있는 코드를 활용하여 VPC를 생성하는 테스트 코드를 작성합니다.

3. main.tf 파일 push 하기

git add .
git commit -m "update main.tf"
git push origin [Default_Branch 이름]
Username for '' : [User name 입력]
Password for '' : [User password 입력]

Default_Branch 이름은 AWS 환경을 구축하는 Terraform 코드의 변수에서 지정한 값을 입력합니다.

4. 결과 확인

스크린샷 2023-09-22 오후 5.18.34

CodeCommit Repository에 정상적으로 등록되었음을 확인합니다.

png

CodePipeline이 정상적으로 작동하는지 확인합니다.

afwfjw1231jdia1f

Terraform Cloud에서 코드가 정상적으로 실행되는지 확인합니다.

스크린샷 2023-09-22 오후 5.27.16

최종적으로 테스트 코드의 목적인 VPC가 생성되었는지 확인합니다.

profile
정리하는 공간

0개의 댓글