有没有办法在 terraform 脚本中检索图层的最新版本?

Is there a way, inside a terraform script, to retrieve the latest version of a layer?

我有引用一个层的 lambda,这个层由其他人维护,当一个新的 版本已发布我必须更新我的 terraform 代码以将最新版本放入 arn(此处为 19)。

有没有办法在 terraform 脚本中获取最新版本并使用它?

module "lambda_function" {
  source = "terraform-aws-modules/lambda/aws"

  function_name = "my-lambda1"
  description   = "My awesome lambda function"
  handler       = "index.lambda_handler"
  runtime       = "python3.8"

  source_path = "../src/lambda-function1"

  tags = {
    Name = "my-lambda1"
  }
  
  layers = [
    "arn:aws:lambda:eu-central-1:587522145896:layer:my-layer-name:19"
  ]
}

谢谢。

ps : 这意味着图层的 terraform 脚本不在我的脚本中,它是我无权访问的其他脚本。

您可以使用 aws_lambda_layer_version data source 来发现最新版本。

例如:

module "lambda_function" {
  source = "terraform-aws-modules/lambda/aws"

  function_name = "my-lambda1"
  description   = "My awesome lambda function"
  handler       = "index.lambda_handler"
  runtime       = "python3.8"

  source_path = "../src/lambda-function1"

  tags = {
    Name = "my-lambda1"
  }
  
  layers = [
    data.aws_lambda_layer_version.layer_version.arn
  ]
}

data "aws_lambda_layer_version" "layer_version" {
  layer_name = "my-layer-name"
}