-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
173 lines (148 loc) · 3.84 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Gustavo Kennedy Renkel
# TF para criação de instância na Lightsail
# Utilizaz repo do Ubuntu para configs
# Exige versão dos providers
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.14.0"
}
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 3.0"
}
nginx = {
source = "getstackhead/nginx"
version = "1.3.2"
}
}
}
#################################################
# Define região da AWS com variável
provider "aws" {
region = var.region
}
# Recebe API do Cloudflare
provider "cloudflare" {
api_token = var.api_cloudflare
}
#################################################
# Cria a instância na Lightsail com variáveis
resource "aws_lightsail_instance" "instance" {
name = var.instance
availability_zone = var.instance_availability_zone
blueprint_id = var.instance_blueprintid
bundle_id = var.instance_bundleid
key_pair_name = "terraform"
tags = {
Environment = "Production"
}
# Faz conexão com instância criada por SSH (usuário ubuntu)
connection {
type = "ssh"
user = "ubuntu"
private_key = file("terraform.pem")
host = aws_lightsail_instance.instance.public_ip_address
}
# Faz envio de comandos - atualiza repositórios, cria pastas e reinicia serviços
provisioner "remote-exec" {
inline = [
"sudo apt-get update"
#"sudo apt-get -y install nginx",
#"sudo mkdir /var/www/html/${var.dominio}",
#"sudo systemctl start nginx"
]
}
# Faz envio de comandos - baixa repo para configuração
provisioner "remote-exec" {
inline = [
"sudo apt-add-repository ppa:ansible/ansible --yes && sudo DEBIAN_FRONTEND=noninteractive apt-get -yq install ansible",
"sudo git clone https://github.com/gustavokennedy/ubuntu.git && cd ubuntu && sudo ansible-playbook playbook.yml --extra-vars 'dominio=${var.dominio} db_nome=${var.db_nome} porta_api=${var.porta_api} porta_front=${var.porta_front}'",
]
}
}
# Envia index padrão para Nginx
#provisioner "file" {
# source = "files/index.nginx-debian.html"
# destination = "/tmp/index.nginx-debian.html"
# }
# Envia arquivo de configuração do bloco do Nginx
#provisioner "file" {
# source = "files/nginx.conf"
# destination = "/tmp/nginx.conf"
# }
# Faz envio de comandos - copia arquivos para sudo
#provisioner "remote-exec" {
# inline = [
# "sudo cp /tmp/index.nginx-debian.html /var/www/html/${var.dominio}/",
# "sudo cp /tmp/nginx.conf /etc/nginx/sites-enabled/${var.dominio}.conf"
# ]
# }
# Faz envio de comandos - remove default do Nginx
#provisioner "remote-exec" {
# inline = [
# "sudo rm /etc/nginx/sites-enabled/default"
# ]
# }
#}
# Libera portas
resource "aws_lightsail_instance_public_ports" "instance" {
instance_name = aws_lightsail_instance.instance.name
# React - Frontend
port_info {
protocol = "tcp"
from_port = 3000
to_port = 3000
}
# NodeJS - Backend
port_info {
protocol = "tcp"
from_port = 3333
to_port = 3333
}
# MySQL
port_info {
protocol = "tcp"
from_port = 3306
to_port = 3306
}
# SSH
port_info {
protocol = "tcp"
from_port = 22
to_port = 22
}
# HTTP
port_info {
protocol = "tcp"
from_port = 80
to_port = 80
}
# HTTPS
port_info {
protocol = "tcp"
from_port = 443
to_port = 443
}
# Zabbix-agent
port_info {
protocol = "tcp"
from_port = 10050
to_port = 10050
}
# Vite - Frontend
port_info {
protocol = "tcp"
from_port = 5173
to_port = 5173
}
}
# Cloudflare - cria registro de DNS com IP da instância
resource "cloudflare_record" "www" {
zone_id = var.zone_id
name = var.instance
value = aws_lightsail_instance.instance.public_ip_address
type = "A"
proxied = false
}