File tree Expand file tree Collapse file tree 8 files changed +170
-17
lines changed
Expand file tree Collapse file tree 8 files changed +170
-17
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+
Original file line number Diff line number Diff line change 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 -->
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 22# Get ID of created Security Group
33# #################################
44locals {
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# #########################
1511resource "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# ################################
3430resource "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
Original file line number Diff line number Diff 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+
1022variable "vpc_id" {
1123 description = " ID of the VPC where to create security group"
1224 type = string
25+ default = null
1326}
1427
1528variable "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
2034variable "use_name_prefix" {
You can’t perform that action at this time.
0 commit comments