-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
143 lines (130 loc) · 3.04 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
provider "aws" {
region = var.region
allowed_account_ids = [var.account]
}
resource "aws_iam_role" "role" {
tags = var.tags
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = "AssumeRole"
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
}
resource "aws_iam_policy" "policy" {
name = "athena_es_policy"
description = "Read Athena, OpenSearch, Glue"
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Action = [
"athena:StartQueryExecution",
"athena:GetQueryExecution",
"athena:GetQueryResults",
"athena:StopQueryExecution",
"glue:GetDatabase",
"glue:GetTable",
"glue:GetTables",
"glue:GetPartition"
],
Resource = [
"*"
]
},
{
"Sid" : "S3ReadOnly",
"Effect" : "Allow",
"Action" : [
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
"s3:ListMultipartUploadParts"
],
"Resource" : [
"arn:aws:s3:::*",
"arn:aws:s3:::*/*"
]
},
{
"Sid" : "S3WriteResults",
"Effect" : "Allow",
"Action" : [
"s3:PutObject"
],
"Resource" : [
"arn:aws:s3:::${var.results_bucket}",
"arn:aws:s3:::${var.results_bucket}/*"
]
},
{
"Sid" : "ESGet",
Effect = "Allow",
Action = [
"es:ESHttpGet"
],
Resource = "${var.opensearch_arn}/*"
}
]
})
}
resource "aws_iam_role_policy_attachment" "attach" {
role = aws_iam_role.role.name
policy_arn = aws_iam_policy.policy.arn
}
resource "aws_iam_instance_profile" "profile" {
name = "ec2_profile"
role = aws_iam_role.role.name
tags = var.tags
}
resource "aws_key_pair" "auth" {
key_name = "ec2_ssh_key"
public_key = file("${var.public_key_path}/id_rsa.pub")
}
resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
description = "Allow SSH inbound traffic"
vpc_id = var.vpc_id
ingress {
from_port = 22
to_port = 22
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"]
}
}
resource "aws_instance" "ec2" {
ami = var.ami_id
instance_type = var.instance_type
subnet_id = var.subnet_id
vpc_security_group_ids = [
var.security_group_id,
aws_security_group.allow_ssh.id
]
key_name = aws_key_pair.auth.key_name
iam_instance_profile = aws_iam_instance_profile.profile.name
tags = merge(
var.tags,
{
Name = "quilt-search-query"
}
)
}
output "ssh_login" {
description = "SSH login string"
value = "ssh -i id_rsa ec2-user@${aws_instance.ec2.public_ip}"
}