Home > Net >  How to dynamically reference a lambda function arn inside step function in aws terraform?
How to dynamically reference a lambda function arn inside step function in aws terraform?

Time:05-24

I'm creating a lambda function using an existing module. Currently I refer the static arn inside the step function definition json file. I need to refer it dynamically i.e. at runtime whatever is the arn created. Here's the code:

module "student_lambda"{
source = git@github...// Some git repo
//Other info like vpc, runtime, memory, etc
}

How can I refer this student_lambda arn in my json file for step function?

Here's the json file snippet

"Student lambda response": {
"Type":"Task",
"Resource":"arn:aws:states:::lambda:invoke",
"Parameters":{
"Payload"......// Generic code
"FunctionName":"arn:aws:lambda:us-east-2:..."// Here I want to use something like Student_Lmabda.arn


}}

Note: module is declared in main.tf file. The step function json file is in another folder.

CodePudding user response:

I am assuming the file structure is something like this.

=====================

  • main.tf
  • variables.tf
  • folder with json/ -json file
  • modules

=====================

In order for us to achieve this, we can create an output of the lambda function that we are creating within the output.tf file in the module.

    output "lambda_arn" {
        value = aws_lambda.<name of the lambda fn>.arn
    }

once this is done we can refer the variable using

"Student lambda response": {
"Type":"Task",
"Resource":"arn:aws:states:::lambda:invoke",
"Parameters":{
"Payload"......
"FunctionName":"${module.student_lambda.lambda_arn}"
}}
  • Related