-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
151 lines (128 loc) · 3.21 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.35.0"
}
cloudflare = {
source = "cloudflare/cloudflare"
version = "4.28.0"
}
ansible = {
source = "ansible/ansible"
version = "1.3.0"
}
}
}
locals {
domain = "${var.subdomain}.${var.zone_name}"
}
provider "cloudflare" {
api_token = var.cf_api
}
provider "aws" {
region = "us-east-1"
}
data "aws_key_pair" "pplanel" {
key_name = var.keypair
}
data "cloudflare_zone" "cf_zone" {
name = var.zone_name
}
resource "aws_key_pair" "new_kp" {
key_name = "general_kp"
public_key = var.public_key
}
resource "cloudflare_record" "machine" {
zone_id = data.cloudflare_zone.cf_zone.id
name = var.subdomain
value = aws_instance.server01.public_dns
type = "CNAME"
}
resource "aws_instance" "server01" {
ami = var.ami_id
instance_type = var.instance_type
key_name = aws_key_pair.new_kp.key_name
subnet_id = var.public_subnet_id
vpc_security_group_ids = [aws_security_group.allow_ssh_http.id]
associate_public_ip_address = true
iam_instance_profile = aws_iam_instance_profile.ssm_instance_profile.name
root_block_device {
volume_size = "80"
volume_type = "gp3"
encrypted = true
delete_on_termination = true
}
ebs_block_device {
device_name = "/dev/xvda"
volume_size = "200"
volume_type = "gp3"
encrypted = true
delete_on_termination = false
}
}
resource "ansible_host" "host" {
name = local.domain
groups = ["all"]
variables = {
ansible_user = "ubuntu"
ansible_ssh_private_key_file = "{{ lookup('community.general.onepassword', '${var.secret_name}', field='private_key', vault='${var.vault_name}') }}"
}
}
resource "aws_iam_instance_profile" "ssm_instance_profile" {
name = "ssm_instance_profile"
role = aws_iam_role.ssm_iam_role.name
}
resource "aws_iam_role_policy_attachment" "ssm_iam_role_policy_attachment" {
role = aws_iam_role.ssm_iam_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_role" "ssm_iam_role" {
name = "ssm_iam_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "ec2.amazonaws.com"
}
}]
})
}
resource "aws_security_group" "allow_ssh_http" {
name = "allow_ssh"
description = "Allow SSH and in EC2 instace"
vpc_id = var.vpc_id
ingress {
description = "SSH to EC2"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTP to EC2"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTPS to EC2"
from_port = 443
to_port = 443
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 = "allow_ssh_http"
}
}