-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
97 lines (70 loc) · 2.55 KB
/
main.tf
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
data "aws_availability_zones" "available" {
state = "available"
}
data "aws_region" "current" {}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.64.0"
cidr = var.vpc_cidr_block
azs = data.aws_availability_zones.available.names
private_subnets = slice(var.private_subnet_cidr_blocks, 0, var.private_subnet_count)
public_subnets = slice(var.public_subnet_cidr_blocks, 0, var.public_subnet_count)
enable_nat_gateway = false
enable_vpn_gateway = var.enable_vpn_gateway
tags = var.resource_tags
}
module "app_security_group" {
source = "terraform-aws-modules/security-group/aws//modules/web"
version = "3.17.0"
name = "web-sg-${var.resource_tags["project"]}-${var.resource_tags["environment"]}"
description = "Security group for web-servers with HTTP ports open within VPC"
vpc_id = module.vpc.vpc_id
ingress_cidr_blocks = module.vpc.public_subnets_cidr_blocks
tags = var.resource_tags
}
module "lb_security_group" {
source = "terraform-aws-modules/security-group/aws//modules/web"
version = "3.17.0"
name = "lb-sg-${var.resource_tags["project"]}-${var.resource_tags["environment"]}"
description = "Security group for load balancer with HTTP ports open within VPC"
vpc_id = module.vpc.vpc_id
ingress_cidr_blocks = ["0.0.0.0/0"]
tags = var.resource_tags
}
resource "random_string" "lb_id" {
length = 3
special = false
}
module "elb_http" {
source = "terraform-aws-modules/elb/aws"
version = "2.4.0"
# Ensure load balancer name is unique
name = "lb-${random_string.lb_id.result}-${var.resource_tags["project"]}-${var.resource_tags["environment"]}"
internal = false
security_groups = [module.lb_security_group.this_security_group_id]
subnets = module.vpc.public_subnets
number_of_instances = length(module.ec2_instances.instance_ids)
instances = module.ec2_instances.instance_ids
listener = [{
instance_port = "80"
instance_protocol = "HTTP"
lb_port = "80"
lb_protocol = "HTTP"
}]
health_check = {
target = "HTTP:80/index.html"
interval = 10
healthy_threshold = 3
unhealthy_threshold = 10
timeout = 5
}
tags = var.resource_tags
}
module "ec2_instances" {
source = "./modules/aws-instance"
instance_count = var.instance_count
instance_type = var.ec2_instance_type
subnet_ids = module.vpc.private_subnets[*]
security_group_ids = [module.app_security_group.this_security_group_id]
tags = var.resource_tags
}