ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

HCL Dynamic Resource Creation with `for_each`

HCL

Goal -- WPM

Ready
Exercise Algorithm Area
1variable "sqs_queues" {
2description = "A map of SQS queue configurations."
3type = map(object({
4name = string
5delay_seconds = optional(number, 0)
6max_message_size = optional(number, 262144) # 256 KiB
7visibility_timeout = optional(number, 30)
8}))
9
10validation {
11condition = length(var.sqs_queues) > 0
12error_message = "At least one SQS queue configuration must be provided."
13}
14
15validation {
16condition = alltrue([for config in values(var.sqs_queues) : config.max_message_size >= 1024 && config.max_message_size <= 262144])
17error_message = "Max message size must be between 1024 bytes and 262144 bytes."
18}
19}
20
21resource "aws_sqs_queue" "app_queues" {
22for_each = var.sqs_queues
23
24name = each.value.name
25delay_seconds = each.value.delay_seconds
26max_message_size = each.value.max_message_size
27visibility_timeout_seconds = each.value.visibility_timeout
28message_retention_seconds = 345600 # 4 days
29receive_wait_time_seconds = 0
30redrive_policy = jsonencode({
31deadLetterTargetArn = "arn:aws:sqs:us-east-1:123456789012:dead-letter-queue"
32maxReceiveCount = 5
33})
34
35tags = {
36ManagedBy = "Terraform"
37QueueName = each.key
38}
39}
40
41output "sqs_queue_urls" {
42description = "Map of SQS queue names to their URLs."
43value = {
44for key, queue in aws_sqs_queue.app_queues : key => queue.id
45}
46}
Algorithm description viewbox

HCL Dynamic Resource Creation with `for_each`

Algorithm description:

This HCL code uses the `for_each` meta-argument to dynamically create multiple AWS SQS queues. The `aws_sqs_queue.app_queues` resource iterates over the `var.sqs_queues` map, creating a separate SQS queue for each entry. The configuration includes input validation to ensure queue names are provided and message sizes are within the acceptable range. This pattern is highly effective for managing collections of similar resources.

Algorithm explanation:

The HCL code leverages the `for_each` meta-argument to provision multiple SQS queues based on a map variable `var.sqs_queues`. `for_each` iterates over the keys of the map, and for each key-value pair, it creates an instance of the `aws_sqs_queue` resource. The `each.key` and `each.value` special variables are used within the resource block to access the current key and its corresponding configuration object. Input validation is performed on the `var.sqs_queues` to ensure at least one queue is defined and that the `max_message_size` falls within the valid AWS range (1024 to 262144 bytes). This approach promotes code reusability and reduces duplication when managing numerous similar resources. The time and space complexity are proportional to the number of queues being created, as each queue is a distinct resource instance.

Pseudocode:

variable sqs_queues of type map:
  each element is an object with:
    name (string)
    delay_seconds (optional number, default 0)
    max_message_size (optional number, default 262144)
    visibility_timeout (optional number, default 30)

validate sqs_queues:
  check sqs_queues is not empty
  check all max_message_size are between 1024 and 262144

for each queue_config in sqs_queues:
  create aws_sqs_queue with:
    name = queue_config.name
    delay_seconds = queue_config.delay_seconds
    max_message_size = queue_config.max_message_size
    visibility_timeout_seconds = queue_config.visibility_timeout