Skip to content

Instantly share code, notes, and snippets.

@max-rocket-internet
Last active January 26, 2022 16:03
Show Gist options
  • Select an option

  • Save max-rocket-internet/0b6955a80b0fab1b3d3fe81f8953c0a2 to your computer and use it in GitHub Desktop.

Select an option

Save max-rocket-internet/0b6955a80b0fab1b3d3fe81f8953c0a2 to your computer and use it in GitHub Desktop.

Revisions

  1. max-rocket-internet revised this gist Oct 8, 2021. 1 changed file with 36 additions and 12 deletions.
    48 changes: 36 additions & 12 deletions terraform-iteration.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Iteration in Terraform 0.12
    # Iteration in Terraform 0.12+

    All of these examples allow for an item to be removed without recreation of any other resources.

    @@ -83,7 +83,6 @@ resource "aws_ecr_repository_policy" "ecr" {
    ## Double iteration

    ```hcl
    variable "data" {
    type = map
    default = {
    @@ -96,14 +95,14 @@ variable "data" {
    ]
    }
    porygon = {
    enabled = true
    enabled = true
    countries = [
    "za",
    "sa",
    ]
    }
    rider_transmission = {
    enabled = true
    enabled = true
    countries = [
    "ca",
    ]
    @@ -134,14 +133,43 @@ resource "aws_sns_topic" "topic" {
    }
    ```

    ## Iteration over map with condition

    ```hcl
    variable "data" {
    type = map(any)
    default = {
    service1 = {
    id = "abcd"
    create_sns = true
    }
    service2 = {
    id = "efghi"
    }
    service3 = {
    id = "jklmno"
    }
    }
    }
    resource "aws_sns_topic" "topic" {
    for_each = {
    for k, v in var.data : k => v
    if lookup(v, "create_sns", false)
    }
    name = each.key
    }
    ```

    ## Complex iteration

    ```hcl
    variable "data" {
    type = any
    default = {
    dispatch_service = {
    enabled = false
    enabled = false
    countries = [
    "at",
    "fi",
    @@ -161,8 +189,8 @@ variable "data" {
    }
    rider_transmission = {
    enabled = true
    sqs_delay_seconds = 90
    enabled = true
    sqs_delay_seconds = 90
    cloudwatch_settings = {
    threshold_2 = 777
    }
    @@ -268,8 +296,4 @@ resource "aws_cloudwatch_metric_alarm" "three" {
    alarm_description = "This metric monitors ec2 cpu utilization"
    insufficient_data_actions = []
    }
    ```




    ```
  2. max-rocket-internet created this gist Jan 22, 2020.
    275 changes: 275 additions & 0 deletions terraform-iteration.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,275 @@
    # Iteration in Terraform 0.12

    All of these examples allow for an item to be removed without recreation of any other resources.

    ## Basic iteration 1

    ```hcl
    variable "data" {
    type = map
    default = {
    test1 = {
    display_name = "some_string1"
    }
    test2 = {
    display_name = "some_string2"
    }
    test3 = {
    display_name = "some_string3"
    }
    }
    }
    resource "aws_sns_topic" "topic" {
    for_each = var.data
    name = each.key
    display_name = each.value["display_name"]
    }
    ```

    ## Basic iteration 2

    ```hcl
    variable "my_lifecycle_policy" {
    type = string
    default = <<EOF
    {
    "rules": [
    {
    "rulePriority": 1,
    "description": "Keep last 1000 images",
    "selection": {
    "tagStatus": "any",
    "countType": "imageCountMoreThan",
    "countNumber": 1000
    },
    "action": {
    "type": "expire"
    }
    }
    ]
    }
    EOF
    }
    variable "ecr_repos" {
    type = list(string)
    default = [
    "my-app-1",
    "my-app-2",
    "my-app-3",
    ]
    }
    resource "aws_ecr_lifecycle_policy" "ecr" {
    for_each = toset(var.ecr_repos)
    repository = aws_ecr_repository.ecr[each.key].name
    policy = var.my_lifecycle_policy
    }
    resource "aws_ecr_repository" "ecr" {
    for_each = toset(var.ecr_repos)
    name = each.key
    }
    resource "aws_ecr_repository_policy" "ecr" {
    for_each = toset(var.ecr_repos)
    repository = aws_ecr_repository.ecr[each.key].name
    policy = data.aws_iam_policy_document.my_ecr_pull_policy.json
    }
    ```

    ## Double iteration

    ```hcl
    variable "data" {
    type = map
    default = {
    dispatch_service = {
    enabled = true
    countries = [
    "at",
    "fi",
    "no"
    ]
    }
    porygon = {
    enabled = true
    countries = [
    "za",
    "sa",
    ]
    }
    rider_transmission = {
    enabled = true
    countries = [
    "ca",
    ]
    }
    }
    }
    locals {
    app_country_pairs = flatten(
    [
    for key, value in var.data : [
    for country in value["countries"] : {
    name = format("%s_%s", key, country)
    enabled = value["enabled"]
    }
    ]
    ]
    )
    resources = {
    for obj in local.app_country_pairs : "${obj.name}" => obj.enabled
    }
    }
    resource "aws_sns_topic" "topic" {
    for_each = local.resources
    name = each.key
    }
    ```

    ## Complex iteration

    ```hcl
    variable "data" {
    type = any
    default = {
    dispatch_service = {
    enabled = false
    countries = [
    "at",
    "fi",
    "no"
    ]
    }
    porygon = {
    enabled = true
    cloudwatch_settings = {
    threshold_1 = 999
    }
    countries = [
    "za",
    "sa",
    ]
    }
    rider_transmission = {
    enabled = true
    sqs_delay_seconds = 90
    cloudwatch_settings = {
    threshold_2 = 777
    }
    countries = [
    "ca",
    "us",
    "mx"
    ]
    extra_tags = {
    extra_tag = "my_value"
    }
    }
    }
    }
    locals {
    cloudwatch_defaults = {
    threshold_1 = 5
    threshold_2 = 10
    threshold_3 = 20
    }
    app_country_pairs = flatten(
    [
    for key, value in var.data : [
    for country in value["countries"] : {
    resource_name = format("%s_%s", key, country)
    app = key
    country = country
    enabled = value["enabled"]
    cloudwatch_settings = merge(local.cloudwatch_defaults, lookup(value, "cloudwatch_settings", {}))
    }
    ]
    ]
    )
    resources = {
    for obj in local.app_country_pairs : "${obj.resource_name}" => {
    cloudwatch_settings = obj.cloudwatch_settings
    app = obj.app
    country = obj.country
    } if obj.enabled
    }
    }
    resource "aws_sns_topic" "topic" {
    for_each = local.resources
    name = each.key
    }
    resource "aws_sqs_queue" "terraform_queue" {
    for_each = local.resources
    name = each.key
    delay_seconds = lookup(var.data[each.value["app"]], "sqs_delay_seconds", 120)
    tags = merge(
    lookup(var.data[each.value["app"]], "extra_tags", {}),
    {
    "app" = each.value["app"]
    "country" = each.value["country"]
    }
    )
    }
    resource "aws_cloudwatch_metric_alarm" "one" {
    for_each = local.resources
    alarm_name = "${each.key}_one"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "2"
    metric_name = "CPUUtilization"
    namespace = "AWS/EC2"
    period = "120"
    statistic = "Average"
    threshold = each.value["cloudwatch_settings"]["threshold_1"]
    alarm_description = "This metric monitors ec2 cpu utilization"
    insufficient_data_actions = []
    }
    resource "aws_cloudwatch_metric_alarm" "two" {
    for_each = local.resources
    alarm_name = "${each.key}_two"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "2"
    metric_name = "CPUUtilization"
    namespace = "AWS/EC2"
    period = "120"
    statistic = "Average"
    threshold = each.value["cloudwatch_settings"]["threshold_2"]
    alarm_description = "This metric monitors ec2 cpu utilization"
    insufficient_data_actions = []
    }
    resource "aws_cloudwatch_metric_alarm" "three" {
    for_each = local.resources
    alarm_name = "${each.key}_three"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "2"
    metric_name = "CPUUtilization"
    namespace = "AWS/EC2"
    period = "120"
    statistic = "Average"
    threshold = each.value["cloudwatch_settings"]["threshold_3"]
    alarm_description = "This metric monitors ec2 cpu utilization"
    insufficient_data_actions = []
    }
    ```