Skip to content

Commit 9c3ef19

Browse files
authored
Break cycles in module dependencies (#4)
module.metaflow-metadata-service depended upon module.metaflow-datastore for database, but at the same time the latter also depended upon former for ingress rule. We break the dependency by moving ingress rule over to the former module.
1 parent daf8597 commit 9c3ef19

File tree

6 files changed

+47
-21
lines changed

6 files changed

+47
-21
lines changed

iam.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,25 @@ resource "aws_iam_role_policy" "grant_cloudwatch" {
248248
role = aws_iam_role.batch_s3_task_role.name
249249
policy = data.aws_iam_policy_document.cloudwatch.json
250250
}
251+
252+
# Create ECS Task Execution IAM role and policy to run ECS tasks
253+
resource "aws_iam_role" "ecsTaskExecutionRole" {
254+
name = "${var.resource_prefix}ecsTaskExecutionRole${var.resource_suffix}"
255+
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
256+
}
257+
258+
data "aws_iam_policy_document" "assume_role_policy" {
259+
statement {
260+
actions = ["sts:AssumeRole"]
261+
262+
principals {
263+
type = "Service"
264+
identifiers = ["ecs-tasks.amazonaws.com"]
265+
}
266+
}
267+
}
268+
269+
resource "aws_iam_role_policy_attachment" "ecsTaskExecutionRole_policy" {
270+
role = aws_iam_role.ecsTaskExecutionRole.name
271+
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
272+
}

main.tf

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ module "metaflow-datastore" {
44
resource_prefix = local.resource_prefix
55
resource_suffix = local.resource_suffix
66

7-
metadata_service_security_group_id = module.metaflow-metadata-service.metadata_service_security_group_id
8-
metaflow_vpc_id = var.vpc_id
9-
subnet1_id = var.subnet1_id
10-
subnet2_id = var.subnet2_id
7+
metaflow_vpc_id = var.vpc_id
8+
subnet1_id = var.subnet1_id
9+
subnet2_id = var.subnet2_id
1110

1211
db_engine = var.datastore_db_engine
1312
db_engine_version = var.datastore_db_engine_version
1413

1514
standard_tags = var.tags
1615
}
1716

17+
# depends-on: metaflow-datastore
1818
module "metaflow-metadata-service" {
1919
source = "./modules/metadata-service"
2020

@@ -26,8 +26,9 @@ module "metaflow-metadata-service" {
2626
database_name = module.metaflow-datastore.database_name
2727
database_password = module.metaflow-datastore.database_password
2828
database_username = module.metaflow-datastore.database_username
29+
database_sg_id = module.metaflow-datastore.database_sg_id
2930
datastore_s3_bucket_kms_key_arn = module.metaflow-datastore.datastore_s3_bucket_kms_key_arn
30-
fargate_execution_role_arn = module.metaflow-computation.ecs_execution_role_arn
31+
fargate_execution_role_arn = aws_iam_role.ecsTaskExecutionRole.arn
3132
iam_partition = var.iam_partition
3233
metadata_service_container_image = local.metadata_service_container_image
3334
metaflow_vpc_id = var.vpc_id
@@ -41,6 +42,7 @@ module "metaflow-metadata-service" {
4142
standard_tags = var.tags
4243
}
4344

45+
# depends-on: metaflow-datastore, metaflow-metadata-service
4446
module "metaflow-ui" {
4547
source = "./modules/ui"
4648
count = var.ui_certificate_arn == "" ? 0 : 1
@@ -52,7 +54,7 @@ module "metaflow-ui" {
5254
database_password = module.metaflow-datastore.database_password
5355
database_username = module.metaflow-datastore.database_username
5456
datastore_s3_bucket_kms_key_arn = module.metaflow-datastore.datastore_s3_bucket_kms_key_arn
55-
fargate_execution_role_arn = module.metaflow-computation.ecs_execution_role_arn
57+
fargate_execution_role_arn = aws_iam_role.ecsTaskExecutionRole.arn
5658
iam_partition = var.iam_partition
5759
metaflow_vpc_id = var.vpc_id
5860
rds_master_instance_endpoint = module.metaflow-datastore.rds_master_instance_endpoint
@@ -78,6 +80,7 @@ module "metaflow-ui" {
7880

7981
}
8082

83+
# depends-on:
8184
module "metaflow-computation" {
8285
source = "./modules/computation"
8386

@@ -98,6 +101,7 @@ module "metaflow-computation" {
98101
standard_tags = var.tags
99102
}
100103

104+
# depends-on: metaflow-computation, metaflow-datastore
101105
module "metaflow-step-functions" {
102106
source = "./modules/step-functions"
103107

modules/datastore/rds.tf

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ resource "aws_security_group" "rds_security_group" {
2626
tags = var.standard_tags
2727
}
2828

29-
# ingress only from port 5432
30-
resource "aws_security_group_rule" "rds_sg_ingress" {
31-
type = "ingress"
32-
from_port = 5432
33-
to_port = 5432
34-
protocol = "tcp"
35-
source_security_group_id = var.metadata_service_security_group_id
36-
security_group_id = aws_security_group.rds_security_group.id
37-
}
38-
3929
# egress to anywhere
4030
resource "aws_security_group_rule" "rds_sg_egress" {
4131
type = "egress"

modules/datastore/variables.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ variable "db_username" {
3131
default = "metaflow"
3232
}
3333

34-
variable "metadata_service_security_group_id" {
35-
type = string
36-
description = "The security group ID used by the MetaData service. We'll grant this access to our DB."
37-
}
38-
3934
variable "metaflow_vpc_id" {
4035
type = string
4136
description = "ID of the Metaflow VPC this SageMaker notebook instance is to be deployed in"

modules/metadata-service/ec2.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ resource "aws_security_group" "metadata_service_security_group" {
4444
)
4545
}
4646

47+
# Inject a ingress rule to RDS's sg to allow ingress only from port 5432
48+
resource "aws_security_group_rule" "rds_sg_ingress" {
49+
type = "ingress"
50+
from_port = 5432
51+
to_port = 5432
52+
protocol = "tcp"
53+
source_security_group_id = aws_security_group.metadata_service_security_group.id
54+
security_group_id = var.database_sg_id
55+
}
56+
4757
resource "aws_lb" "this" {
4858
name = "${var.resource_prefix}nlb${var.resource_suffix}"
4959
internal = true

modules/metadata-service/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ variable "database_username" {
2525
description = "The database username"
2626
}
2727

28+
variable "database_sg_id" {
29+
type = string
30+
description = "We will use this to add a ingress rule to RDS sg to allow metadata service access"
31+
}
32+
2833
variable "datastore_s3_bucket_kms_key_arn" {
2934
type = string
3035
description = "The ARN of the KMS key used to encrypt the Metaflow datastore S3 bucket"

0 commit comments

Comments
 (0)