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

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

问题描述

这是我之前问过的一个问题:如何获取AWS Lambda使用Terraform进行ARN吗?

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:未找到函数...".错误.

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天全站免登陆