Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial version #5565

Merged
merged 65 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
feaf165
Initial version
madhu-k-sr2 Apr 3, 2024
d259b1f
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 4, 2024
f3275a5
v2b-dms-module-added-dbname-to-list-var
madhu-k-sr2 Apr 4, 2024
3aef8e8
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 4, 2024
d0e1d3b
v3-dms-module-tf-files-movedout
madhu-k-sr2 Apr 4, 2024
2405ebd
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 4, 2024
15e23b7
v4-dms-module-new-tf-files-added
madhu-k-sr2 Apr 4, 2024
8e6f51d
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 5, 2024
a56c4fb
v5-dms-module-multiple-corrections
madhu-k-sr2 Apr 5, 2024
4a1e81a
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 5, 2024
0dd9609
v6-dms-module-tags-added
madhu-k-sr2 Apr 5, 2024
e6b503d
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 5, 2024
e6142c5
v6-dms-module-target-output-parquet
madhu-k-sr2 Apr 5, 2024
892c5b8
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 5, 2024
763bf85
v7-dms-module-validation-enabled
madhu-k-sr2 Apr 8, 2024
538a0e4
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 8, 2024
8ba8955
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 8, 2024
d9e9581
v8-dms-module-t3small-used
madhu-k-sr2 Apr 8, 2024
2ccbb64
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 8, 2024
4940edf
v9-dms-module-t3medium-used
madhu-k-sr2 Apr 8, 2024
3cba307
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 8, 2024
5879142
v10-dms-module-task-log-enabled
madhu-k-sr2 Apr 8, 2024
c2e1ccf
v10b-dms-module-task-log-enabled
madhu-k-sr2 Apr 8, 2024
117ce6b
v10c-dms-module-task-log-enabled
madhu-k-sr2 Apr 8, 2024
5ffd206
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 8, 2024
f140823
v11-dms-module-iam-outputs-corrections
madhu-k-sr2 Apr 8, 2024
132a6ec
v11b-dms-module-iam-outputs-corrections
madhu-k-sr2 Apr 8, 2024
a79bea6
v11d-dms-module-iam-outputs-corrections
madhu-k-sr2 Apr 8, 2024
c58d250
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 9, 2024
5f5ae03
v12-dms-module-remove-data-validation
madhu-k-sr2 Apr 9, 2024
3cfc382
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 9, 2024
2f6d9bf
v12b-dms-module-remove-data-validation
madhu-k-sr2 Apr 9, 2024
cfd9644
v12c-dms-module-remove-data-validation
madhu-k-sr2 Apr 9, 2024
ba4d5c0
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 9, 2024
15cb676
v12d-dms-module-remove-data-validation
madhu-k-sr2 Apr 9, 2024
e6c3a02
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 9, 2024
979884c
v12e-dms-module-remove-data-validation
madhu-k-sr2 Apr 9, 2024
3e2ac73
v12f-dms-module-remove-data-validation
madhu-k-sr2 Apr 9, 2024
949d4e7
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 9, 2024
6bbe794
v12g-dms-module-remove-data-validation
madhu-k-sr2 Apr 9, 2024
dd5d712
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 9, 2024
b1fb943
v13-dms-module-s3-resource-created
madhu-k-sr2 Apr 9, 2024
5743940
v13b-dms-module-s3-resource-created
madhu-k-sr2 Apr 9, 2024
16032f7
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 10, 2024
dfc4af8
v14-dms-module-s3-review-code-changes
madhu-k-sr2 Apr 10, 2024
18735d7
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 10, 2024
573d485
v15-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
b28d585
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 10, 2024
9df022e
v15b-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
a1e5d14
v15c-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
cf7eb39
v15d-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
874c259
v15e-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
588c593
v15f-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
a1af262
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 10, 2024
8e93132
v13c-dms-module-s3-resource-created
madhu-k-sr2 Apr 10, 2024
c567dc4
v15g-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
e8fc7a5
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 10, 2024
6828d73
v15h-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
dfdeee2
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 10, 2024
98c1d54
v15i-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
e96449b
v15j-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
52f2ee4
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 10, 2024
5a373f4
v15k-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
3155e92
Merge branch 'main' of https://github.com/ministryofjustice/modernisa…
madhu-k-sr2 Apr 10, 2024
32b28a3
v15l-dms-module-s3-glue-crawler-added
madhu-k-sr2 Apr 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions terraform/environments/electronic-monitoring-data/dms_data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
data "aws_iam_policy_document" "dms_assume_role" {
statement {
actions = ["sts:AssumeRole"]

principals {
identifiers = ["dms.eu-west-2.amazonaws.com"]
type = "Service"
}
}
}

