Skip to content

Commit d271a8c

Browse files
authored
feat: manage default security group (#382)
1 parent 64cdab0 commit d271a8c

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ These types of resources are supported:
1919
* [VPC Flow Log](https://www.terraform.io/docs/providers/aws/r/flow_log.html)
2020
* [VPC Endpoint](https://www.terraform.io/docs/providers/aws/r/vpc_endpoint.html):
2121
* Gateway: S3, DynamoDB
22-
* Interface: EC2, SSM, EC2 Messages, SSM Messages, SQS, ECR API, ECR DKR, API Gateway, KMS,
23-
ECS, ECS Agent, ECS Telemetry, SES, SNS, STS, Glue, CloudWatch(Monitoring, Logs, Events),
24-
Elastic Load Balancing, CloudTrail, Secrets Manager, Config, CodeBuild, CodeCommit,
25-
Git-Codecommit, Transfer Server, Kinesis Streams, Kinesis Firehose, SageMaker(Notebook, Runtime, API),
22+
* Interface: EC2, SSM, EC2 Messages, SSM Messages, SQS, ECR API, ECR DKR, API Gateway, KMS,
23+
ECS, ECS Agent, ECS Telemetry, SES, SNS, STS, Glue, CloudWatch(Monitoring, Logs, Events),
24+
Elastic Load Balancing, CloudTrail, Secrets Manager, Config, CodeBuild, CodeCommit,
25+
Git-Codecommit, Transfer Server, Kinesis Streams, Kinesis Firehose, SageMaker(Notebook, Runtime, API),
2626
CloudFormation, CodePipeline, Storage Gateway, AppMesh, Transfer, Service Catalog, AppStream,
2727
Athena, Rekognition, Elastic File System (EFS), Cloud Directory, Elastic Beanstalk (+ Health), Elastic Map Reduce(EMR),
2828
DataSync, EBS, SMS, Elastic Inference Runtime, QLDB Session, Step Functions, Access Analyzer, Auto Scaling Plans,
29-
Application Auto Scaling, Workspaces, ACM PCA.
29+
Application Auto Scaling, Workspaces, ACM PCA.
3030

3131
* [RDS DB Subnet Group](https://www.terraform.io/docs/providers/aws/r/db_subnet_group.html)
3232
* [ElastiCache Subnet Group](https://www.terraform.io/docs/providers/aws/r/elasticache_subnet_group.html)
@@ -316,6 +316,10 @@ It is possible to integrate this VPC module with [terraform-aws-transit-gateway
316316
| default\_network\_acl\_ingress | List of maps of ingress rules to set on the Default Network ACL | `list(map(string))` | <pre>[<br> {<br> "action": "allow",<br> "cidr_block": "0.0.0.0/0",<br> "from_port": 0,<br> "protocol": "-1",<br> "rule_no": 100,<br> "to_port": 0<br> },<br> {<br> "action": "allow",<br> "from_port": 0,<br> "ipv6_cidr_block": "::/0",<br> "protocol": "-1",<br> "rule_no": 101,<br> "to_port": 0<br> }<br>]</pre> | no |
317317
| default\_network\_acl\_name | Name to be used on the Default Network ACL | `string` | `""` | no |
318318
| default\_network\_acl\_tags | Additional tags for the Default Network ACL | `map(string)` | `{}` | no |
319+
| default\_security\_group\_egress | List of maps of egress rules to set on the default security group | `list(map(string))` | `null` | no |
320+
| default\_security\_group\_ingress | List of maps of ingress rules to set on the default security group | `list(map(string))` | `null` | no |
321+
| default\_security\_group\_name | Name to be used on the default security group | `string` | `"default"` | no |
322+
| default\_security\_group\_tags | Additional tags for the default security group | `map(string)` | `{}` | no |
319323
| default\_vpc\_enable\_classiclink | Should be true to enable ClassicLink in the Default VPC | `bool` | `false` | no |
320324
| default\_vpc\_enable\_dns\_hostnames | Should be true to enable DNS hostnames in the Default VPC | `bool` | `false` | no |
321325
| default\_vpc\_enable\_dns\_support | Should be true to enable DNS support in the Default VPC | `bool` | `true` | no |
@@ -496,6 +500,7 @@ It is possible to integrate this VPC module with [terraform-aws-transit-gateway
496500
| logs\_endpoint\_security\_group\_ids | The ID of one or more security groups to associate with the network interface for CloudWatch Logs endpoint | `list(string)` | `[]` | no |
497501
| logs\_endpoint\_subnet\_ids | The ID of one or more subnets in which to create a network interface for CloudWatch Logs endpoint. Only a single subnet within an AZ is supported. If omitted, private subnets will be used. | `list(string)` | `[]` | no |
498502
| manage\_default\_network\_acl | Should be true to adopt and manage Default Network ACL | `bool` | `false` | no |
503+
| manage\_default\_security\_group | Should be true to adopt and manage default security group | `bool` | `false` | no |
499504
| manage\_default\_vpc | Should be true to adopt and manage Default VPC | `bool` | `false` | no |
500505
| map\_public\_ip\_on\_launch | Should be false if you do not want to auto-assign public IP on launch | `bool` | `true` | no |
501506
| monitoring\_endpoint\_private\_dns\_enabled | Whether or not to associate a private hosted zone with the specified VPC for CloudWatch Monitoring endpoint | `bool` | `false` | no |

examples/complete-vpc/main.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ module "vpc" {
106106
sqs_endpoint_private_dns_enabled = true
107107
sqs_endpoint_security_group_ids = [data.aws_security_group.default.id]
108108

109+
# Default security group - ingress/egress rules cleared to deny all
110+
manage_default_security_group = true
111+
default_security_group_ingress = [{}]
112+
default_security_group_egress = [{}]
113+
109114
# VPC Flow Logs (Cloudwatch log group and IAM role will be created)
110115
enable_flow_log = true
111116
create_flow_log_cloudwatch_log_group = true

main.tf

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,48 @@ resource "aws_vpc_ipv4_cidr_block_association" "this" {
5454
cidr_block = element(var.secondary_cidr_blocks, count.index)
5555
}
5656

57+
resource "aws_default_security_group" "this" {
58+
count = var.create_vpc && var.manage_default_security_group ? 1 : 0
59+
60+
vpc_id = aws_vpc.this[0].id
61+
62+
dynamic "ingress" {
63+
for_each = var.default_security_group_ingress
64+
content {
65+
self = lookup(ingress.value, "self", null)
66+
cidr_blocks = compact(split(",", lookup(ingress.value, "cidr_blocks", "")))
67+
ipv6_cidr_blocks = compact(split(",", lookup(ingress.value, "ipv6_cidr_blocks", "")))
68+
prefix_list_ids = compact(split(",", lookup(ingress.value, "prefix_list_ids", "")))
69+
description = lookup(ingress.value, "description", null)
70+
from_port = lookup(ingress.value, "from_port", 0)
71+
to_port = lookup(ingress.value, "to_port", 0)
72+
protocol = lookup(ingress.value, "protocol", "-1")
73+
}
74+
}
75+
76+
dynamic "egress" {
77+
for_each = var.default_security_group_egress
78+
content {
79+
self = lookup(egress.value, "self", null)
80+
cidr_blocks = compact(split(",", lookup(egress.value, "cidr_blocks", "")))
81+
ipv6_cidr_blocks = compact(split(",", lookup(egress.value, "ipv6_cidr_blocks", "")))
82+
prefix_list_ids = compact(split(",", lookup(egress.value, "prefix_list_ids", "")))
83+
description = lookup(egress.value, "description", null)
84+
from_port = lookup(egress.value, "from_port", 0)
85+
to_port = lookup(egress.value, "to_port", 0)
86+
protocol = lookup(egress.value, "protocol", "-1")
87+
}
88+
}
89+
90+
tags = merge(
91+
{
92+
"Name" = format("%s", var.default_security_group_name)
93+
},
94+
var.tags,
95+
var.default_security_group_tags,
96+
)
97+
}
98+
5799
###################
58100
# DHCP Options Set
59101
###################
@@ -1105,4 +1147,3 @@ resource "aws_default_vpc" "this" {
11051147
var.default_vpc_tags,
11061148
)
11071149
}
1108-

variables.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,12 +2259,42 @@ variable "elasticache_outbound_acl_rules" {
22592259
]
22602260
}
22612261

2262+
variable "manage_default_security_group" {
2263+
description = "Should be true to adopt and manage default security group"
2264+
type = bool
2265+
default = false
2266+
}
2267+
2268+
variable "default_security_group_name" {
2269+
description = "Name to be used on the default security group"
2270+
type = string
2271+
default = "default"
2272+
}
2273+
2274+
variable "default_security_group_ingress" {
2275+
description = "List of maps of ingress rules to set on the default security group"
2276+
type = list(map(string))
2277+
default = null
2278+
}
2279+
22622280
variable "enable_flow_log" {
22632281
description = "Whether or not to enable VPC Flow Logs"
22642282
type = bool
22652283
default = false
22662284
}
22672285

2286+
variable "default_security_group_egress" {
2287+
description = "List of maps of egress rules to set on the default security group"
2288+
type = list(map(string))
2289+
default = null
2290+
}
2291+
2292+
variable "default_security_group_tags" {
2293+
description = "Additional tags for the default security group"
2294+
type = map(string)
2295+
default = {}
2296+
}
2297+
22682298
variable "create_flow_log_cloudwatch_log_group" {
22692299
description = "Whether to create CloudWatch log group for VPC Flow Logs"
22702300
type = bool

0 commit comments

Comments
 (0)