0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Lambdaのコード管理とdeployについて

Last updated at Posted at 2025-04-24

課題を感じていた構成

  • Lambda関数のGoで書かれたコードを全てterraformで管理
  • 同じリポジトリにterraform管理するディレクトリ(teffagrunt)とアプリケーションディレクトリ(server)がある
  • アプリケーション(server配下)はGoで実装している
  • Lambdaの関数もGoで実装している

この構成で運用しづらかったこと

  • teffarormの中にアプリケーションのコードがある違和感
  • テストがやりづらい
    • localで動かすにはLambda特有のhandler関数 を都度localで動く形に置き換えが必要
    • terraform管理配下にコードがあることでアプリケーションディレクトリ配下の共通処理やライブラリが使えない
    • テストがやりづらい。アプリケーションディレクトリ配下にあるものはtestDBを使うようにしているがこれが使えない。

改善

  • Lambda(Goのコード)は、アプリケーションディレクトリに移動し、terraformではインフラのみ管理する形に変更する
    • アプリケーションディレクトリに移動することで以下が可能になる
      • モジュールの再利用
      • テスト機構の再利用(testDBなど)
  • デプロイ時はこれまで通りterraformで実施
    • main.tfの中でビルド対象のファイルを指定してzipを読み込めればOK

参考にした記事
https://dev.classmethod.jp/articles/deploy-golang-lambda-function-with-terraform/

ASIS

├── server/
│   ├── controller/
│   ~
├── terragrunt/
│   ├── envs/
│   ├── modules/
│       └── lambda
│             └─lambda_logic/ # (ここにGoのコードがあるのが運用しづらくしてい)
│               └──db.go 
│               └──go.mod
│               └──go.sum
│               └──main.go

main.tf (build部分抜粋)

# 必要なモジュールをbuildする
resource "terraform_data" "build_lambda" {

  provisioner "local-exec" {
    command = <<EOT
      # Set the target OS and architecture
      cd ${path.module}/lambda
      GOOS=linux GOARCH=amd64 go build -o bootstrap main.go db.go

      # Check if the build was successful
      if [ $? -ne 0 ]; then
        echo "Go build failed!"
        exit 1
      fi
    EOT
  }

  # 毎回ビルドする
  triggers_replace = [
    timestamp()
  ]
}

TOBE

├── server/
│   ├── controller/
│   ~
│   └── lambda/
│          └── main.tf
│          └── xxxxx/ # (lambda関数Aで使用するgoのコード群)
│                 ├── main.go
│                 ├── go.mod
│                 ├── go.sum
│                 └── db.go  # (ここに移動。テストもしやすくなる)
├── terragrunt/
│   ├── envs/
│   │   ├── dev/
│   │   ├── prod/
│   ├── modules/
│   │   └── lambda_xxxxx/
│       └── main.tf  # Lambda や IAM の作成のみを定義

main.tf
Goソースがあるディレクトリに移動する部分だけ変更

# --------------------------------------------------
# 1) Lambdaビルド (Goソースをコンパイル)
# --------------------------------------------------
# 必要なモジュールをbuildする
resource "terraform_data" "build_lambda" {

  provisioner "local-exec" {
    command = <<EOT
      # Set the target OS and architecture

      # ここを変更するだけ!!
      # server/lambdas/xxxxx ディレクトリに移動
      cd ${path.root}/../../server/lambdas/xxxxx
      
      GOOS=linux GOARCH=amd64 go build -o bootstrap main.go db.go

      # Check if the build was successful
      if [ $? -ne 0 ]; then
        echo "Go build failed!"
        exit 1
      fi
    EOT
  }

  # 毎回ビルドする
  triggers_replace = [
    timestamp()
  ]
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?