Terraform Cloud와 AWS CodeCommit Registry의 VCS (Version Control System) 연동은 공식적으로 지원되지 않습니다. 하지만 AWS Code 시리즈의 기능을 이용하면 수동으로 VCS 연동이 가능합니다. 이번 글에서는 Terraform Cloud와 Code 시리즈를 활용한 VCS 연동 방법을 알아보겠습니다.
AWS에서 제공하는 서비스를 이용하여 환경을 다음 사진과 같이 구성하였습니다.
다음 링크에서 코드를 받아 루트 모듈의 변수를 변경하여 사용하시면 됩니다.
https://github.com/gweowe/Terraform_Cloud_VCS_using_Code_Pipeline
Terraform 코드는 아래 글을 참고하여 작성하였습니다.
git clone [Repository 주소]
Username for '' : [User name 입력]
Password for '' : [User password 입력]
Username
과 Password
는 terraform.state
파일에서 확인하실 수 있습니다.cd [Repository 폴더]
vi 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를 생성하는 테스트 코드를 작성합니다.
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 코드의 변수에서 지정한 값을 입력합니다.
CodeCommit Repository에 정상적으로 등록되었음을 확인합니다.
CodePipeline이 정상적으로 작동하는지 확인합니다.
Terraform Cloud에서 코드가 정상적으로 실행되는지 확인합니다.
최종적으로 테스트 코드의 목적인 VPC가 생성되었는지 확인합니다.