Skip to content

Commit 8232b47

Browse files
authored
feat: Add support for creating a security group along with the load balancer (#273)
1 parent 7ba7833 commit 8232b47

File tree

10 files changed

+283
-132
lines changed

10 files changed

+283
-132
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.77.0
3+
rev: v1.77.1
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_wrapper_module_for_each

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,16 @@ No modules.
320320
| [aws_lb_listener_rule.https_listener_rule](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener_rule) | resource |
321321
| [aws_lb_target_group.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group) | resource |
322322
| [aws_lb_target_group_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group_attachment) | resource |
323+
| [aws_security_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
324+
| [aws_security_group_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
323325

324326
## Inputs
325327

326328
| Name | Description | Type | Default | Required |
327329
|------|-------------|------|---------|:--------:|
328330
| <a name="input_access_logs"></a> [access\_logs](#input\_access\_logs) | Map containing access logging configuration for load balancer. | `map(string)` | `{}` | no |
329331
| <a name="input_create_lb"></a> [create\_lb](#input\_create\_lb) | Controls if the Load Balancer should be created | `bool` | `true` | no |
332+
| <a name="input_create_security_group"></a> [create\_security\_group](#input\_create\_security\_group) | Determines if a security group is created | `bool` | `true` | no |
330333
| <a name="input_desync_mitigation_mode"></a> [desync\_mitigation\_mode](#input\_desync\_mitigation\_mode) | Determines how the load balancer handles requests that might pose a security risk to an application due to HTTP desync. | `string` | `"defensive"` | no |
331334
| <a name="input_drop_invalid_header_fields"></a> [drop\_invalid\_header\_fields](#input\_drop\_invalid\_header\_fields) | Indicates whether invalid header fields are dropped in application load balancers. Defaults to false. | `bool` | `false` | no |
332335
| <a name="input_enable_cross_zone_load_balancing"></a> [enable\_cross\_zone\_load\_balancing](#input\_enable\_cross\_zone\_load\_balancing) | Indicates whether cross zone load balancing should be enabled in application load balancers. | `bool` | `false` | no |
@@ -355,6 +358,11 @@ No modules.
355358
| <a name="input_name_prefix"></a> [name\_prefix](#input\_name\_prefix) | The resource name prefix and Name tag of the load balancer. Cannot be longer than 6 characters | `string` | `null` | no |
356359
| <a name="input_preserve_host_header"></a> [preserve\_host\_header](#input\_preserve\_host\_header) | Indicates whether Host header should be preserve and forward to targets without any change. Defaults to false. | `bool` | `false` | no |
357360
| <a name="input_putin_khuylo"></a> [putin\_khuylo](#input\_putin\_khuylo) | Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo! | `bool` | `true` | no |
361+
| <a name="input_security_group_description"></a> [security\_group\_description](#input\_security\_group\_description) | Description of the security group created | `string` | `null` | no |
362+
| <a name="input_security_group_name"></a> [security\_group\_name](#input\_security\_group\_name) | Name to use on security group created | `string` | `null` | no |
363+
| <a name="input_security_group_rules"></a> [security\_group\_rules](#input\_security\_group\_rules) | Security group rules to add to the security group created | `any` | `{}` | no |
364+
| <a name="input_security_group_tags"></a> [security\_group\_tags](#input\_security\_group\_tags) | A map of additional tags to add to the security group created | `map(string)` | `{}` | no |
365+
| <a name="input_security_group_use_name_prefix"></a> [security\_group\_use\_name\_prefix](#input\_security\_group\_use\_name\_prefix) | Determines whether the security group name (`security_group_name`) is used as a prefix | `bool` | `true` | no |
358366
| <a name="input_security_groups"></a> [security\_groups](#input\_security\_groups) | The security groups to attach to the load balancer. e.g. ["sg-edcd9784","sg-edcd9785"] | `list(string)` | `[]` | no |
359367
| <a name="input_subnet_mapping"></a> [subnet\_mapping](#input\_subnet\_mapping) | A list of subnet mapping blocks describing subnets to attach to network load balancer | `list(map(string))` | `[]` | no |
360368
| <a name="input_subnets"></a> [subnets](#input\_subnets) | A list of subnets to associate with the load balancer. e.g. ['subnet-1a2b3c4d','subnet-1a2b3c4e','subnet-1a2b3c4f'] | `list(string)` | `null` | no |
@@ -376,6 +384,8 @@ No modules.
376384
| <a name="output_lb_dns_name"></a> [lb\_dns\_name](#output\_lb\_dns\_name) | The DNS name of the load balancer |
377385
| <a name="output_lb_id"></a> [lb\_id](#output\_lb\_id) | The ID and ARN of the load balancer we created |
378386
| <a name="output_lb_zone_id"></a> [lb\_zone\_id](#output\_lb\_zone\_id) | The zone\_id of the load balancer to assist with creating DNS records |
387+
| <a name="output_security_group_arn"></a> [security\_group\_arn](#output\_security\_group\_arn) | Amazon Resource Name (ARN) of the security group |
388+
| <a name="output_security_group_id"></a> [security\_group\_id](#output\_security\_group\_id) | ID of the security group |
379389
| <a name="output_target_group_arn_suffixes"></a> [target\_group\_arn\_suffixes](#output\_target\_group\_arn\_suffixes) | ARN suffixes of our target groups - can be used with CloudWatch |
380390
| <a name="output_target_group_arns"></a> [target\_group\_arns](#output\_target\_group\_arns) | ARNs of the target groups. Useful for passing to your Auto Scaling group |
381391
| <a name="output_target_group_attachments"></a> [target\_group\_attachments](#output\_target\_group\_attachments) | ARNs of the target group attachment IDs |

examples/complete-alb/README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ Note that this example may create resources which cost money. Run `terraform des
2222
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
2323
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.27 |
2424
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2.0 |
25-
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
2625

2726
## Providers
2827

2928
| Name | Version |
3029
|------|---------|
3130
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.27 |
3231
| <a name="provider_null"></a> [null](#provider\_null) | >= 2.0 |
33-
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |
3432

3533
## Modules
3634

@@ -41,7 +39,7 @@ Note that this example may create resources which cost money. Run `terraform des
4139
| <a name="module_lambda_with_allowed_triggers"></a> [lambda\_with\_allowed\_triggers](#module\_lambda\_with\_allowed\_triggers) | terraform-aws-modules/lambda/aws | ~> 3.0 |
4240
| <a name="module_lambda_without_allowed_triggers"></a> [lambda\_without\_allowed\_triggers](#module\_lambda\_without\_allowed\_triggers) | terraform-aws-modules/lambda/aws | ~> 3.0 |
4341
| <a name="module_lb_disabled"></a> [lb\_disabled](#module\_lb\_disabled) | ../../ | n/a |
44-
| <a name="module_security_group"></a> [security\_group](#module\_security\_group) | terraform-aws-modules/security-group/aws | ~> 4.0 |
42+
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 3.0 |
4543
| <a name="module_wildcard_cert"></a> [wildcard\_cert](#module\_wildcard\_cert) | terraform-aws-modules/acm/aws | ~> 3.0 |
4644

4745
## Resources
@@ -53,11 +51,9 @@ Note that this example may create resources which cost money. Run `terraform des
5351
| [aws_cognito_user_pool_domain.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cognito_user_pool_domain) | resource |
5452
| [aws_instance.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |
5553
| [null_resource.download_package](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
56-
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
5754
| [aws_ami.amazon_linux](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
55+
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
5856
| [aws_route53_zone.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
59-
| [aws_subnets.all](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
60-
| [aws_vpc.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
6157

6258
## Inputs
6359

@@ -76,6 +72,8 @@ No inputs.
7672
| <a name="output_lb_dns_name"></a> [lb\_dns\_name](#output\_lb\_dns\_name) | The DNS name of the load balancer. |
7773
| <a name="output_lb_id"></a> [lb\_id](#output\_lb\_id) | The ID and ARN of the load balancer we created. |
7874
| <a name="output_lb_zone_id"></a> [lb\_zone\_id](#output\_lb\_zone\_id) | The zone\_id of the load balancer to assist with creating DNS records. |
75+
| <a name="output_security_group_arn"></a> [security\_group\_arn](#output\_security\_group\_arn) | Amazon Resource Name (ARN) of the security group |
76+
| <a name="output_security_group_id"></a> [security\_group\_id](#output\_security\_group\_id) | ID of the security group |
7977
| <a name="output_target_group_arn_suffixes"></a> [target\_group\_arn\_suffixes](#output\_target\_group\_arn\_suffixes) | ARN suffixes of our target groups - can be used with CloudWatch. |
8078
| <a name="output_target_group_arns"></a> [target\_group\_arns](#output\_target\_group\_arns) | ARNs of the target groups. Useful for passing to your Auto Scaling group. |
8179
| <a name="output_target_group_attachments"></a> [target\_group\_attachments](#output\_target\_group\_attachments) | ARNs of the target group attachment IDs. |

examples/complete-alb/main.tf

Lines changed: 116 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,66 @@
11
provider "aws" {
2-
region = "eu-west-1"
3-
}
4-
5-
locals {
6-
domain_name = "terraform-aws-modules.modules.tf"
7-
}
8-
9-
##################################################################
10-
# Data sources to get VPC and subnets
11-
##################################################################
12-
data "aws_vpc" "default" {
13-
default = true
14-
}
15-
16-
data "aws_subnets" "all" {
17-
filter {
18-
name = "vpc-id"
19-
values = [data.aws_vpc.default.id]
20-
}
21-
}
22-
23-
resource "random_pet" "this" {
24-
length = 2
25-
}
26-
27-
data "aws_route53_zone" "this" {
28-
name = local.domain_name
2+
region = local.region
293
}
304

31-
module "security_group" {
32-
source = "terraform-aws-modules/security-group/aws"
33-
version = "~> 4.0"
34-
35-
name = "alb-sg-${random_pet.this.id}"
36-
description = "Security group for example usage with ALB"
37-
vpc_id = data.aws_vpc.default.id
38-
39-
ingress_cidr_blocks = ["0.0.0.0/0"]
40-
ingress_rules = ["http-80-tcp", "all-icmp"]
41-
egress_rules = ["all-all"]
42-
}
5+
data "aws_availability_zones" "available" {}
436

44-
#module "log_bucket" {
45-
# source = "terraform-aws-modules/s3-bucket/aws"
46-
# version = "~> 3.0"
47-
#
48-
# bucket = "logs-${random_pet.this.id}"
49-
# acl = "log-delivery-write"
50-
# force_destroy = true
51-
# attach_elb_log_delivery_policy = true
52-
#}
53-
54-
module "acm" {
55-
source = "terraform-aws-modules/acm/aws"
56-
version = "~> 3.0"
57-
58-
domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".")
59-
zone_id = data.aws_route53_zone.this.id
60-
}
7+
locals {
8+
name = "ex-${basename(path.cwd)}"
9+
region = "eu-west-1"
6110

62-
module "wildcard_cert" {
63-
source = "terraform-aws-modules/acm/aws"
64-
version = "~> 3.0"
11+
vpc_cidr = "10.0.0.0/16"
12+
azs = slice(data.aws_availability_zones.available.names, 0, 3)
6513

66-
domain_name = "*.${local.domain_name}" # trimsuffix(data.aws_route53_zone.this.name, ".")
67-
zone_id = data.aws_route53_zone.this.id
68-
}
69-
70-
##################################################################
71-
# AWS Cognito User Pool
72-
##################################################################
73-
resource "aws_cognito_user_pool" "this" {
74-
name = "user-pool-${random_pet.this.id}"
75-
}
76-
77-
resource "aws_cognito_user_pool_client" "this" {
78-
name = "user-pool-client-${random_pet.this.id}"
79-
user_pool_id = aws_cognito_user_pool.this.id
80-
generate_secret = true
81-
allowed_oauth_flows = ["code", "implicit"]
82-
callback_urls = ["https://${local.domain_name}/callback"]
83-
allowed_oauth_scopes = ["email", "openid"]
84-
allowed_oauth_flows_user_pool_client = true
85-
}
14+
domain_name = "terraform-aws-modules.modules.tf"
8615

87-
resource "aws_cognito_user_pool_domain" "this" {
88-
domain = random_pet.this.id
89-
user_pool_id = aws_cognito_user_pool.this.id
16+
tags = {
17+
Example = local.name
18+
GithubRepo = "terraform-aws-alb"
19+
GithubOrg = "terraform-aws-modules"
20+
}
9021
}
9122

9223
##################################################################
9324
# Application Load Balancer
9425
##################################################################
26+
9527
module "alb" {
9628
source = "../../"
9729

98-
name = "complete-alb-${random_pet.this.id}"
30+
name = local.name
9931

10032
load_balancer_type = "application"
10133

102-
vpc_id = data.aws_vpc.default.id
103-
security_groups = [module.security_group.security_group_id]
104-
subnets = data.aws_subnets.all.ids
34+
vpc_id = module.vpc.vpc_id
35+
subnets = module.vpc.public_subnets
36+
# Attach security groups
37+
security_groups = [module.vpc.default_security_group_id]
38+
# Attach rules to the created security group
39+
security_group_rules = {
40+
ingress_all_http = {
41+
type = "ingress"
42+
from_port = 80
43+
to_port = 80
44+
protocol = "http"
45+
description = "HTTP web traffic"
46+
cidr_blocks = ["0.0.0.0/0"]
47+
}
48+
ingress_all_icmp = {
49+
type = "ingress"
50+
from_port = -1
51+
to_port = -1
52+
protocol = "icmp"
53+
description = "ICMP"
54+
cidr_blocks = ["0.0.0.0/0"]
55+
}
56+
egress_all = {
57+
type = "egress"
58+
from_port = 0
59+
to_port = 0
60+
protocol = "-1"
61+
cidr_blocks = ["0.0.0.0/0"]
62+
}
63+
}
10564

10665
# # See notes in README (ref: https://github.com/terraform-providers/terraform-provider-aws/issues/7987)
10766
# access_logs = {
@@ -158,7 +117,7 @@ module "alb" {
158117
prompt = "login"
159118
}
160119
on_unauthenticated_request = "authenticate"
161-
session_cookie_name = "session-${random_pet.this.id}"
120+
session_cookie_name = "session-${local.name}"
162121
session_timeout = 3600
163122
user_pool_arn = aws_cognito_user_pool.this.arn
164123
user_pool_client_id = aws_cognito_user_pool_client.this.id
@@ -202,7 +161,7 @@ module "alb" {
202161
type = "authenticate-cognito"
203162

204163
on_unauthenticated_request = "authenticate"
205-
session_cookie_name = "session-${random_pet.this.id}"
164+
session_cookie_name = "session-${local.name}"
206165
session_timeout = 3600
207166
user_pool_arn = aws_cognito_user_pool.this.arn
208167
user_pool_client_id = aws_cognito_user_pool_client.this.id
@@ -465,6 +424,7 @@ module "alb" {
465424
#########################
466425
# LB will not be created
467426
#########################
427+
468428
module "lb_disabled" {
469429
source = "../../"
470430

@@ -474,6 +434,7 @@ module "lb_disabled" {
474434
##################
475435
# Extra resources
476436
##################
437+
477438
data "aws_ami" "amazon_linux" {
478439
most_recent = true
479440

@@ -524,7 +485,7 @@ module "lambda_with_allowed_triggers" {
524485
source = "terraform-aws-modules/lambda/aws"
525486
version = "~> 3.0"
526487

527-
function_name = "${random_pet.this.id}-with-allowed-triggers"
488+
function_name = "${local.name}-with-allowed-triggers"
528489
description = "My awesome lambda function (with allowed triggers)"
529490
handler = "index.lambda_handler"
530491
runtime = "python3.8"
@@ -548,7 +509,7 @@ module "lambda_without_allowed_triggers" {
548509
source = "terraform-aws-modules/lambda/aws"
549510
version = "~> 3.0"
550511

551-
function_name = "${random_pet.this.id}-without-allowed-triggers"
512+
function_name = "${local.name}-without-allowed-triggers"
552513
description = "My awesome lambda function (without allowed triggers)"
553514
handler = "index.lambda_handler"
554515
runtime = "python3.8"
@@ -563,3 +524,68 @@ module "lambda_without_allowed_triggers" {
563524

564525
depends_on = [null_resource.download_package]
565526
}
527+
528+
##################################################################
529+
# Data sources to get VPC and subnets
530+
##################################################################
531+
532+
module "vpc" {
533+
source = "terraform-aws-modules/vpc/aws"
534+
version = "~> 3.0"
535+
536+
name = local.name
537+
cidr = local.vpc_cidr
538+
539+
azs = local.azs
540+
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 4, k)]
541+
public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 48)]
542+
543+
enable_nat_gateway = true
544+
single_nat_gateway = true
545+
enable_dns_hostnames = true
546+
547+
tags = local.tags
548+
}
549+
550+
data "aws_route53_zone" "this" {
551+
name = local.domain_name
552+
}
553+
554+
module "acm" {
555+
source = "terraform-aws-modules/acm/aws"
556+
version = "~> 3.0"
557+
558+
domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".")
559+
zone_id = data.aws_route53_zone.this.id
560+
}
561+
562+
module "wildcard_cert" {
563+
source = "terraform-aws-modules/acm/aws"
564+
version = "~> 3.0"
565+
566+
domain_name = "*.${local.domain_name}" # trimsuffix(data.aws_route53_zone.this.name, ".")
567+
zone_id = data.aws_route53_zone.this.id
568+
}
569+
570+
##################################################################
571+
# AWS Cognito User Pool
572+
##################################################################
573+
574+
resource "aws_cognito_user_pool" "this" {
575+
name = "user-pool-${local.name}"
576+
}
577+
578+
resource "aws_cognito_user_pool_client" "this" {
579+
name = "user-pool-client-${local.name}"
580+
user_pool_id = aws_cognito_user_pool.this.id
581+
generate_secret = true
582+
allowed_oauth_flows = ["code", "implicit"]
583+
callback_urls = ["https://${local.domain_name}/callback"]
584+
allowed_oauth_scopes = ["email", "openid"]
585+
allowed_oauth_flows_user_pool_client = true
586+
}
587+
588+
resource "aws_cognito_user_pool_domain" "this" {
589+
domain = local.name
590+
user_pool_id = aws_cognito_user_pool.this.id
591+
}

examples/complete-alb/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,17 @@ output "target_group_attachments" {
6262
description = "ARNs of the target group attachment IDs."
6363
value = module.alb.target_group_attachments
6464
}
65+
66+
################################################################################
67+
# Security Group
68+
################################################################################
69+
70+
output "security_group_arn" {
71+
description = "Amazon Resource Name (ARN) of the security group"
72+
value = module.alb.security_group_arn
73+
}
74+
75+
output "security_group_id" {
76+
description = "ID of the security group"
77+
value = module.alb.security_group_id
78+
}

examples/complete-alb/versions.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ terraform {
66
source = "hashicorp/aws"
77
version = ">= 4.27"
88
}
9-
random = {
10-
source = "hashicorp/random"
11-
version = ">= 2.0"
12-
}
139
null = {
1410
source = "hashicorp/null"
1511
version = ">= 2.0"

0 commit comments

Comments
 (0)