data "aws_iam_policy_document" "dms_glue_assume_role" {
statement {
actions = ["sts:AssumeRole"]

principals {
identifiers = ["glue.amazonaws.com"]
type = "Service"
}
}
}

data "aws_iam_policy_document" "dms_target_ep_s3_bucket" {
statement {
sid = "EnforceTLSv12orHigher"
principals {
type = "AWS"
identifiers = ["*"]
}
effect = "Deny"
actions = ["s3:*"]
resources = [
aws_s3_bucket.dms_target_ep_s3_bucket.arn,
"${aws_s3_bucket.dms_target_ep_s3_bucket.arn}/*"
]
condition {
test = "NumericLessThan"
variable = "s3:TlsVersion"
values = [1.2]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
resource "aws_glue_connection" "rds_sqlserver_db_glue_connection" {
connection_properties = {
JDBC_CONNECTION_URL = "jdbc:sqlserver://${aws_db_instance.database_2022.endpoint}"
PASSWORD = aws_secretsmanager_secret_version.db_password.secret_string
USERNAME = "admin"
}

name = "rds-sqlserver-db-glue-conn-tf"

physical_connection_requirements {
security_group_id_list = [aws_security_group.db.id]
subnet_id = data.aws_subnet.private_subnets_a.id
availability_zone = data.aws_subnet.private_subnets_a.availability_zone
}
}

resource "aws_glue_catalog_database" "rds_sqlserver_glue_catalog_db" {
name = "rds_sqlserver_dms"
create_table_default_permission {
permissions = ["SELECT"]

principal {
data_lake_principal_identifier = "IAM_ALLOWED_PRINCIPALS"
}
}
}

resource "aws_glue_crawler" "rds-sqlserver-db-glue-crawler" {
name = "rds-sqlserver-${aws_db_instance.database_2022.identifier}-tf"
role = aws_iam_role.dms-glue-crawler-role.arn
database_name = aws_glue_catalog_database.rds_sqlserver_glue_catalog_db.name
description = "Crawler to fetch database names"
# table_prefix = "your_table_prefix"

jdbc_target {
connection_name = aws_glue_connection.rds_sqlserver_db_glue_connection.name
path = "%"
}
tags = merge(
local.tags,
{
Resource_Type = "RDS SQLServer Glue Crawler for DMS",
}
)

# provisioner "local-exec" {
# command = "aws glue start-crawler --name ${self.name}"
# }
}
83 changes: 83 additions & 0 deletions terraform/environments/electronic-monitoring-data/dms_iam.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Database Migration Service requires the below IAM Roles to be created before replication instances can be created.

# Define IAM role for DMS S3 Endpoint
resource "aws_iam_role" "dms-endpoint-role" {
name = "dms-endpoint-access-role-tf"
assume_role_policy = data.aws_iam_policy_document.dms_assume_role.json
}

# Define S3 IAM policy for DMS S3 Endpoint
resource "aws_iam_policy" "dms-s3-ep-role-policy" {
name = "dms-s3-target-ep-policy"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DMSAccess",
"Effect": "Allow",
"Action": [
"s3:GetBucketLocation",
"s3:ListBucket"
],
"Resource": "${aws_s3_bucket.dms_target_ep_s3_bucket.arn}"
},
{
"Sid": "DMSObjectActions",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": "${aws_s3_bucket.dms_target_ep_s3_bucket.arn}/*"
}
]
}
matt-heery marked this conversation as resolved.
Show resolved Hide resolved
EOF
}

# Attach predefined IAM Policy to the Role for DMS S3 Endpoint
resource "aws_iam_role_policy_attachment" "dms-endpoint-role" {
role = aws_iam_role.dms-endpoint-role.name
policy_arn = aws_iam_policy.dms-s3-ep-role-policy.arn
}

# ==========================================================================

# Create DMS VPC EC2 Role
resource "aws_iam_role" "dms-vpc-role" {
name = "dms-vpc-mng-role-tf"
assume_role_policy = data.aws_iam_policy_document.dms_assume_role.json
}

# Attach IAM Policy to the predefined DMS VPC EC2 Role
resource "aws_iam_role_policy_attachment" "dms-vpc-role-AmazonDMSVPCManagementRole" {
role = aws_iam_role.dms-vpc-role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSVPCManagementRole"
}

# ==========================================================================

resource "aws_iam_role" "dms-cloudwatch-logs-role" {
name = "dms-cloudwatch-logs-role-tf"
assume_role_policy = data.aws_iam_policy_document.dms_assume_role.json
}

resource "aws_iam_role_policy_attachment" "dms-cloudwatch-logs-role-AmazonDMSCloudWatchLogsRole" {
role = aws_iam_role.dms-cloudwatch-logs-role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSCloudWatchLogsRole"
}

resource "aws_iam_role" "dms-glue-crawler-role" {
name = "dms-glue-crawler-role-tf"
assume_role_policy = data.aws_iam_policy_document.dms_glue_assume_role.json
managed_policy_arns = ["arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole"]
}

resource "aws_iam_role_policy_attachment" "dms-glue-crawler-role" {
role = aws_iam_role.dms-glue-crawler-role.name
policy_arn = aws_iam_policy.dms-s3-ep-role-policy.arn
}
# ==========================================================================
25 changes: 25 additions & 0 deletions terraform/environments/electronic-monitoring-data/dms_main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module "dms_task" {
source = "./modules/dms"

for_each = toset(var.database_list)

database_name = each.key

# DMS Source Endpoint Inputs
rds_db_security_group_id = aws_security_group.db.id
rds_db_server_name = split(":", aws_db_instance.database_2022.endpoint)[0]
rds_db_instance_port = aws_db_instance.database_2022.port
rds_db_username = aws_db_instance.database_2022.username
rds_db_instance_pasword = aws_db_instance.database_2022.password

# DMS Target Endpoint Inputs
target_s3_bucket_name = aws_s3_bucket.dms_target_ep_s3_bucket.id
ep_service_access_role_arn = aws_iam_role.dms-endpoint-role.arn

# DMS Migration Task Inputs
dms_replication_instance_arn = aws_dms_replication_instance.dms_replication_instance.replication_instance_arn
rep_task_settings_filepath = trimspace(file("${path.module}/dms_replication_task_settings.json"))
rep_task_table_mapping_filepath = trimspace(file("${path.module}/dms_rep_task_table_mappings.json"))

local_tags = local.tags
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"rules": [
{
"rule-type": "selection",
"rule-id": "054476697",
"rule-name": "054476697",
"object-locator": {
"schema-name": "%",
"table-name": "%"
},
"rule-action": "include",
"filters": [],
"parallel-load": null,
"isAutoSegmentationChecked": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Create a new replication subnet group
resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" {
replication_subnet_group_description = "RDS subnet group"
replication_subnet_group_id = "rds-replication-subnet-group-tf"

subnet_ids = tolist(aws_db_subnet_group.db.subnet_ids)

tags = merge(
local.tags,
{
Resource_Type = "DMS Replication Subnet Group",
}
)

# explicit depends_on is needed since this resource doesn't reference the role or policy attachment
depends_on = [aws_iam_role_policy_attachment.dms-vpc-role-AmazonDMSVPCManagementRole]
}

# ==========================================================================

# Create a new replication instance

resource "aws_dms_replication_instance" "dms_replication_instance" {
allocated_storage = var.dms_allocated_storage_gib
apply_immediately = true
auto_minor_version_upgrade = true
availability_zone = var.dms_availability_zone
engine_version = var.dms_engine_version
# kms_key_arn = "arn:aws:kms:eu-west-2:800964199911:key/b7f54acb-16a3-4958-9340-3bdf5f5842d8"
multi_az = false
# preferred_maintenance_window = "sun:10:30-sun:14:30"
publicly_accessible = false
replication_instance_class = var.dms_replication_instance_class
replication_instance_id = "dms-replication-instance-tf"
replication_subnet_group_id = aws_dms_replication_subnet_group.dms_replication_subnet_group.id

tags = merge(
local.tags,
{
Resource_Type = "DMS Replication Instance",
}
)

vpc_security_group_ids = [
aws_security_group.dms_ri_security_group.id,
]

depends_on = [
aws_iam_role_policy_attachment.dms-endpoint-role,
aws_iam_role_policy_attachment.dms-cloudwatch-logs-role-AmazonDMSCloudWatchLogsRole,
aws_iam_role_policy_attachment.dms-vpc-role-AmazonDMSVPCManagementRole
]
}
Loading