forked from hashicorp-education/learn-terraform-variables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
121 lines (94 loc) · 2.62 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
region = "us-west-2"
}
data "aws_availability_zones" "available" {
state = "available"
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.64.0"
cidr = "10.0.0.0/16"
azs = data.aws_availability_zones.available.names
private_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
enable_nat_gateway = true
enable_vpn_gateway = false
tags = {
project = "project-alpha",
environment = "dev"
}
}
module "app_security_group" {
source = "terraform-aws-modules/security-group/aws//modules/web"
version = "3.17.0"
name = "web-sg-project-alpha-dev"
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 = {
project = "project-alpha",
environment = "dev"
}
}
module "lb_security_group" {
source = "terraform-aws-modules/security-group/aws//modules/web"
version = "3.17.0"
name = "lb-sg-project-alpha-dev"
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 = {
project = "project-alpha",
environment = "dev"
}
}
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}-project-alpha-dev"
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 = {
project = "project-alpha",
environment = "dev"
}
}
module "ec2_instances" {
source = "./modules/aws-instance"
instance_count = 2
instance_type = "t2.micro"
subnet_ids = module.vpc.private_subnets[*]
security_group_ids = [module.app_security_group.this_security_group_id]
tags = {
project = "project-alpha",
environment = "dev"
}
}