Skip to content

Commit ac988ca

Browse files
authored
feat: Create group optionally (#218)
1 parent 18bb00a commit ac988ca

File tree

8 files changed

+170
-17
lines changed

8 files changed

+170
-17
lines changed

README.md

Lines changed: 7 additions & 9 deletions
Large diffs are not rendered by default.

examples/complete/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ No requirements.
3535
| <a name="module_fixed_name_sg"></a> [fixed\_name\_sg](#module\_fixed\_name\_sg) | ../../ | |
3636
| <a name="module_ipv4_ipv6_example"></a> [ipv4\_ipv6\_example](#module\_ipv4\_ipv6\_example) | ../../ | |
3737
| <a name="module_main_sg"></a> [main\_sg](#module\_main\_sg) | ../../ | |
38+
| <a name="module_only_rules"></a> [only\_rules](#module\_only\_rules) | ../../ | |
3839
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | |
3940

4041
## Resources

examples/complete/main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,20 @@ module "fixed_name_sg" {
380380
ingress_rules = ["https-443-tcp"]
381381
}
382382

383+
############################
384+
# Only security group rules
385+
############################
386+
module "only_rules" {
387+
source = "../../"
388+
389+
create_sg = false
390+
security_group_id = module.complete_sg.security_group_id
391+
ingress_with_source_security_group_id = [
392+
{
393+
description = "http from service one"
394+
rule = "http-80-tcp"
395+
source_security_group_id = data.aws_security_group.default.id
396+
},
397+
]
398+
}
399+

examples/rules-only/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Create rules only
2+
3+
Configuration in this directory creates two security groups using native Terraform resources, and then uses the module to add rules.
4+
5+
Data sources are used to discover existing VPC resources (VPC and default security group).
6+
7+
## Usage
8+
9+
To run this example you need to execute:
10+
11+
```bash
12+
$ terraform init
13+
$ terraform plan
14+
$ terraform apply
15+
```
16+
17+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
18+
19+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
20+
## Requirements
21+
22+
No requirements.
23+
24+
## Providers
25+
26+
| Name | Version |
27+
|------|---------|
28+
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
29+
30+
## Modules
31+
32+
| Name | Source | Version |
33+
|------|--------|---------|
34+
| <a name="module_rules_one"></a> [rules\_one](#module\_rules\_one) | ../../ | |
35+
| <a name="module_rules_two"></a> [rules\_two](#module\_rules\_two) | ../../ | |
36+
37+
## Resources
38+
39+
| Name | Type |
40+
|------|------|
41+
| [aws_security_group.service_one](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
42+
| [aws_security_group.service_two](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
43+
| [aws_security_group.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/security_group) | data source |
44+
| [aws_vpc.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
45+
46+
## Inputs
47+
48+
No inputs.
49+
50+
## Outputs
51+
52+
| Name | Description |
53+
|------|-------------|
54+
| <a name="output_service_one_security_group_id"></a> [service\_one\_security\_group\_id](#output\_service\_one\_security\_group\_id) | The ID of the security group for service one |
55+
| <a name="output_service_two_security_group_id"></a> [service\_two\_security\_group\_id](#output\_service\_two\_security\_group\_id) | The ID of the security group for service two |
56+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/rules-only/main.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
#############################################################
6+
# Data sources to get VPC and default security group details
7+
#############################################################
8+
data "aws_vpc" "default" {
9+
default = true
10+
}
11+
12+
data "aws_security_group" "default" {
13+
name = "default"
14+
vpc_id = data.aws_vpc.default.id
15+
}
16+
17+
########################################################
18+
# Create SGs
19+
########################################################
20+
21+
resource "aws_security_group" "service_one" {
22+
name = "service_one"
23+
description = "Allow access from service two"
24+
}
25+
26+
resource "aws_security_group" "service_two" {
27+
name = "service_two"
28+
description = "Allow access from service one"
29+
}
30+
31+
########################################################
32+
# Add SG rules
33+
########################################################
34+
35+
module "rules_one" {
36+
source = "../../"
37+
38+
create_sg = false
39+
security_group_id = aws_security_group.service_one.id
40+
ingress_with_source_security_group_id = [
41+
{
42+
description = "http from service two"
43+
rule = "http-80-tcp"
44+
source_security_group_id = aws_security_group.service_two.id
45+
},
46+
]
47+
}
48+
49+
module "rules_two" {
50+
source = "../../"
51+
52+
create_sg = false
53+
security_group_id = aws_security_group.service_two.id
54+
ingress_with_source_security_group_id = [
55+
{
56+
description = "http from service one"
57+
rule = "http-80-tcp"
58+
source_security_group_id = aws_security_group.service_one.id
59+
},
60+
]
61+
}
62+

examples/rules-only/outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "service_one_security_group_id" {
2+
description = "The ID of the security group for service one"
3+
value = aws_security_group.service_one.id
4+
}
5+
6+
output "service_two_security_group_id" {
7+
description = "The ID of the security group for service two"
8+
value = aws_security_group.service_two.id
9+
}

main.tf

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22
# Get ID of created Security Group
33
##################################
44
locals {
5-
this_sg_id = concat(
6-
aws_security_group.this.*.id,
7-
aws_security_group.this_name_prefix.*.id,
8-
[""],
9-
)[0]
5+
this_sg_id = var.create_sg ? concat(aws_security_group.this.*.id, aws_security_group.this_name_prefix.*.id, [""])[0] : var.security_group_id
106
}
117

128
##########################
139
# Security group with name
1410
##########################
1511
resource "aws_security_group" "this" {
16-
count = var.create && false == var.use_name_prefix ? 1 : 0
12+
count = var.create && var.create_sg && !var.use_name_prefix ? 1 : 0
1713

1814
name = var.name
1915
description = var.description
@@ -32,7 +28,7 @@ resource "aws_security_group" "this" {
3228
# Security group with name_prefix
3329
#################################
3430
resource "aws_security_group" "this_name_prefix" {
35-
count = var.create && var.use_name_prefix ? 1 : 0
31+
count = var.create && var.create_sg && var.use_name_prefix ? 1 : 0
3632

3733
name_prefix = "${var.name}-"
3834
description = var.description

variables.tf

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@ variable "create" {
77
default = true
88
}
99

10+
variable "create_sg" {
11+
description = "Whether to create security group"
12+
type = bool
13+
default = true
14+
}
15+
16+
variable "security_group_id" {
17+
description = "ID of existing security group whose rules we will manage"
18+
type = string
19+
default = null
20+
}
21+
1022
variable "vpc_id" {
1123
description = "ID of the VPC where to create security group"
1224
type = string
25+
default = null
1326
}
1427

1528
variable "name" {
16-
description = "Name of security group"
29+
description = "Name of security group - not required if create_group is false"
1730
type = string
31+
default = null
1832
}
1933

2034
variable "use_name_prefix" {

0 commit comments

Comments
 (0)