<GitHub discussion={{"id":"D_kwDOF8slf84ASu1F","number":673,"author":{"login":"dbd-infra"},"title":"create multiple instances of modules","body":"\nWe want to use [sqs/sns/sqs-sns-connection modules](https://github.com/gruntwork-io/terraform-aws-messaging/tree/main/modules) to create multiple channels.\r\n\r\nThere is an example how to [use for_eac](https://terragrunt.gruntwork.io/docs/rfc/for_each_iteration/#option-2-for_each-attribute-in-terragrunt-config-that-generates-multiple-copies-of-the-config)h to create multiple copies of config.\r\nIs there a similar way for our purpose or please suggest the best way to configure it.\n\n---\n\n<ins datetime=\"2023-02-28T15:49:17Z\">\n <p><a href=\"https://support.gruntwork.io/hc/requests/109938\">Tracked in ticket #109938</a></p>\n</ins>\n","bodyHTML":"<p dir=\"auto\">We want to use <a href=\"https://github.com/gruntwork-io/terraform-aws-messaging/tree/main/modules\">sqs/sns/sqs-sns-connection modules</a> to create multiple channels.</p>\n<p dir=\"auto\">There is an example how to <a href=\"https://terragrunt.gruntwork.io/docs/rfc/for_each_iteration/#option-2-for_each-attribute-in-terragrunt-config-that-generates-multiple-copies-of-the-config\" rel=\"nofollow\">use for_eac</a>h to create multiple copies of config.<br>\nIs there a similar way for our purpose or please suggest the best way to configure it.</p>\n<hr>\n<ins datetime=\"2023-02-28T15:49:17Z\">\n <p dir=\"auto\"><a href=\"https://support.gruntwork.io/hc/requests/109938\" rel=\"nofollow\">Tracked in ticket #109938</a></p>\n</ins>","answer":{"body":"Hi @dbd-infra, you can use the Terraform `for_each` argument directly with those modules. Here's a (simplified) example:\r\n\r\nThe `locals` block here is just an example input of the `for_each` argument. You could also set this through variables\r\n\r\nThis example will create 3 sets of an SQS queue, SNS topic, and connection.\r\n\r\n```\r\nlocals {\r\n to_create = toset([\"1\",\"2\",\"3\"])\r\n}\r\n\r\nmodule \"sqs\" {\r\n\r\n for_each = local.to_create\r\n\r\n source = \"git::
[email protected]:gruntwork-io/terraform-aws-messaging.git//modules/sqs?ref=v0.9.1\"\r\n\r\n name = each.key\r\n}\r\n\r\nmodule \"sns\" {\r\n\r\n for_each = local.to_create\r\n\r\n source = \"git::
[email protected]:gruntwork-io/terraform-aws-messaging.git//modules/sns?ref=v0.9.1\"\r\n\r\n name = each.key\r\n}\r\n\r\nmodule \"connection\" {\r\n\r\n for_each = local.to_create\r\n\r\n source = \"git::
[email protected]:gruntwork-io/terraform-aws-messaging.git//modules/sns-sqs-connection?ref=v0.9.1\"\r\n\r\n sns_topic_arn = module.sns[each.key].topic_arn\r\n sqs_arn = module.sqs[each.key].queue_arn\r\n sqs_queue_url = module.sqs[each.key].queue_url\r\n}\r\n```\r\n","bodyHTML":"<p dir=\"auto\">Hi <a class=\"user-mention notranslate\" data-hovercard-type=\"user\" data-hovercard-url=\"/users/dbd-infra/hovercard\" data-octo-click=\"hovercard-link-click\" data-octo-dimensions=\"link_type:self\" href=\"https://github.com/dbd-infra\">@dbd-infra</a>, you can use the Terraform <code class=\"notranslate\">for_each</code> argument directly with those modules. Here's a (simplified) example:</p>\n<p dir=\"auto\">The <code class=\"notranslate\">locals</code> block here is just an example input of the <code class=\"notranslate\">for_each</code> argument. You could also set this through variables</p>\n<p dir=\"auto\">This example will create 3 sets of an SQS queue, SNS topic, and connection.</p>\n<div class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"locals {\n to_create = toset(["1","2","3"])\n}\n\nmodule "sqs" {\n\n for_each = local.to_create\n\n source = "git::
[email protected]:gruntwork-io/terraform-aws-messaging.git//modules/sqs?ref=v0.9.1"\n\n name = each.key\n}\n\nmodule "sns" {\n\n for_each = local.to_create\n\n source = "git::
[email protected]:gruntwork-io/terraform-aws-messaging.git//modules/sns?ref=v0.9.1"\n\n name = each.key\n}\n\nmodule "connection" {\n\n for_each = local.to_create\n\n source = "git::
[email protected]:gruntwork-io/terraform-aws-messaging.git//modules/sns-sqs-connection?ref=v0.9.1"\n\n sns_topic_arn = module.sns[each.key].topic_arn\n sqs_arn = module.sqs[each.key].queue_arn\n sqs_queue_url = module.sqs[each.key].queue_url\n}\"><pre class=\"notranslate\"><code class=\"notranslate\">locals {\n to_create = toset([\"1\",\"2\",\"3\"])\n}\n\nmodule \"sqs\" {\n\n for_each = local.to_create\n\n source = \"git::
[email protected]:gruntwork-io/terraform-aws-messaging.git//modules/sqs?ref=v0.9.1\"\n\n name = each.key\n}\n\nmodule \"sns\" {\n\n for_each = local.to_create\n\n source = \"git::
[email protected]:gruntwork-io/terraform-aws-messaging.git//modules/sns?ref=v0.9.1\"\n\n name = each.key\n}\n\nmodule \"connection\" {\n\n for_each = local.to_create\n\n source = \"git::
[email protected]:gruntwork-io/terraform-aws-messaging.git//modules/sns-sqs-connection?ref=v0.9.1\"\n\n sns_topic_arn = module.sns[each.key].topic_arn\n sqs_arn = module.sqs[each.key].queue_arn\n sqs_queue_url = module.sqs[each.key].queue_url\n}\n</code></pre></div>"}}} />
0 commit comments