Skip to content

Instantly share code, notes, and snippets.

@KernelPanicAUS
Last active July 31, 2020 13:40
Show Gist options
  • Save KernelPanicAUS/a5bd52e3fc51a09f108922e4c62dc340 to your computer and use it in GitHub Desktop.
Save KernelPanicAUS/a5bd52e3fc51a09f108922e4c62dc340 to your computer and use it in GitHub Desktop.

Revisions

  1. KernelPanicAUS revised this gist Jul 31, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions main.tf
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ data "aws_vpc" "selected" {
    }

    locals {
    ingress_map = [
    ingress_rules = [
    {
    description = "test-one",
    from_port = 443,
    @@ -55,7 +55,7 @@ resource "aws_security_group" "test_two" {

    resource "aws_security_group_rule" "test_two_rules" {
    for_each = {
    for rule in local.ingress_map :
    for rule in local.ingress_rules :
    "${rule.description}-${rule.protocol}" => rule
    }

  2. KernelPanicAUS revised this gist Jul 30, 2020. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions main.tf
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    ```hcl
    terraform {
    required_version = ">= 0.12"

    @@ -68,4 +67,3 @@ resource "aws_security_group_rule" "test_two_rules" {
    cidr_blocks = lookup(each.value, "cidr_blocks")
    security_group_id = aws_security_group.test_two.id
    }
    ```
  3. KernelPanicAUS revised this gist Jul 30, 2020. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion main.tf
    Original file line number Diff line number Diff line change
    @@ -55,7 +55,11 @@ resource "aws_security_group" "test_two" {
    }

    resource "aws_security_group_rule" "test_two_rules" {
    for_each = { for rule in local.ingress_map : "${rule.description}-${rule.protocol}" => rule }
    for_each = {
    for rule in local.ingress_map :
    "${rule.description}-${rule.protocol}" => rule
    }

    type = "ingress"
    description = lookup(each.value, "description")
    from_port = lookup(each.value, "from_port")
  4. KernelPanicAUS created this gist Jul 30, 2020.
    67 changes: 67 additions & 0 deletions main.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    ```hcl
    terraform {
    required_version = ">= 0.12"

    required_providers {
    aws = "~> 2.0"
    }
    }

    provider "aws" {
    region = "eu-central-1"
    }

    data "aws_vpc" "selected" {
    default = true
    }

    locals {
    ingress_map = [
    {
    description = "test-one",
    from_port = 443,
    to_port = 443,
    protocol = "tcp"
    cidr_blocks = [data.aws_vpc.selected.cidr_block]
    },
    {
    description = "test-two",
    from_port = 444,
    to_port = 444,
    protocol = "tcp"
    cidr_blocks = [data.aws_vpc.selected.cidr_block]
    },
    {
    description = "test-three",
    from_port = 445,
    to_port = 445,
    protocol = "tcp"
    cidr_blocks = [data.aws_vpc.selected.cidr_block]
    },
    {
    description = "test-four",
    from_port = 446,
    to_port = 446,
    protocol = "tcp"
    cidr_blocks = [data.aws_vpc.selected.cidr_block]
    }
    ]
    }

    resource "aws_security_group" "test_two" {
    name = "test_two"
    description = "Allow inbound traffic"
    vpc_id = data.aws_vpc.selected.id
    }

    resource "aws_security_group_rule" "test_two_rules" {
    for_each = { for rule in local.ingress_map : "${rule.description}-${rule.protocol}" => rule }
    type = "ingress"
    description = lookup(each.value, "description")
    from_port = lookup(each.value, "from_port")
    to_port = lookup(each.value, "to_port")
    protocol = lookup(each.value, "protocol")
    cidr_blocks = lookup(each.value, "cidr_blocks")
    security_group_id = aws_security_group.test_two.id
    }
    ```