Skip to content

Commit f2a300e

Browse files
authored
Create dedicated security group for the Compute environment (#24)
* Create dedicated security group for the Compute environment * Allow egress * Expose egress CIDR blocks * Delete vpc_default data source; allow internal traffic
1 parent 61e67e9 commit f2a300e

File tree

10 files changed

+48
-6
lines changed

10 files changed

+48
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ You can find a more complete example that uses this module but also includes set
9898
| <a name="input_api_basic_auth"></a> [api\_basic\_auth](#input\_api\_basic\_auth) | Enable basic auth for API Gateway? (requires key export) | `bool` | `true` | no |
9999
| <a name="input_batch_type"></a> [batch\_type](#input\_batch\_type) | AWS Batch Compute Type ('ec2', 'fargate') | `string` | `"ec2"` | no |
100100
| <a name="input_compute_environment_desired_vcpus"></a> [compute\_environment\_desired\_vcpus](#input\_compute\_environment\_desired\_vcpus) | Desired Starting VCPUs for Batch Compute Environment [0-16] for EC2 Batch Compute Environment (ignored for Fargate) | `number` | `8` | no |
101+
| <a name="input_compute_environment_egress_cidr_blocks"></a> [compute\_environment\_egress\_cidr\_blocks](#input\_compute\_environment\_egress\_cidr\_blocks) | CIDR blocks to which egress is allowed from the Batch Compute environment's security group | `list(string)` | <pre>[<br> "0.0.0.0/0"<br>]</pre> | no |
101102
| <a name="input_compute_environment_instance_types"></a> [compute\_environment\_instance\_types](#input\_compute\_environment\_instance\_types) | The instance types for the compute environment | `list(string)` | <pre>[<br> "c4.large",<br> "c4.xlarge",<br> "c4.2xlarge",<br> "c4.4xlarge",<br> "c4.8xlarge"<br>]</pre> | no |
102103
| <a name="input_compute_environment_max_vcpus"></a> [compute\_environment\_max\_vcpus](#input\_compute\_environment\_max\_vcpus) | Maximum VCPUs for Batch Compute Environment [16-96] | `number` | `64` | no |
103104
| <a name="input_compute_environment_min_vcpus"></a> [compute\_environment\_min\_vcpus](#input\_compute\_environment\_min\_vcpus) | Minimum VCPUs for Batch Compute Environment [0-16] for EC2 Batch Compute Environment (ignored for Fargate) | `number` | `8` | no |
@@ -133,6 +134,7 @@ You can find a more complete example that uses this module but also includes set
133134
| <a name="output_METAFLOW_SFN_DYNAMO_DB_TABLE"></a> [METAFLOW\_SFN\_DYNAMO\_DB\_TABLE](#output\_METAFLOW\_SFN\_DYNAMO\_DB\_TABLE) | AWS DynamoDB table name for tracking AWS Step Functions execution metadata. |
134135
| <a name="output_METAFLOW_SFN_IAM_ROLE"></a> [METAFLOW\_SFN\_IAM\_ROLE](#output\_METAFLOW\_SFN\_IAM\_ROLE) | IAM role for AWS Step Functions to access AWS resources (AWS Batch, AWS DynamoDB). |
135136
| <a name="output_api_gateway_rest_api_id_key_id"></a> [api\_gateway\_rest\_api\_id\_key\_id](#output\_api\_gateway\_rest\_api\_id\_key\_id) | API Gateway Key ID for Metadata Service. Fetch Key from AWS Console [METAFLOW\_SERVICE\_AUTH\_KEY] |
137+
| <a name="output_batch_compute_environment_security_group_id"></a> [batch\_compute\_environment\_security\_group\_id](#output\_batch\_compute\_environment\_security\_group\_id) | The ID of the security group attached to the Batch Compute environment. |
136138
| <a name="output_datastore_s3_bucket_kms_key_arn"></a> [datastore\_s3\_bucket\_kms\_key\_arn](#output\_datastore\_s3\_bucket\_kms\_key\_arn) | The ARN of the KMS key used to encrypt the Metaflow datastore S3 bucket |
137139
| <a name="output_metadata_svc_ecs_task_role_arn"></a> [metadata\_svc\_ecs\_task\_role\_arn](#output\_metadata\_svc\_ecs\_task\_role\_arn) | n/a |
138140
| <a name="output_metaflow_api_gateway_rest_api_id"></a> [metaflow\_api\_gateway\_rest\_api\_id](#output\_metaflow\_api\_gateway\_rest\_api\_id) | The ID of the API Gateway REST API we'll use to accept MetaData service requests to forward to the Fargate API instance |

main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ module "metaflow-computation" {
7878
compute_environment_instance_types = var.compute_environment_instance_types
7979
compute_environment_max_vcpus = var.compute_environment_max_vcpus
8080
compute_environment_min_vcpus = var.compute_environment_min_vcpus
81+
compute_environment_egress_cidr_blocks = var.compute_environment_egress_cidr_blocks
8182
enable_step_functions = var.enable_step_functions
8283
iam_partition = var.iam_partition
8384
metaflow_step_functions_dynamodb_policy = module.metaflow-step-functions.metaflow_step_functions_dynamodb_policy

modules/computation/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ To read more, see [the Metaflow docs](https://docs.metaflow.org/metaflow-on-aws/
1414
|------|-------------|------|---------|:--------:|
1515
| <a name="input_batch_type"></a> [batch\_type](#input\_batch\_type) | AWS Batch Compute Type ('ec2', 'fargate') | `string` | `"ec2"` | no |
1616
| <a name="input_compute_environment_desired_vcpus"></a> [compute\_environment\_desired\_vcpus](#input\_compute\_environment\_desired\_vcpus) | Desired Starting VCPUs for Batch Compute Environment [0-16] for EC2 Batch Compute Environment (ignored for Fargate) | `number` | n/a | yes |
17+
| <a name="input_compute_environment_egress_cidr_blocks"></a> [compute\_environment\_egress\_cidr\_blocks](#input\_compute\_environment\_egress\_cidr\_blocks) | CIDR blocks to which egress is allowed from the Batch Compute environment's security group | `list(string)` | <pre>[<br> "0.0.0.0/0"<br>]</pre> | no |
1718
| <a name="input_compute_environment_instance_types"></a> [compute\_environment\_instance\_types](#input\_compute\_environment\_instance\_types) | The instance types for the compute environment as a comma-separated list | `list(string)` | n/a | yes |
1819
| <a name="input_compute_environment_max_vcpus"></a> [compute\_environment\_max\_vcpus](#input\_compute\_environment\_max\_vcpus) | Maximum VCPUs for Batch Compute Environment [16-96] | `number` | n/a | yes |
1920
| <a name="input_compute_environment_min_vcpus"></a> [compute\_environment\_min\_vcpus](#input\_compute\_environment\_min\_vcpus) | Minimum VCPUs for Batch Compute Environment [0-16] for EC2 Batch Compute Environment (ignored for Fargate) | `number` | n/a | yes |
@@ -32,6 +33,7 @@ To read more, see [the Metaflow docs](https://docs.metaflow.org/metaflow-on-aws/
3233
| Name | Description |
3334
|------|-------------|
3435
| <a name="output_METAFLOW_BATCH_JOB_QUEUE"></a> [METAFLOW\_BATCH\_JOB\_QUEUE](#output\_METAFLOW\_BATCH\_JOB\_QUEUE) | AWS Batch Job Queue ARN for Metaflow |
36+
| <a name="output_batch_compute_environment_security_group_id"></a> [batch\_compute\_environment\_security\_group\_id](#output\_batch\_compute\_environment\_security\_group\_id) | The ID of the security group attached to the Batch Compute environment. |
3537
| <a name="output_batch_job_queue_arn"></a> [batch\_job\_queue\_arn](#output\_batch\_job\_queue\_arn) | The ARN of the job queue we'll use to accept Metaflow tasks |
3638
| <a name="output_ecs_execution_role_arn"></a> [ecs\_execution\_role\_arn](#output\_ecs\_execution\_role\_arn) | The IAM role that grants access to ECS and Batch services which we'll use as our Metadata Service API's execution\_role for our Fargate instance |
3739
| <a name="output_ecs_instance_role_arn"></a> [ecs\_instance\_role\_arn](#output\_ecs\_instance\_role\_arn) | This role will be granted access to our S3 Bucket which acts as our blob storage. |

modules/computation/batch.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ resource "aws_batch_compute_environment" "this" {
4949

5050
# Security group to apply to the instances launched.
5151
security_group_ids = [
52-
data.aws_security_group.vpc_default.id,
52+
aws_security_group.this.id,
5353
]
5454

5555
# Which subnet to launch the instances into.

modules/computation/data.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,3 @@ data "aws_ssm_parameter" "ecs_optimized_cpu_ami" {
77
data "aws_ssm_parameter" "ecs_optimized_gpu_ami" {
88
name = "/aws/service/ecs/optimized-ami/amazon-linux-2/gpu/recommended"
99
}
10-
11-
data "aws_security_group" "vpc_default" {
12-
name = "default"
13-
vpc_id = var.metaflow_vpc_id
14-
}

modules/computation/ec2.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,23 @@ resource "aws_iam_instance_profile" "ecs_instance_role" {
4141
name = local.ecs_instance_role_name
4242
role = aws_iam_role.ecs_instance_role.name
4343
}
44+
45+
resource "aws_security_group" "this" {
46+
name = local.batch_security_group_name
47+
vpc_id = var.metaflow_vpc_id
48+
49+
egress {
50+
from_port = 0
51+
to_port = 0
52+
protocol = "-1"
53+
cidr_blocks = var.compute_environment_egress_cidr_blocks
54+
}
55+
56+
ingress {
57+
from_port = 0
58+
to_port = 0
59+
protocol = "-1"
60+
self = true
61+
description = "internal traffic"
62+
}
63+
}

modules/computation/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ output "ecs_instance_role_arn" {
1717
value = aws_iam_role.ecs_instance_role.arn
1818
description = "This role will be granted access to our S3 Bucket which acts as our blob storage."
1919
}
20+
21+
output "batch_compute_environment_security_group_id" {
22+
value = aws_security_group.this.id
23+
description = "The ID of the security group attached to the Batch Compute environment."
24+
}

modules/computation/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ variable "compute_environment_min_vcpus" {
2424
description = "Minimum VCPUs for Batch Compute Environment [0-16] for EC2 Batch Compute Environment (ignored for Fargate)"
2525
}
2626

27+
variable "compute_environment_egress_cidr_blocks" {
28+
type = list(string)
29+
default = ["0.0.0.0/0"]
30+
description = "CIDR blocks to which egress is allowed from the Batch Compute environment's security group"
31+
}
32+
2733
variable "enable_step_functions" {
2834
default = false
2935
description = "If true, apply policies required for step functions"

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,8 @@ output "ui_alb_arn" {
123123
value = (length(module.metaflow-ui) > 0) ? module.metaflow-ui[0].alb_arn : ""
124124
description = "UI ALB ARN"
125125
}
126+
127+
output "batch_compute_environment_security_group_id" {
128+
value = module.metaflow-computation.batch_compute_environment_security_group_id
129+
description = "The ID of the security group attached to the Batch Compute environment."
130+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ variable "compute_environment_max_vcpus" {
6161
default = 64
6262
}
6363

64+
variable "compute_environment_egress_cidr_blocks" {
65+
type = list(string)
66+
default = ["0.0.0.0/0"]
67+
description = "CIDR blocks to which egress is allowed from the Batch Compute environment's security group"
68+
}
69+
6470
variable "iam_partition" {
6571
type = string
6672
default = "aws"

0 commit comments

Comments
 (0)