-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlb.tf
More file actions
129 lines (109 loc) · 3.17 KB
/
Copy pathlb.tf
File metadata and controls
129 lines (109 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# note that this creates the alb, target group, and access logs
# the listeners are defined in lb-http.tf and lb-https.tf
# delete either of these if your app doesn't need them
# but you need at least one
# This is the default list of cidr blocks that will be allowed to access the ALB on http and/or https
variable "custom_default_alb_cidr_blocks" {
type = list(string)
default = ["0.0.0.0/0"]
}
resource "aws_alb" "main" {
name = "${var.app}-${var.environment}"
# launch lbs in public or private subnets based on "internal" variable
internal = !var.create_public_ip
subnets = var.load_balancer_subnets
security_groups = [aws_security_group.nsg_lb.id]
tags = var.tags
# enable access logs in order to get support from aws
access_logs {
enabled = true
bucket = aws_s3_bucket.lb_access_logs.bucket
}
}
resource "aws_alb_target_group" "main" {
name = "${var.app}-${var.environment}"
port = var.lb_port
protocol = var.lb_protocol
vpc_id = var.vpc
target_type = "ip"
deregistration_delay = var.deregistration_delay
health_check {
path = var.health_check
matcher = var.health_check_matcher
interval = var.health_check_interval
timeout = var.health_check_timeout
healthy_threshold = 5
unhealthy_threshold = 5
}
tags = var.tags
}
data "aws_elb_service_account" "main" {
}
# bucket for storing ALB access logs
resource "aws_s3_bucket" "lb_access_logs" {
bucket = "${var.app}-${var.environment}-lb-access-logs"
tags = var.tags
force_destroy = true
}
resource "aws_s3_bucket_server_side_encryption_configuration" "bucket_encryption" {
bucket = aws_s3_bucket.lb_access_logs.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_lifecycle_configuration" "bucket_lifecycle_multipart" {
bucket = aws_s3_bucket.lb_access_logs.id
rule {
id = "cleanup"
status = "Enabled"
# This should not happen, but just in case
abort_incomplete_multipart_upload {
days_after_initiation = 1
}
expiration {
days = var.lb_access_logs_expiration_days
}
}
}
# give load balancing service access to the bucket
resource "aws_s3_bucket_policy" "lb_access_logs" {
bucket = aws_s3_bucket.lb_access_logs.id
policy = <<POLICY
{
"Id": "Policy",
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:PutObject"
],
"Effect": "Allow",
"Resource": [
"${aws_s3_bucket.lb_access_logs.arn}",
"${aws_s3_bucket.lb_access_logs.arn}/*"
],
"Principal": {
"AWS": [ "${data.aws_elb_service_account.main.arn}" ]
}
}
]
}
POLICY
}
resource "aws_s3_bucket_public_access_block" "example" {
bucket = aws_s3_bucket.lb_access_logs.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
# The load balancer DNS name
output "lb_dns" {
value = aws_alb.main.dns_name
}
# The arn of the load balancer
output "lb_arn" {
value = aws_alb.main.arn
}