Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
27
Help us understand the problem. What is going on with this article?
@Pampus

TerraformでLambdaをデプロイするときの小技

More than 3 years have passed since last update.

TerraformでLambdaをデプロイするとき、ローカルでzipファイルを作成しておくか、あらかじめS3にアップしておく必要があります。

resource "aws_lambda_function" "sample_function" {
  filename         = "lambda/upload/sample_function.zip"
  function_name    = "sample_function"
  role             = "${aws_iam_role.lambda_sample_function.arn}"
  handler          = "lambda_function.lambda_handler"
  source_code_hash = "${base64sha256(file("lambda/sample_function/upload.zip"))}"
  runtime          = "python3.6"

  memory_size = 128
  timeout     = 30
}

手動、もしくはなんらかのツールでzipを作成してもいいのですがArchive Providerを使用すればTerraformがZIPファイルを作成してくれるようになります。
https://www.terraform.io/docs/providers/archive/d/archive_file.html

data "archive_file" "sample_function" {
  type        = "zip"
  source_dir  = "lambda/sample_function"
  output_path = "lambda/upload/sample_function.zip"
}

resource "aws_lambda_function" "sample_function" {
  filename         = "${data.archive_file.sample_function.output_path}"
  function_name    = "sample_function"
  role             = "${aws_iam_role.lambda_sample_function.arn}"
  handler          = "lambda_function.lambda_handler"
  source_code_hash = "${data.archive_file.sample_function.output_base64sha256}"
  runtime          = "python3.6"

  memory_size = 128
  timeout     = 30
}

Lambdaのコードを変更してterraform planを実行するとzipが作成され差分が出るのでapplyでそのままデプロイできます。

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  ~ aws_lambda_function.sample_function
      last_modified:    "2018-04-13T06:00:00.000+0000" => <computed>
      source_code_hash: "C2gsFZgweCbQ88wjZk9+TmH+Beo+/FDPTrkRIn8mEfg=" => "PCJ/UxjA1evwRUUwxD9OI5mXWXWEmfSShCKC8LcaDD/Ak="


Plan: 0 to add, 1 to change, 0 to destroy.
27
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Pampus
cloudpack
Amazon Web Services (AWS) の導入設計、環境構築、運用・保守をサポートするマネジドホスティングサービス

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
27
Help us understand the problem. What is going on with this article?