-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
163 lines (140 loc) · 3.85 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
terraform {
required_providers {
# hcloud = {
# source = "hetznercloud/hcloud"
# version = "~> 1.45"
# }
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}
# provider "hcloud" {
# token = var.pat
# }
# Configure the AWS Provider
provider "aws" {
region = "ap-south-1"
access_key = var.access_key
secret_key = var.secret_key
}
#------------------For Hetzner cloud------------------
# resource "hcloud_server" "infisical-server" {
# name = var.basic_configurations.name
# datacenter = var.basic_configurations.datacenter
# server_type = var.basic_configurations.server_type
# image = var.basic_configurations.image
# ssh_keys = var.ssh_keys
# public_net {
# ipv4_enabled = true
# ipv6_enabled = false
# }
# connection {
# type = "ssh"
# user = "root"
# host = self.ipv4_address
# timeout = "4m"
# private_key = file(var.pvt_key)
# }
# provisioner "file" {
# source = var.scripts_path
# destination = "/tmp/"
# }
# provisioner "remote-exec" {
# inline = [
# "chmod +x /tmp/script.sh",
# "/tmp/script.sh"
# ]
# }
# }
# ------------------For AWS ------------------
resource "aws_instance" "infisical-instance" {
ami = var.ami_id
instance_type = var.instance_type
key_name = aws_key_pair.infisical-key-pair.key_name
user_data = <<-EOF
#!/bin/bash
echo 'ubuntu ALL=(ALL:ALL) NOPASSWD:ALL' | sudo tee /etc/sudoers.d/ubuntu
EOF
tags = {
Name = "Infisical"
}
connection {
type = "ssh"
user = "ubuntu"
host = self.public_ip
private_key = file(var.private_key_path)
}
provisioner "file" {
source = var.aws_script_path
destination = "/tmp/script-aws.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/script-aws.sh",
"/tmp/script-aws.sh",
]
}
}
resource "aws_security_group" "infisical-firewall" {
name = "infisical"
description = "Rules to limit the traffic to VPN only"
ingress {
from_port = 22
to_port = 22
cidr_blocks = [ "13.127.106.156/32" ]
description = "Allow SSH from VPN IP only."
protocol = "tcp"
}
ingress{
from_port = 443
to_port = 443
cidr_blocks = [ "13.127.106.156/32" ]
description = "Allow requests from VPN IP only."
protocol = "tcp"
}
ingress{
from_port = 80
to_port = 80
cidr_blocks = [ "13.127.106.156/32" ]
description = "Allow requests from VPN IP only."
protocol = "tcp"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
}
resource "aws_key_pair" "infisical-key-pair" {
key_name = var.key_pair_name
public_key = file(var.public_key_path)
}
provider "digitalocean" {
token = var.do_token
}
resource "digitalocean_record" "infisical_domain_mapping" {
domain = "abc.com"
type = "A"
name = var.subdomain_name
value = aws_instance.infisical-instance.public_ip
ttl = 369
# provisioner "local-exec" {
# command = "ansible-playbook -i ${hcloud_server.infisical-server.ipv4_address}, --ssh-extra-args='-o StrictHostKeyChecking=no' -e 'hetzner_pat=${var.pat}' playbook.yaml "
# working_dir = "${path.module}/ansible"
# }
provisioner "local-exec" {
command = "ansible-playbook -i ${aws_instance.infisical-instance.public_ip}, --ssh-extra-args='-o StrictHostKeyChecking=no' -e 'instance_id=${aws_instance.infisical-instance.id}' -e 'security_group_id=${aws_security_group.infisical-firewall.id}' playbook-aws.yaml"
working_dir = "${path.module}/ansible"
}
provisioner "local-exec" {
command = "aws ec2 modify-instance-attribute --instance-id ${aws_instance.infisical-instance.id} --groups ${aws_security_group.infisical-firewall.id}"
}
}