Skip to content

Commit 8d7a7df

Browse files
authored
Merge branch 'develop' into feature/made14-NRL-1231-delete-invalid-pointers
2 parents 23acc87 + 8f8f085 commit 8d7a7df

File tree

28 files changed

+624
-16
lines changed

28 files changed

+624
-16
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ override.tf.json
4242
*_override.tf
4343
*_override.tf.json
4444

45+
# Ignore output of data object
46+
terraform/account-wide-infrastructure/modules/glue/files/src.zip
47+
4548
# Include override files you do wish to add to version control using negated pattern
4649
#
4750
# !example_override.tf
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module "dev-athena" {
2+
source = "../modules/athena"
3+
name_prefix = "nhsd-nrlf--dev"
4+
target_bucket_name = module.dev-glue.target_bucket_name
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module "dev-glue" {
2+
source = "../modules/glue"
3+
name_prefix = "nhsd-nrlf--dev"
4+
python_version = 3
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
resource "aws_athena_database" "reporting-db" {
2+
name = var.database
3+
4+
bucket = var.target_bucket_name
5+
6+
encryption_configuration {
7+
encryption_option = "SSE_KMS"
8+
kms_key = aws_kms_key.athena.arn
9+
}
10+
11+
force_destroy = true
12+
}
13+
14+
resource "aws_athena_workgroup" "athena" {
15+
name = "${var.name_prefix}-athena-wg"
16+
17+
configuration {
18+
enforce_workgroup_configuration = true
19+
publish_cloudwatch_metrics_enabled = true
20+
21+
result_configuration {
22+
output_location = "s3://{aws_s3_bucket.athena.bucket}/output/"
23+
24+
encryption_configuration {
25+
encryption_option = "SSE_KMS"
26+
kms_key_arn = aws_kms_key.athena.arn
27+
}
28+
}
29+
}
30+
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "aws_kms_key" "athena" {
2+
}
3+
4+
resource "aws_kms_alias" "athena" {
5+
name = "alias/${var.name_prefix}-athena"
6+
target_key_id = aws_kms_key.athena.key_id
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "workgroup" {
2+
value = aws_athena_workgroup.athena
3+
}
4+
5+
output "bucket" {
6+
value = aws_s3_bucket.athena
7+
}
8+
9+
output "database" {
10+
value = aws_athena_database.reporting-db
11+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
resource "aws_s3_bucket" "athena" {
2+
bucket = "${var.name_prefix}-athena"
3+
}
4+
5+
resource "aws_s3_bucket_policy" "athena" {
6+
bucket = "${var.name_prefix}-athena"
7+
8+
policy = jsonencode({
9+
Version = "2012-10-17"
10+
Id = "athena-policy"
11+
Statement = [
12+
{
13+
Sid = "HTTPSOnly"
14+
Effect = "Deny"
15+
Principal = {
16+
"AWS" : "*"
17+
}
18+
Action = "s3:*"
19+
Resource = [
20+
aws_s3_bucket.athena.arn,
21+
"${aws_s3_bucket.athena.arn}/*",
22+
]
23+
Condition = {
24+
Bool = {
25+
"aws:SecureTransport" = "false"
26+
}
27+
}
28+
},
29+
]
30+
})
31+
}
32+
33+
resource "aws_s3_bucket_public_access_block" "athena-public-access-block" {
34+
bucket = aws_s3_bucket.athena.id
35+
36+
block_public_acls = true
37+
block_public_policy = true
38+
ignore_public_acls = true
39+
restrict_public_buckets = true
40+
}
41+
42+
43+
resource "aws_s3_bucket_server_side_encryption_configuration" "athena" {
44+
bucket = aws_s3_bucket.athena.bucket
45+
rule {
46+
apply_server_side_encryption_by_default {
47+
sse_algorithm = "aws:kms"
48+
kms_master_key_id = aws_kms_key.athena.arn
49+
}
50+
}
51+
52+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
variable "database" {
2+
description = "What the db will be called"
3+
default = "nrl_reporting"
4+
}
5+
6+
variable "name_prefix" {
7+
type = string
8+
description = "The prefix to apply to all resources in the module."
9+
}
10+
11+
variable "target_bucket_name" {
12+
type = string
13+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Create Glue Data Catalog Database
2+
resource "aws_glue_catalog_database" "raw_log_database" {
3+
name = "${var.name_prefix}-raw_log"
4+
location_uri = "${aws_s3_bucket.source-data-bucket.id}/"
5+
}
6+
7+
# Create Glue Crawler
8+
resource "aws_glue_crawler" "raw_log_crawler" {
9+
name = "${var.name_prefix}-raw-log-crawler"
10+
database_name = aws_glue_catalog_database.raw_log_database.name
11+
role = aws_iam_role.glue_service_role.name
12+
s3_target {
13+
path = "${aws_s3_bucket.source-data-bucket.id}/"
14+
}
15+
schema_change_policy {
16+
delete_behavior = "LOG"
17+
}
18+
configuration = jsonencode({
19+
"Version" : 1.0,
20+
"Grouping" : {
21+
"TableGroupingPolicy" : "CombineCompatibleSchemas"
22+
}
23+
})
24+
}
25+
resource "aws_glue_trigger" "raw_log_trigger" {
26+
name = "${var.name_prefix}-org-report-trigger"
27+
type = "ON_DEMAND"
28+
actions {
29+
crawler_name = aws_glue_crawler.raw_log_crawler.name
30+
}
31+
}
32+
33+
resource "aws_glue_job" "glue_job" {
34+
name = "${var.name_prefix}-glue-job"
35+
role_arn = aws_iam_role.glue_service_role.arn
36+
description = "Transfer logs from source to bucket"
37+
glue_version = "4.0"
38+
worker_type = "G.1X"
39+
timeout = 2880
40+
max_retries = 1
41+
number_of_workers = 2
42+
command {
43+
name = "glueetl"
44+
python_version = var.python_version
45+
script_location = "s3://${aws_s3_bucket.code-bucket.id}/main.py"
46+
}
47+
48+
default_arguments = {
49+
"--enable-auto-scaling" = "true"
50+
"--enable-continous-cloudwatch-log" = "true"
51+
"--datalake-formats" = "delta"
52+
"--source-path" = "s3://${aws_s3_bucket.source-data-bucket.id}/" # Specify the source S3 path
53+
"--destination-path" = "s3://${aws_s3_bucket.target-data-bucket.id}/" # Specify the destination S3 path
54+
"--job-name" = "poc-glue-job"
55+
"--enable-continuous-log-filter" = "true"
56+
"--enable-metrics" = "true"
57+
"--extra-py-files" = "s3://${aws_s3_bucket.code-bucket.id}/src.zip"
58+
}
59+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
resource "aws_iam_role" "glue_service_role" {
2+
name = "${var.name_prefix}-glue_service_role"
3+
4+
assume_role_policy = jsonencode({
5+
"Version" : "2012-10-17",
6+
"Statement" : [
7+
{
8+
"Effect" : "Allow",
9+
"Principal" : {
10+
"Service" : "glue.amazonaws.com"
11+
},
12+
"Action" : "sts:AssumeRole"
13+
}
14+
]
15+
})
16+
}
17+
18+
resource "aws_iam_role_policy_attachment" "glue_service" {
19+
role = aws_iam_role.glue_service_role.id
20+
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole"
21+
}

0 commit comments

Comments
 (0)