1
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?

More than 1 year has passed since last update.

TerraformでLambdaEdgeをCloudFrontに登録

Last updated at Posted at 2022-06-21

概要

  • TerraformでCloudFrontにLambdaEdgeの登録をしたい
  • Terraform公式にあるやり方でも他記事でもうまくいかない。結構はまった。
  • 他にも困っている人向けに
  • Terraformバージョンアップ早いのですぐ変更される可能性あり

詳細

  • 成功イメージ
  • エラー
    • Lambdaのarnはバージョンが必要である旨のメッセージ
    • そんなことは公式マニュアルに書いてないはず
Error: error updating CloudFront Distribution (ET80HP6KGL1W5): InvalidLambdaFunctionAssociation: The function ARN must reference a specific function version. (The ARN must end with the version number.) ARN: arn:aws:lambda:us-east-1:121212121212:function:dev-lambdaedge-cloudfront:$LATEST
  • LambdaEdge側
lambdaedge.tf
resource "aws_lambda_function" "dev_lambdaedge_cloudfront" {
  filename      = "dev-lambdaedge-cloudfront.zip"
  function_name = "dev-lambdaedge-cloudfront"
  role          = aws_iam_role.dev_iam_role_lambda_edge.arn
  handler       = "index.handler"
  provider      = aws.virginia
  publish       = true #PublishされたLambdaしか連携できないので注意
  source_code_hash = filebase64sha256("dev-lambdaedge-cloudfront.zip")
  runtime = "nodejs14.x"
  # 環境変数が中途半端?にあるとCloudFront側でエラー(""The function cannot have environment variables.")
  #environment {
  # variables = {
  #               project = "hoge"
  #  }
  # }
}
  • CloudFront側
cloudfront.tf
resource "aws_cloudfront_distribution" "dev_cloudfront_distribution_admin" {

  //オリジン設定
  origin {

  //ビヘイビア設定
  default_cache_behavior {
    :
  
    lambda_function_association {
      event_type = "origin-request"
      #lambda_arn = aws_lambda_function.dev_lambdaedge_cloudfront.arn #エラーになる
      #lambda_arn = aws_lambda_function.dev_lambdaedge_cloudfront.qualified_arn #エラーになる
      //以下のようにバージョンを明示的に記載すると成功する。数値をベタ書きでも良い。
      lambda_arn   = "${aws_lambda_function.dev_lambdaedge_cloudfront.arn}:${aws_lambda_function.dev_lambdaedge_cloudfront.version}"
      include_body = false
    }
  }
  • 上記のような記述条件を満たし"terraform apply"すれば成功
1
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
1
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?