Home > database >  Is it possible to insert a value into a file before it gets used in a terraform resource?
Is it possible to insert a value into a file before it gets used in a terraform resource?

Time:06-16

I'm deploying an Amazon Connect instance with an attached contact flow, following this documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/connect_contact_flow

My contact flow is stored in a file, so I'm using the following:

resource "aws_connect_contact_flow" "general" {
    instance_id = aws_connect_instance.dev.id
    name        = "General"
    description = "General Flow routing customers to queues"
    filename     = "flows/contact_flow.json"
    content_hash = filebase64sha256("flows/contact_flow.json")
}

However my contact flow requires me to specify the ARN of an AWS Lambda function in one particular section:

...

"parameters":[
   {
          "name":"FunctionArn",
          "value":"<lambda function arn>",
          "namespace":null
    },
    {
          "name":"TimeLimit",
          "value":"3"
    }
],

</snip>

...

the value I would like to substitute into the json file at <lambda function arn> before the contact flow is created is accessible at

data.terraform_remote_state.remote.outputs.lambda_arn

Is there any way to achieve this? Or will I have to use the 'content = ' method in the documentation linked above to achieve what I need?

Thanks.

CodePudding user response:

If you really want to use filename instead of content, you have to write the rendered template to some temporary file using local_file.

But using content with templatefile directly for that would probably be easier. For that you would have to convert your flows/contact_flow.json into a template format:

"parameters":[
   {
          "name":"FunctionArn",
          "value":"$arn",
          "namespace":null
    },
    {
          "name":"TimeLimit",
          "value":"3"
    }
],

then, for example:

locals {

  contact_flow = templatefile("flows/contact_flow.json", {
                   arn = data.terraform_remote_state.remote.outputs.lambda_arn
                 })
}


resource "aws_connect_contact_flow" "general" {
    instance_id = aws_connect_instance.dev.id
    name        = "General"
    description = "General Flow routing customers to queues"
    content     = local.contact_flow
}
  • Related