-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
103 lines (83 loc) · 2.26 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
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "Niv Gabay"
}
}
resource "aws_subnet" "subnet_public" {
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.1.0/24"
tags = {
Name = "Niv Gabay"
}
}
resource "aws_internet_gateway" "vpc_igw" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "Niv Gabay"
}
}
resource "aws_route_table" "route_table" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.vpc_igw.id
}
tags = {
Name = "Niv Gabay"
}
}
resource "aws_route_table_association" "subnet_association" {
subnet_id = aws_subnet.subnet_public.id
route_table_id = aws_route_table.route_table.id
}
resource "aws_security_group" "security_group" {
vpc_id = aws_vpc.vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "Niv Gabay"
}
}
resource "aws_instance" "instance" {
ami = "ami-03cceb19496c25679"
instance_type = "t2.micro"
subnet_id = aws_subnet.subnet_public.id
security_groups = [aws_security_group.security_group.id]
associate_public_ip_address = true
tags = {
Name = "Niv Gabay"
}
user_data = <<-EOF
#!/bin/bash
# install requirements
sudo yum update
sudo yum install docker -y
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo yum install -y git
# clone repository
git clone https://github.com/n1vgabay/labos-devops-task.git
cd /labos-devops-task
# setup to run
sudo usermod -aG docker $USER
sudo systemctl restart docker
# run
sudo docker-compose up -d
EOF
}
output "public_ip" {
value = aws_instance.instance.public_ip
}