Skip to content

Commit bc6ea6e

Browse files
Fixed minor issues for ecs IAM scripts (#70)
* Fixed minor issues for ecs IAM scripts * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * MANIFEST.in file minor changes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Minor code change --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 7da81de commit bc6ea6e

File tree

9 files changed

+149
-27
lines changed

9 files changed

+149
-27
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,10 @@ ipython_config.py
113113

114114
# Coverage
115115
.coverage
116+
117+
# Terraform
118+
**/.terraform/**
119+
**/*.tfstate*
120+
**/.terraform.lock.hcl
121+
**/*.tfvars
122+
**/*.plan

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [UNRELEASED]
99

10+
### Changed
11+
12+
- Changed the `name` variable / attribute to `prefix` to follow uniformity across all executor plugins
13+
- Changed the **dict** type of defaults to `ExecutorInfraDefaults` & `ExecutorPluginDefaults`
14+
- Minor changes in `MANIFEST.in` file
15+
16+
### Added
17+
18+
- Added a tftpl file for ECS executor to output the configuration
19+
1020
## [0.32.0] - 2023-10-06
1121

1222
### Changed

MANIFEST.in

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
include VERSION
22
include requirements.txt
3+
include covalent_ecs_plugin/assets/infra/*
4+
include covalent_ecs_plugin/assets/infra/*.swp
35
include covalent_ecs_plugin/assets/infra/*.tf
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[ecs]
2+
credentials=${credentials}
3+
profile=${profile}
4+
region=${region}
5+
s3_bucket_name=${s3_bucket_name}
6+
ecs_cluster_name=${ecs_cluster_name}
7+
ecs_task_execution_role_name=${ecs_task_execution_role_name}
8+
ecs_task_role_name=${ecs_task_role_name}
9+
ecs_task_subnet_id=${ecs_task_subnet_id}
10+
ecs_task_security_group_id=${ecs_task_security_group_id}
11+
ecs_task_log_group_name=${ecs_task_log_group_name}
12+
vcpu=${vcpu}
13+
memory=${memory}
14+
cache_dir=${cache_dir}
15+
poll_freq=${poll_freq}

covalent_ecs_plugin/assets/infra/iam.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ data "aws_iam_policy_document" "ecs_tasks_execution_role" {
1010
}
1111

1212
resource "aws_iam_role" "ecs_tasks_execution_role" {
13-
name = "${var.name}-task-execution-role"
13+
name = "${var.prefix}-task-execution-role"
1414
assume_role_policy = data.aws_iam_policy_document.ecs_tasks_execution_role.json
1515
}
1616

@@ -20,7 +20,7 @@ resource "aws_iam_role_policy_attachment" "ecs_tasks_execution_role" {
2020
}
2121

2222
resource "aws_iam_role_policy" "task_policy" {
23-
name = "${var.name}-task-policy"
23+
name = "${var.prefix}-task-policy"
2424
role = aws_iam_role.task_role.id
2525

2626
policy = jsonencode({
@@ -49,7 +49,7 @@ resource "aws_iam_role_policy" "task_policy" {
4949
}
5050

5151
resource "aws_iam_role" "task_role" {
52-
name = "${var.name}-task-role"
52+
name = "${var.prefix}-task-role"
5353

5454
assume_role_policy = jsonencode({
5555
"Version" : "2012-10-17",

covalent_ecs_plugin/assets/infra/main.tf

+31-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ provider "aws" {
1919
}
2020

2121
resource "aws_s3_bucket" "bucket" {
22-
bucket = "${var.name}-bucket"
22+
bucket = "${var.prefix}-bucket"
2323
force_destroy = true
2424
}
2525

@@ -38,7 +38,7 @@ resource "aws_s3_bucket_acl" "bucket_acl" {
3838
}
3939

4040
resource "aws_ecr_repository" "ecr_repository" {
41-
name = "${var.name}-ecr-repo"
41+
name = "${var.prefix}-ecr-repo"
4242
image_tag_mutability = "IMMUTABLE"
4343

4444
force_delete = true
@@ -49,11 +49,11 @@ resource "aws_ecr_repository" "ecr_repository" {
4949
}
5050

5151
resource "aws_cloudwatch_log_group" "log_group" {
52-
name = "${var.name}-log-group"
52+
name = "${var.prefix}-log-group"
5353
}
5454

5555
resource "aws_ecs_cluster" "ecs_cluster" {
56-
name = "${var.name}-ecs-cluster"
56+
name = "${var.prefix}-ecs-cluster"
5757

5858
configuration {
5959
execute_command_configuration {
@@ -64,3 +64,30 @@ resource "aws_ecs_cluster" "ecs_cluster" {
6464
}
6565
}
6666
}
67+
68+
# Executor Covalent config section
69+
data template_file executor_config {
70+
template = "${file("${path.module}/ecs.conf.tftpl")}"
71+
72+
vars = {
73+
credentials=var.credentials
74+
profile=var.profile
75+
region=var.aws_region
76+
s3_bucket_name=aws_s3_bucket.bucket.id
77+
ecs_cluster_name=aws_ecs_cluster.ecs_cluster.name
78+
ecs_task_execution_role_name=aws_iam_role.ecs_tasks_execution_role.name
79+
ecs_task_role_name=aws_iam_role.task_role.name
80+
ecs_task_subnet_id=module.vpc.public_subnets[0]
81+
ecs_task_security_group_id=aws_security_group.sg.id
82+
ecs_task_log_group_name=aws_cloudwatch_log_group.log_group.name
83+
vcpu=var.vcpus
84+
memory=var.memory
85+
cache_dir=var.cache_dir
86+
poll_freq=var.poll_freq
87+
}
88+
}
89+
90+
resource local_file executor_config {
91+
content = data.template_file.executor_config.rendered
92+
filename = "${path.module}/ecs.conf"
93+
}

covalent_ecs_plugin/assets/infra/networking.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module "vpc" {
1919

2020
create_vpc = (var.vpc_id == "")
2121

22-
name = "${var.name}-vpc"
22+
name = "${var.prefix}-vpc"
2323
cidr = var.vpc_cidr
2424

2525
azs = ["${var.aws_region}a"]
@@ -35,7 +35,7 @@ module "vpc" {
3535
}
3636

3737
resource "aws_security_group" "sg" {
38-
name = "${var.name}-sg"
38+
name = "${var.prefix}-sg"
3939
description = "Allow traffic to Covalent server"
4040
vpc_id = "${var.vpc_id == "" ? module.vpc.vpc_id : var.vpc_id}"
4141

covalent_ecs_plugin/assets/infra/variables.tf

+36-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
variable "name" {
17+
variable "prefix" {
1818
default = "covalent-ecs-ft"
1919
description = "Name used to prefix AWS resources"
2020
}
@@ -38,3 +38,38 @@ variable "vpc_cidr" {
3838
default = "10.0.0.0/24"
3939
description = "VPC CIDR range"
4040
}
41+
42+
variable "cache_dir" {
43+
type = string
44+
default = "/tmp/covalent"
45+
description = "Path on local machine where temporary files are generated"
46+
}
47+
48+
variable "poll_freq" {
49+
type = number
50+
default = 5
51+
description = "Frequency with which to poll AWS batch for the result object"
52+
}
53+
54+
variable "vcpus" {
55+
type = number
56+
default = 2
57+
description = "Number of vcpus a batch job will consume by default"
58+
}
59+
60+
variable "memory" {
61+
type = number
62+
default = 2
63+
description = "Memory in GB for the batch job"
64+
}
65+
66+
variable "credentials" {
67+
type = string
68+
default = ""
69+
description = "Path to the AWS shared configuration file"
70+
}
71+
72+
variable "profile" {
73+
type = string
74+
description = "AWS profile used during execution"
75+
}

covalent_ecs_plugin/ecs.py

+43-17
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,51 @@
2929
from covalent._shared_files.config import get_config
3030
from covalent._shared_files.logger import app_log
3131
from covalent_aws_plugins import AWSExecutor
32+
from pydantic import BaseModel
3233

3334
from .utils import _execute_partial_in_threadpool, _load_pickle_file
3435

35-
_EXECUTOR_PLUGIN_DEFAULTS = {
36-
"credentials": "",
37-
"profile": "",
38-
"region": "",
39-
"s3_bucket_name": "covalent-fargate-task-resources",
40-
"ecs_cluster_name": "covalent-fargate-cluster",
41-
"ecs_task_execution_role_name": "ecsTaskExecutionRole",
42-
"ecs_task_role_name": "CovalentFargateTaskRole",
43-
"ecs_task_subnet_id": "",
44-
"ecs_task_security_group_id": "",
45-
"ecs_task_log_group_name": "covalent-fargate-task-logs",
46-
"vcpu": 0.25,
47-
"memory": 0.5,
48-
"cache_dir": "/tmp/covalent",
49-
"poll_freq": 10,
50-
}
36+
37+
class ExecutorPluginDefaults(BaseModel):
38+
credentials: str = ""
39+
profile: str = ""
40+
region: str = ""
41+
s3_bucket_name: str = "covalent-fargate-task-resources"
42+
ecs_cluster_name: str = "covalent-fargate-cluster"
43+
ecs_task_execution_role_name: str = "ecsTaskExecutionRole"
44+
ecs_task_role_name: str = "CovalentFargateTaskRole"
45+
ecs_task_subnet_id: str = ""
46+
ecs_task_security_group_id: str = ""
47+
ecs_task_log_group_name: str = "covalent-fargate-task-logs"
48+
vcpu: float = 0.25
49+
memory: float = 0.5
50+
cache_dir: str = "/tmp/covalent"
51+
poll_freq: int = 10
52+
53+
54+
class ExecutorInfraDefaults(BaseModel):
55+
"""
56+
Configuration values for provisioning AWS Batch cloud infrastructure
57+
"""
58+
59+
prefix: str = ""
60+
credentials: str = ""
61+
profile: str = ""
62+
region: str = ""
63+
s3_bucket_name: str = "covalent-fargate-task-resources"
64+
ecs_cluster_name: str = "covalent-fargate-cluster"
65+
ecs_task_execution_role_name: str = "ecsTaskExecutionRole"
66+
ecs_task_role_name: str = "CovalentFargateTaskRole"
67+
ecs_task_subnet_id: str = ""
68+
ecs_task_security_group_id: str = ""
69+
ecs_task_log_group_name: str = "covalent-fargate-task-logs"
70+
vcpu: float = 0.25
71+
memory: float = 0.5
72+
cache_dir: str = "/tmp/covalent"
73+
poll_freq: int = 10
74+
75+
76+
_EXECUTOR_PLUGIN_DEFAULTS = ExecutorPluginDefaults().dict()
5177

5278
EXECUTOR_PLUGIN_NAME = "ECSExecutor"
5379

@@ -215,7 +241,7 @@ async def submit_task(self, task_metadata: Dict, identity: Dict) -> Any:
215241
],
216242
},
217243
],
218-
cpu=str(int(self.vcpu * 1024)),
244+
cpu=str(int(self.vcpu)),
219245
memory=str(int(self.memory * 1024)),
220246
)
221247
await _execute_partial_in_threadpool(partial_func)

0 commit comments

Comments
 (0)