-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.tf
134 lines (119 loc) · 3.48 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
provider "aws" {
region = var.region
profile = "dev2-admin"
}
#---------------S3 Bucket-------------------
resource "random_id" "id" {
byte_length = 8
}
// Define S3 bucket for this demo
// This will generate a unique S3, globally
resource "aws_s3_bucket" "demo_bucket" {
bucket = "athena-dbt-demo-${random_id.id.hex}"
force_destroy = true
tags = {
project_type = var.default_project_type
}
}
// Define sub folder and uploading demo data to s3
resource "aws_s3_object" "raw_data" {
bucket = aws_s3_bucket.demo_bucket.id
// This will upload the file while create this sub folder
key = "/raw_data/women_clothing_ecommerce_reviews.csv"
source = "./data/women_clothing_ecommerce_reviews.csv"
tags = {
project_type = var.default_project_type
}
}
#--------------------------Glue related configuration------------
// Create Glue Catalog Database
resource "aws_glue_catalog_database" "raw_data" {
name = "raw_data_${random_id.id.hex}"
}
// Create role for Glue Crawler service
resource "aws_iam_role" "glue_crawler_role" {
name = "AWSGlueServiceRoleDefault"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "glue.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
// Extra policy required for crawler to access s3 bucket and folder
resource "aws_iam_policy" "glue_crawler_policy_access_s3" {
name = "AWSGlueServiceRole-s3Policy"
path = "/"
description = "This policy will be used for Glue Crawler and Job execution"
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"${aws_s3_bucket.demo_bucket.arn}/raw_data/*"
]
}
]
})
}
// Service role policy to be attached
resource "aws_iam_role_policy_attachment" "glue_service_policy" {
role = aws_iam_role.glue_crawler_role.id
policy_arn = var.glue_service_role_policy
}
resource "aws_iam_role_policy_attachment" "glue_service_s3_folder" {
role = aws_iam_role.glue_crawler_role.id
policy_arn = aws_iam_policy.glue_crawler_policy_access_s3.arn
}
// Define Glue Crawler for raw data
resource "aws_glue_crawler" "raw_data_crawler" {
database_name = aws_glue_catalog_database.raw_data.name
name = "athena_dbt_demo_crawler"
role = aws_iam_role.glue_crawler_role.id
table_prefix = "women_clothes_reviews_"
s3_target {
path = "s3://${aws_s3_bucket.demo_bucket.id}/raw_data"
}
tags = {
project_type = var.default_project_type
}
}
#------------------------------Athena & Athena Adapter--------------------------------------
// Athena database to build models into
resource "aws_glue_catalog_database" "athena_dbt_models" {
name = "athena_dbt_models_${random_id.id.hex}"
description = "Athena database to store dbt models"
}
// Globally unique S3 bucket for Athena to store query results
resource "aws_s3_bucket" "athena_query_result_bucket" {
bucket = "athena-dbt-demo-athena-query-result-bucket-${random_id.id.hex}"
force_destroy = true
tags = {
project_type = var.default_project_type
}
}
// Athena Workgroup
resource "aws_athena_workgroup" "athena-dbt-demo"{
name = "athena-dbt-demo-workgroup"
description = "Athena Workgroup for DBT Demo"
force_destroy = true
configuration {
result_configuration {
output_location = "s3://${aws_s3_bucket.athena_query_result_bucket.id}"
}
}
}