Terraform folder, file hash값으로 인프라 업데이트하기

Key Kim
Oct 3, 2021

--

Terraform으로 여러개의 AWS Lambda 를 배포할 때 env, 소스파일 등의 hash값을 기준으로 인프라 업데이트 유무를 결정하도록 구성했다.

이 때 terraform에서 제공하는 md5file 함수를 사용하는데, 문제는 폴더에는 file함수를 사용할 수 없어서 관련된 파일이 늘어날수록 코드가 지저분해지는 문제가 발생한다.

조금 찾아보니 이런게 있다.

https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/archive_file

terraform apply 할 때 마다 폴더를 압축하고, 압축파일의 hash를 구하여 null_resource.trigger 에 추가하면 해결될 것 같다.

코드를 작성하면 아래와 같다.

data "archive_file" "shared" {  type        = "zip"  source_dir = "${path.root}/shared"  output_path = "${path.module}/shared.zip"}
data "archive_file" "env" { type = "zip" source_dir = "${path.module}/env" output_path = "${path.module}/env.zip"}resource "null_resource" "ecr_image" { triggers = { python_file = md5(file("${path.module}/index.py")) docker_file = md5(file("${path.module}/Dockerfile")) requirements = md5(file("${path.module}/requirements.txt")) shared_hash = data.archive_file.shared.output_sha env_hash = data.archive_file.env.output_sha}provisioner "local-exec" { command = <<EOF sh build.sh \ EOF }}

--

--

No responses yet