创建后如何构建 terraform 代码以获取 Lambda ARN? [英] How to structure terraform code to get Lambda ARN after creation?

查看:22
本文介绍了创建后如何构建 terraform 代码以获取 Lambda ARN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前提出的一个问题:如何获取 AWS LambdaARN 使用 Terraform?

This was a previous question I asked: How to get AWS Lambda ARN using Terraform?

这个问题得到了回答,但实际上并没有解决我的问题,所以这是一个跟进.

This question was answered but turns out didn't actually solve my problem so this is a follow up.

我编写的 terraform 代码提供了一个 Lambda 函数:

The terraform code I have written provisions a Lambda function:

根模块:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
    }
  }
}

provider "aws" {
   region = var.region
   profile = var.aws_profile
}

module "aws_lambda_function" {
   source = "./modules/lambda_function"
}

子模块:

resource "aws_lambda_function" "lambda_function" {
   function_name = "lambda_function"
   handler = "lambda_function.lambda_handler"
   runtime = "python3.8"
   filename = "./task/dist/package.zip"
   role = aws_iam_role.lambda_exec.arn
}

resource "aws_iam_role" "lambda_exec" {
   name = "aws_iam_lambda"
   assume_role_policy = file("policy.json")
}

我希望用户能够做什么来获得 Lambda ARN:

What I want the user to be able to do to get the Lambda ARN:

terraform output

问题:我似乎无法在 terraform 代码中的任何位置包含以下代码,因为它会导致ResourceNotFOundException: Function not found..."错误.

The problem: I cannot seem to include the following code anywhere in my terraform code as it causes a "ResourceNotFOundException: Function not found..." error.

data "aws_lambda_function" "existing" {
  function_name = "lambda_function"
}

output "arn" {
  value = data.aws_lambda_function.existing.arn
}

我需要在哪里或如何包含此信息才能获得 ARN,或者这可能吗?

Where or how do I need to include this to be able to get the ARN or is this possible?

推荐答案

您不能同时查找您正在创建的资源的 data.您需要从模块中输出 ARN,然后从主 terraform 模板中再次输出.

You can't lookup the data for a resource you are creating at the same time. You need to output the ARN from the module, and then output it again from the main terraform template.

在您的 Lambda 模块中:

In your Lambda module:

output "arn" {
  value = aws_lambda_function.lambda_function.arn
}

然后在你的主文件中:

output "arn" {
  value = module.aws_lambda_function.arn
}

这篇关于创建后如何构建 terraform 代码以获取 Lambda ARN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