-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
294 lines (247 loc) · 6.56 KB
/
main.tf
File metadata and controls
294 lines (247 loc) · 6.56 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.31"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "ap-south-1"
}
resource "aws_s3_bucket" "mybucket" {
bucket = "my-tf-test-bucket132"
force_destroy = true
tags = {
Name = "My bucket"
Environment = "Dev"
}
}
resource "aws_kinesis_firehose_delivery_stream" "extended_s3_stream" {
name = "myKDFStream"
destination = "extended_s3"
extended_s3_configuration {
role_arn = aws_iam_role.firehose_role.arn
bucket_arn = aws_s3_bucket.mybucket.arn
prefix = "data/year=!{timestamp:yyyy}/month=!{timestamp:MM}/day=!{timestamp:dd}/hour=!{timestamp:HH}/"
error_output_prefix = "errors/year=!{timestamp:yyyy}/month=!{timestamp:MM}/day=!{timestamp:dd}/hour=!{timestamp:HH}/!{firehose:error-output-type}/"
buffering_size = 2
buffering_interval = 60
}
}
data "aws_iam_policy_document" "firehose_assume_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["firehose.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
resource "aws_iam_policy" "policy_s3" {
name = "policy_s3"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = ["s3:*"]
Effect = "Allow"
Resource = "arn:aws:s3:::*",
},
]
})
}
resource "aws_iam_role" "firehose_role" {
name = "firehose_test_role"
assume_role_policy = data.aws_iam_policy_document.firehose_assume_role.json
managed_policy_arns = [aws_iam_policy.policy_s3.arn]
}
data "aws_iam_policy_document" "ec2_assume_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
resource "aws_iam_policy" "policy_kdf" {
name = "policy-kdf"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = ["firehose:*"]
Effect = "Allow"
Resource = "*",
},
]
})
}
resource "aws_iam_role" "ec2_role" {
name = "ec2_test_role"
assume_role_policy = data.aws_iam_policy_document.ec2_assume_role.json
managed_policy_arns = [aws_iam_policy.policy_kdf.arn]
}
resource "aws_key_pair" "deployer" {
key_name = "deployer-key"
public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIE5VRxIGPNSUurPV1LsQ7ebuuLJ7hNILBynbDrFO2xXi rushichaudhari26@gmail.com"
}
resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
description = "Allow SSH traffic"
ingress {
description = "Connect to Internet"
from_port = 0
to_port = 65535
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
description = "Connect to Internet"
from_port = 0
to_port = 65535
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "allow_ssh"
}
}
resource "aws_iam_instance_profile" "ec2_profile" {
name = "ec2_profile"
role = aws_iam_role.ec2_role.name
}
resource "aws_instance" "my_instance" {
ami = "ami-0a0f1259dd1c90938"
key_name = aws_key_pair.deployer.key_name
vpc_security_group_ids = [aws_security_group.allow_ssh.id]
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
instance_type = "t2.nano"
user_data = <<-EOF
#!/bin/bash
mkdir /tmp/data
chmod +777 /tmp/data
mkdir /tmp/generator
chmod +777 /tmp/generator
echo yes | sudo yum install aws-kinesis-agent
echo yes | sudo yum install python
echo yes | sudo yum install pip
cd /etc/aws-kinesis
sudo rm agent.json
wget https://raw.githubusercontent.com/Reyano132/AWSStreamingDataProcessing/main/agent.json
chmod +777 agent.json
sudo service aws-kinesis-agent start
cd /tmp/generator
wget https://raw.githubusercontent.com/Reyano132/AWSStreamingDataProcessing/main/dataGenerator.py
pip install faker
python dataGenerator.py
echo "Done!!" > result.txt
EOF
tags = {
Name = "test-spot2nano"
}
}
resource "aws_glue_catalog_database" "mydatabase" {
name = "mydatabase"
}
resource "aws_glue_crawler" "mycrawler" {
database_name = aws_glue_catalog_database.mydatabase.name
name = "mycrawler"
role = aws_iam_role.glue_role.arn
s3_target {
path = "s3://${aws_s3_bucket.mybucket.bucket}/data/"
}
}
data "aws_iam_policy_document" "glue_assume_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["glue.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
resource "aws_iam_policy" "policy_glue" {
name = "policy-kdf"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"glue:*",
"s3:GetBucketLocation",
"s3:ListBucket",
"s3:ListAllMyBuckets",
"s3:GetBucketAcl",
"ec2:DescribeVpcEndpoints",
"ec2:DescribeRouteTables",
"ec2:CreateNetworkInterface",
"ec2:DeleteNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DescribeSecurityGroups",
"ec2:DescribeSubnets",
"ec2:DescribeVpcAttribute",
"iam:ListRolePolicies",
"iam:GetRole",
"iam:GetRolePolicy",
"cloudwatch:PutMetricData"
]
Effect = "Allow"
Resource = "*",
},
{
Action = [
"s3:CreateBucket",
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject"
]
Effect = "Allow"
Resource = "*",
},
{
Action = [
"s3:GetObject"
]
Effect = "Allow"
Resource = [
"arn:aws:s3:::crawler-public*",
"arn:aws:s3:::aws-glue-*"
],
},
{
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Effect = "Allow"
Resource = [
"arn:aws:logs:*:*:*:/aws-glue/*"
]
},
{
Action = [
"ec2:CreateTags",
"ec2:DeleteTags"
]
Effect = "Allow"
Resource = [
"arn:aws:ec2:*:*:network-interface/*",
"arn:aws:ec2:*:*:security-group/*",
"arn:aws:ec2:*:*:instance/*"
]
}
]
})
}
resource "aws_iam_role" "glue_role" {
name = "glue_test_role"
assume_role_policy = data.aws_iam_policy_document.glue_assume_role.json
managed_policy_arns = [aws_iam_policy.policy_glue.arn]
}