-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
105 lines (80 loc) · 2.15 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
provider "aws" {
region = var.aws_region
}
resource "aws_security_group" "instance_sg" {
name = "instance_sg"
description = "Security group for EC2 instance"
vpc_id = "vpc-00917d332f96d91a5"
## This allowed us to be able to login to our EC2 instance and perfromance admin task
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
## Ingress trafic control for users
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
## Instance type that we are requireing from AWS
resource "aws_instance" "nginx_instance" {
ami = var.instance_ami
instance_type = var.instance_type
subnet_id = var.subnet_id
associate_public_ip_address = true
key_name = aws_key_pair.nginx_key_pair.key_name
tags = {
Name = "NginxInstance"
}
}
# Name for the key pair and path to the public key
resource "aws_key_pair" "nginx_key_pair" {
key_name = "nginx-key"
public_key = file("~/.ssh/id_rsa.pub")
}
## LB related resoures from AWS
resource "aws_lb" "nginx_lb" {
name = "nginx-lb"
internal = false
load_balancer_type = "application"
enable_deletion_protection = false
subnets = var.subnet_ids
enable_http2 = true
}
resource "aws_lb_listener" "nginx_listener_http" {
load_balancer_arn = aws_lb.nginx_lb.arn
port = 80
protocol = "HTTP"
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
status_code = 200
message_body = "Welcome to Nginx"
}
}
}
resource "aws_lb_target_group" "nginx_target_group" {
name = "nginx-tg"
port = 80
protocol = "HTTP"
vpc_id = "vpc-00917d332f96d91a5"
health_check {
path = "/"
protocol = "HTTP"
}
}
resource "aws_lb_target_group_attachment" "nginx_target_attachment" {
target_group_arn = aws_lb_target_group.nginx_target_group.arn
target_id = aws_instance.nginx_instance.id
}