|
| 1 | +provider "aws" { |
| 2 | + region = "eu-west-1" |
| 3 | +} |
| 4 | + |
| 5 | +##### |
| 6 | +# VPC and subnets |
| 7 | +##### |
| 8 | +module "vpc" { |
| 9 | + source = "terraform-aws-modules/vpc/aws" |
| 10 | + version = "2.44.0" |
| 11 | + |
| 12 | + name = "simple-vpc" |
| 13 | + |
| 14 | + cidr = "10.0.0.0/16" |
| 15 | + |
| 16 | + azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"] |
| 17 | + private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] |
| 18 | + public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] |
| 19 | + |
| 20 | + enable_nat_gateway = false |
| 21 | + |
| 22 | + tags = { |
| 23 | + Environment = "test" |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +##### |
| 28 | +# Application Load Balancer |
| 29 | +##### |
| 30 | +module "alb" { |
| 31 | + source = "../../" |
| 32 | + |
| 33 | + name_prefix = "example-with-access-logs" |
| 34 | + |
| 35 | + load_balancer_type = "application" |
| 36 | + |
| 37 | + internal = false |
| 38 | + vpc_id = module.vpc.vpc_id |
| 39 | + subnets = flatten([module.vpc.public_subnets]) |
| 40 | + |
| 41 | + enable_http_to_https_redirect = true |
| 42 | + cidr_blocks_redirect = ["10.10.0.0/16"] |
| 43 | + |
| 44 | + access_logs = { |
| 45 | + bucket = aws_s3_bucket.alb_access_logs.bucket |
| 46 | + prefix = "example-with-access-logs-alb" |
| 47 | + enabled = true |
| 48 | + } |
| 49 | + |
| 50 | + tags = { |
| 51 | + Project = "Test" |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +##### |
| 56 | +# ALB listener |
| 57 | +##### |
| 58 | +resource "aws_lb_listener" "alb_80_redirect_to_443" { |
| 59 | + load_balancer_arn = module.alb.arn |
| 60 | + port = "80" |
| 61 | + protocol = "HTTP" |
| 62 | + |
| 63 | + default_action { |
| 64 | + type = "redirect" |
| 65 | + |
| 66 | + redirect { |
| 67 | + port = "443" |
| 68 | + protocol = "HTTPS" |
| 69 | + status_code = "HTTP_301" |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +##### |
| 75 | +# SGs |
| 76 | +##### |
| 77 | +resource "aws_security_group_rule" "alb_ingress_443" { |
| 78 | + security_group_id = module.alb.security_group_id |
| 79 | + type = "ingress" |
| 80 | + protocol = "tcp" |
| 81 | + from_port = 443 |
| 82 | + to_port = 443 |
| 83 | + cidr_blocks = ["0.0.0.0/0"] |
| 84 | + ipv6_cidr_blocks = ["::/0"] |
| 85 | +} |
| 86 | + |
| 87 | +##### |
| 88 | +# S3 bucket storing ALB access logs |
| 89 | +##### |
| 90 | +locals { |
| 91 | + alb_root_account_id = "156460612806" # valid account id for Ireland Region. Full list -> https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html |
| 92 | +} |
| 93 | + |
| 94 | +resource "aws_s3_bucket" "alb_access_logs" { |
| 95 | + bucket = "example-alb-access-logs-bucket" |
| 96 | + acl = "private" |
| 97 | + region = data.aws_region.current.name |
| 98 | + |
| 99 | + server_side_encryption_configuration { |
| 100 | + rule { |
| 101 | + apply_server_side_encryption_by_default { |
| 102 | + sse_algorithm = "AES256" |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + policy = <<POLICY |
| 108 | +{ |
| 109 | + "Version": "2012-10-17", |
| 110 | + "Statement": [ |
| 111 | + { |
| 112 | + "Sid": "AllowELBRootAccount", |
| 113 | + "Effect": "Allow", |
| 114 | + "Principal": { |
| 115 | + "AWS": "arn:aws:iam::${local.alb_root_account_id}:root" |
| 116 | + }, |
| 117 | + "Action": "s3:PutObject", |
| 118 | + "Resource": "arn:aws:s3:::example-alb-access-logs-bucket/*" |
| 119 | + }, |
| 120 | + { |
| 121 | + "Sid": "AWSLogDeliveryWrite", |
| 122 | + "Effect": "Allow", |
| 123 | + "Principal": { |
| 124 | + "Service": "delivery.logs.amazonaws.com" |
| 125 | + }, |
| 126 | + "Action": "s3:PutObject", |
| 127 | + "Resource": "arn:aws:s3:::example-alb-access-logs-bucket/*", |
| 128 | + "Condition": { |
| 129 | + "StringEquals": { |
| 130 | + "s3:x-amz-acl": "bucket-owner-full-control" |
| 131 | + } |
| 132 | + } |
| 133 | + }, |
| 134 | + { |
| 135 | + "Sid": "AWSLogDeliveryAclCheck", |
| 136 | + "Effect": "Allow", |
| 137 | + "Principal": { |
| 138 | + "Service": "delivery.logs.amazonaws.com" |
| 139 | + }, |
| 140 | + "Action": "s3:GetBucketAcl", |
| 141 | + "Resource": "arn:aws:s3:::example-alb-access-logs-bucket" |
| 142 | + } |
| 143 | + ] |
| 144 | +} |
| 145 | +POLICY |
| 146 | + |
| 147 | + tags = { |
| 148 | + Environment = "test" |
| 149 | + } |
| 150 | +} |
0 commit comments