From 2abd6a37d74cfeed5e57620cdbbdb2449b038354 Mon Sep 17 00:00:00 2001 From: Garland Kan Date: Thu, 27 Jan 2022 15:55:46 -0800 Subject: [PATCH] Aws sg (#246) --- .../aws/security_groups/README.md | 26 +++++++++++++++++++ terraform-modules/aws/security_groups/main.tf | 2 +- .../aws/security_groups/outputs.tf | 7 +++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 terraform-modules/aws/security_groups/README.md diff --git a/terraform-modules/aws/security_groups/README.md b/terraform-modules/aws/security_groups/README.md new file mode 100644 index 000000000..fb7868689 --- /dev/null +++ b/terraform-modules/aws/security_groups/README.md @@ -0,0 +1,26 @@ +# AWS Security Group +This creates a set of security groups that can be used on other items. + +## Retrieving the security group by name: + +The `sg_list` output data structure: +``` +sg_list = { + "id" = [ + "sg-0978d62cacw3e8b21", + "sg-06f385f8d8w319d59", + "sg-033cc7d494w3cbe47", + "sg-0071a0c41bwaea18e", + ] + "name" = [ + "dev-foo", + "dev-app", + "dev-bar", + "dev-ami", + ] +``` + +Can use the `index` function to find an index by the name: +``` +module.security_groups.sg_list["id"][index(module.security_groups.sg_list["name"], "dev-app")] +``` \ No newline at end of file diff --git a/terraform-modules/aws/security_groups/main.tf b/terraform-modules/aws/security_groups/main.tf index 123db989f..e14bd6dd2 100644 --- a/terraform-modules/aws/security_groups/main.tf +++ b/terraform-modules/aws/security_groups/main.tf @@ -3,7 +3,7 @@ resource "aws_security_group" "sg" { count = length(var.security_groups) name = var.security_groups[count.index].name vpc_id = var.vpc_id - tags = var.security_groups[count.index].tags + tags = merge(var.security_groups[count.index].tags, {Name=var.security_groups[count.index].name}) } // loop through the security groups to create the security group rules diff --git a/terraform-modules/aws/security_groups/outputs.tf b/terraform-modules/aws/security_groups/outputs.tf index 654181c40..5c22a4c37 100644 --- a/terraform-modules/aws/security_groups/outputs.tf +++ b/terraform-modules/aws/security_groups/outputs.tf @@ -9,3 +9,10 @@ output "security_group_arn_list" { output "security_group_name_list" { value = aws_security_group.sg.*.name } + +output "sg_list" { + value = { + name = aws_security_group.sg[*].name + id = aws_security_group.sg[*].id + } +}