-1

I'm trying to create a definition for AWS state machine generating it from this list of object:

state_object_list = [
  {
      name = "task1",
      type = "Task",
      resource = "arn:aws:lambda:us-east-1:123456789:function:test",
      end = false
  },
  {
      name = "task2",
      type = "Task",
      resource = "arn:aws:lambda:us-east-1:1234567890:function:test2",
      end = true
  }
]

I would like to have something similar to this, so with the possibility to define dinamically the key and the values of each state.

resource "aws_sfn_state_machine" "this" {
  name     = "my-state"
  role_arn = "my-role"

    definition = jsonencode({
    Comment = var.definition_comment
    StartAt = var.definition_startat

    States = {
      for state in var.state_object_list: {
        state.name = {
            Type = state.type
            Resource = state.resource
            End = state.end
        }
      }
    }
  })
}

Is it possible? Thank you for the help.

Jenkinz
  • 53
  • 5
  • Yes, use a dynamic block https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks – Paolo Apr 03 '23 at 15:20
  • Could you please give an example? I tried before to use it and I'm still trying to use it, but it always give me errors. p.s. I already used dynamic block for other resources, but here it doesn't works for me. Thank you – Jenkinz Apr 03 '23 at 15:50
  • What error are you seeing? – Paolo Apr 03 '23 at 16:13
  • If I put it in the "States" row: dynamic "states" { Expected an equals sign ("=") to mark the beginning of the attribute value. – Jenkinz Apr 03 '23 at 16:17

2 Answers2

1

Can you explain why you want this? The state machine definition follows JSON format so putting your JSON object into the definition=jsonencode({...}) with minor adjustments to follow the Amazon States Language syntax should work and doesn't create more complexity in the code. Maybe I missed your point.

tailaiw
  • 26
  • 4
0

Easiest way to accomplish this issue to create a local variable then use it in your resource.

variables.tf


variable "state_object_list" {
  default = [
    {
      name     = "task1",
      type     = "Task",
      resource = "arn:aws:lambda:us-east-1:123456789:function:test",
      end      = false
    },
    {
      name     = "task2",
      type     = "Task",
      resource = "arn:aws:lambda:us-east-1:1234567890:function:test2",
      end      = true
    }
  ]
}

variable "definition_comment" {
    type = string
    default = "definition comment"
}

variable "definition_startat" {
    type = string
    default = "definition state"
}

main.tf


locals {
  states = {
    for state in var.state_object_list: state.name => {
      Type = state.type
      Resource = state.resource
      End = state.end
    }
  }
}

resource "aws_sfn_state_machine" "this" {
  name     = "my-state"
  role_arn = "arn:aws:iam::123456789098:role/my-role"

    definition = jsonencode({
      Comment = var.definition_comment
      StartAt = var.definition_startat
      States = local.states
   })
}
Mehmet Güngören
  • 2,383
  • 1
  • 9
  • 16