6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【黒魔術】TerraformでCloud Functionsを更新確認してデプロイする

Last updated at Posted at 2021-02-05

TerraformでAWS lambdaをデプロイすると、コードに更新があった時だけリソースも更新してくれますよね。
GCP Cloud Functionsの場合、残念ながらコードを更新してもリソースの更新がかかりません。
今回は、そんな残念なTerraformくんにDirty hackを仕込んでちゃんとコードを更新したときにリソースにも更新がかかるようにしたいと思います。
まあ、アップロードするファイルにmd5付けるだけなんですけどね。

# Cloud Functionsにアップロードするファイルをzipに固める。
data "archive_file" "src" { 
  type        = "zip"
  source_dir  = "./src" 
  output_path = "./src.zip"
}

# GCS Bucketにzipで固めたソースコードをアップロードする。
resource "google_storage_bucket_object" "src" { 
  # ここでパスにmd5を付与することで、変更が検知できるようにする。これがだいじ。
  name   = "functions/src_${data.archive_file.src.output_md5}.zip" 
  bucket = "foo_bucket"
  source = data.archive_file.src.output_path
}

resource "google_cloudfunctions_function" "service" {
  name                  = "service"
  runtime               = "python37"
  source_archive_bucket = "foo_bucket"
  source_archive_object = google_storage_bucket_object.src.name
  trigger_http          = true
  available_memory_mb   = 128
  timeout               = 120
  entry_point           = "run"
}

おわり。

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?