-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sudoblark/feature/initial-module-setup
Initial module setup
- Loading branch information
Showing
12 changed files
with
265 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.5.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
resource "aws_security_group" "groups" { | ||
for_each = { for group in var.raw_security_groups : group.suffix => group } | ||
|
||
name = upper(format("aws-%s-%s-%s-SG", | ||
var.environment, | ||
var.application_name, | ||
each.value["suffix"]) | ||
) | ||
description = format("%s - Managed by Terraform", each.value["description"]) | ||
vpc_id = var.vpc_config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
locals { | ||
actual_security_group_rules = flatten([ | ||
for group in var.raw_security_groups : [ | ||
for rule in group.rules : merge(rule, { | ||
identifier : format("%s/%s", group.suffix, rule.name), | ||
security_group_id = aws_security_group.groups[group.suffix].id | ||
}) | ||
] | ||
]) | ||
} | ||
|
||
resource "aws_security_group_rule" "rule" { | ||
for_each = { for rule in local.actual_security_group_rules : rule.identifier => rule } | ||
type = each.value["type"] | ||
from_port = each.value["from_port"] | ||
to_port = each.value["to_port"] | ||
protocol = each.value["protocol"] | ||
security_group_id = each.value["security_group_id"] | ||
description = each.value["description"] | ||
|
||
cidr_blocks = try(each.value["cidr_blocks"], null) | ||
ipv6_cidr_blocks = try(each.value["ipv6_cidr_blocks"], null) | ||
prefix_list_ids = try(each.value["prefix_list_ids"], null) | ||
self = try(each.value["self"], null) | ||
source_security_group_id = try(each.value["source_security_group_id"], null) | ||
|
||
depends_on = [ | ||
aws_security_group.groups | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.5.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Get default VPC | ||
data "aws_vpc" "vpc" {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
locals { | ||
raw_security_groups = [ | ||
{ | ||
"suffix" : "lambda", | ||
"description" : "Lambda Function security group", | ||
"rules" : [ | ||
{ | ||
"name" : "All traffic from VPC", | ||
"type" : "ingress", | ||
"from_port" : "0", | ||
"to_port" : "0", | ||
"protocol" : "-1", | ||
"description" : "VPC Traffic (Ingress)", | ||
"cidr_blocks" : ["1.2.3.0/24"] | ||
}, | ||
{ | ||
"name" : "All traffic to VPC", | ||
"type" : "egress", | ||
"from_port" : "0", | ||
"to_port" : "0", | ||
"protocol" : "-1", | ||
"description" : "VPC Traffic (Egress)", | ||
"cidr_blocks" : ["1.2.3.0/24"] | ||
}, | ||
{ | ||
"name" : "Public endpoint traffic", | ||
"type" : "egress", | ||
"from_port" : "443", | ||
"to_port" : "443", | ||
"protocol" : "tcp", | ||
"description" : "Public Endpoint traffic (Egress)", | ||
"cidr_blocks" : ["0.0.0.0/0"] | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = ">= 5.61.0" | ||
} | ||
} | ||
required_version = "~> 1.5.0" | ||
} | ||
|
||
provider "aws" { | ||
region = "eu-west-2" | ||
} | ||
|
||
module "security_group" { | ||
source = "github.com/sudoblark/sudoblark.terraform.module.aws.security_group?ref=1.0.0" | ||
environment = var.environment | ||
application_name = var.application_name | ||
raw_security_groups = local.raw_security_groups | ||
vpc_config = data.aws_vpc.vpc.id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
variable "environment" { | ||
description = "Which environment this is being instantiated in." | ||
type = string | ||
validation { | ||
condition = contains(["dev", "test", "prod"], var.environment) | ||
error_message = "Must be either dev, test or prod" | ||
} | ||
default = "prod" | ||
} | ||
|
||
variable "application_name" { | ||
description = "Name of the application utilising the resource resource." | ||
type = string | ||
default = "demo-app" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = ">= 5.61.0" | ||
} | ||
} | ||
required_version = "~> 1.5.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Input variable definitions | ||
variable "environment" { | ||
description = "Which environment this is being instantiated in." | ||
type = string | ||
validation { | ||
condition = contains(["dev", "test", "prod"], var.environment) | ||
error_message = "Must be either dev, test or prod" | ||
} | ||
} | ||
|
||
variable "application_name" { | ||
description = "Name of the application utilising resource." | ||
type = string | ||
} | ||
|
||
variable "vpc_config" { | ||
description = "AWS VPC ID within which to create the security group." | ||
type = string | ||
} | ||
|
||
variable "raw_security_groups" { | ||
description = <<EOF | ||
Data structure | ||
--------------- | ||
A list of dictionaries, where each dictionary has the following attributes: | ||
REQUIRED | ||
--------- | ||
- suffix : Security group suffix to use for naming and unique identifiers | ||
- description : Description to give to the security group | ||
OPTIONAL | ||
--------- | ||
- rules: A list of dictionaries, where each dictionary has the following values: | ||
-- name : Friendly name used through Terraform for instantiation and cross-referencing | ||
-- type : Ingress/egress | ||
-- from_port : Start port | ||
-- to_port : End port | ||
-- protocol : Protocol. If not icmp, icmpv6, tcp, udp, or all use the protocol number. | ||
-- description : Friendly description of the rule, required for auditing purposes. | ||
In addition, the following optional args are available: | ||
-- cidr_blocks : List of CIDR blocks. Cannot be specified with source_security_group_id or self. | ||
-- ipv6_cidr_blocks : List of IPv6 CIDR blocks. Cannot be specified with source_security_group_id or self. | ||
-- prefix_list_ids : List of Prefix List IDs. | ||
-- self : Whether the security group itself will be added as a source to this ingress rule. Cannot be specified with cidr_blocks, ipv6_cidr_blocks, or source_security_group_id. | ||
-- source_security_group_id : Security group id to allow access to/from, depending on the type. Cannot be specified with cidr_blocks, ipv6_cidr_blocks, or self. | ||
EOF | ||
type = list( | ||
object({ | ||
suffix : string, | ||
description : string, | ||
rules : optional(list( | ||
object({ | ||
name = string, | ||
type = string, | ||
from_port = string, | ||
to_port = string, | ||
protocol = string, | ||
description = string, | ||
cidr_blocks = optional(list(string), null), | ||
ipv6_cidr_blocks = optional(list(string), null), | ||
prefix_list_ids = optional(list(string), null), | ||
self = optional(bool, null), | ||
source_security_group_id = optional(string, null) | ||
}) | ||
), []) | ||
|
||
}) | ||
) | ||
} |