-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
247 lines (226 loc) · 7.02 KB
/
main.tf
File metadata and controls
247 lines (226 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
provider "aws" {
version = "~> 2.10"
region = var.region
profile = var.profile
}
# S3 bucket
resource "aws_s3_bucket" "bucket" {
bucket = var.bucket_name
acl = "private"
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
# Upload training script
resource "aws_s3_bucket_object" "training_script" {
bucket = aws_s3_bucket.bucket.id
key = "scripts/train.tar.gz"
source = "scripts/train.tar.gz"
etag = filemd5(
"scripts/train.tar.gz",
)
}
# ECR Image
module "ecr-image-conversion"{
source = "./modules/ecr-image"
name_prefix = "mammogram"
resource_tags = {}
repository_name = "conversion"
source_image_path = "conversion"
aws_credentials_file = var.credentials_path
}
module "ecr-image-preprocessing"{
source = "./modules/ecr-image"
name_prefix = "mammogram"
resource_tags = {}
repository_name = "preprocessing"
source_image_path = "conversion"
aws_credentials_file = var.credentials_path
}
# ECS Cluster
module "ecs-cluster"{
source = "./modules/ecs-cluster"
name_prefix = "mammogram"
resource_tags = {}
}
# ECS Task
module "ecs-task-conversion"{
source = "./modules/ecs-task"
name_prefix = "mammogram"
environment = {
vpc_id = module.vpc.vpc_id
aws_region = var.region
public_subnets = module.vpc.public_subnets
private_subnets = module.vpc.private_subnets
}
resource_tags = {}
container_image = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${var.region}.amazonaws.com/mammogram-conversion:latest"
container_name = "mammogram-conversion"
container_num_cores = 4
container_ram_gb = 30
ecs_cluster_name = "mammogram-cluster"
ecs_launch_type = "FARGATE"
permitted_s3_buckets = [var.bucket_name]
task_definition_name = "mammogram-conversion"
use_fargate = true
}
# ECS Task
module "ecs-task-preprocessing"{
source = "./modules/ecs-task"
name_prefix = "mammogram"
environment = {
vpc_id = module.vpc.vpc_id
aws_region = var.region
public_subnets = module.vpc.public_subnets
private_subnets = module.vpc.private_subnets
}
resource_tags = {}
container_image = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${var.region}.amazonaws.com/mammogram-preprocessing:latest"
container_name = "mammogram-preprocessing"
container_num_cores = 4
container_ram_gb = 30
ecs_cluster_name = "mammogram-cluster"
ecs_launch_type = "FARGATE"
permitted_s3_buckets = [var.bucket_name]
task_definition_name = "mammogram-preprocessing"
use_fargate = true
}
# Lambda function
resource "aws_lambda_function" "unique_job_name" {
filename = "scripts/unique_job_name.zip"
function_name = "unique_job_name"
role = aws_iam_role.lambda_role.arn
handler = "unique_job_name.lambda_handler"
runtime = "python3.7"
}
# Step Functions
resource "aws_sfn_state_machine" "state_machine" {
name = "mammogram-state-machine"
role_arn = aws_iam_role.step_functions_role.arn
depends_on = [null_resource.delay]
definition = <<EOF
{
"StartAt": "Conversion",
"States": {
"Conversion": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask.sync",
"Parameters": {
"LaunchType": "FARGATE",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"Subnets": ${jsonencode(module.vpc.public_subnets)},
"AssignPublicIp": "ENABLED"
}
},
"Overrides": {
"ContainerOverrides": [
{
"Name": "mammogram-conversion",
"Command": ["${var.bucket_name}", "CBIS-DDSM"]
}
]
},
"Cluster": "arn:aws:ecs:${var.region}:068255676137:cluster/mammogram-cluster",
"TaskDefinition": "arn:aws:ecs:${var.region}:068255676137:task-definition/mammogram-conversion"
},
"Next": "Preprocess"
},
"Preprocess": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask.sync",
"Parameters": {
"LaunchType": "FARGATE",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"Subnets": ${jsonencode(module.vpc.public_subnets)},
"AssignPublicIp": "ENABLED"
}
},
"Overrides": {
"ContainerOverrides": [
{
"Name": "mammogram-preprocessing",
"Command": ["${var.bucket_name}"]
}
]
},
"Cluster": "arn:aws:ecs:${var.region}:068255676137:cluster/mammogram-cluster",
"TaskDefinition": "arn:aws:ecs:${var.region}:068255676137:task-definition/mammogram-preprocessing"
},
"Next": "Generate Unique Job Name"
},
"Generate Unique Job Name": {
"Resource": "arn:aws:lambda:${var.region}:068255676137:function:unique_job_name",
"Type": "Task",
"Next": "Train Model"
},
"Train Model": {
"Resource": "arn:aws:states:::sagemaker:createTrainingJob.sync",
"Parameters": {
"AlgorithmSpecification": {
"TrainingImage": "763104351884.dkr.ecr.${var.region}.amazonaws.com/pytorch-training:1.3.1-gpu-py3",
"TrainingInputMode": "File",
"MetricDefinitions": [
{
"Name": "test:accuracy",
"Regex": "Accuracy: ([0-9\\.]+)"
}
]
},
"OutputDataConfig": {
"S3OutputPath": "s3://${var.bucket_name}/output"
},
"StoppingCondition": {
"MaxRuntimeInSeconds": 86400
},
"ResourceConfig": {
"InstanceCount": 2,
"InstanceType": "ml.p2.xlarge",
"VolumeSizeInGB": 30
},
"RoleArn": "${aws_iam_role.sagemaker_role.arn}",
"InputDataConfig": [
{
"DataSource": {
"S3DataSource": {
"S3DataType": "S3Prefix",
"S3Uri": "s3://${var.bucket_name}/PNG-Images-Processed/Train",
"S3DataDistributionType": "FullyReplicated"
}
},
"ChannelName": "training"
},
{
"DataSource": {
"S3DataSource": {
"S3DataType": "S3Prefix",
"S3Uri": "s3://${var.bucket_name}/PNG-Images-Processed/Test",
"S3DataDistributionType": "FullyReplicated"
}
},
"ChannelName": "testing"
}
],
"HyperParameters": {
"epochs": "6",
"batch-size": "16",
"test-batch-size": "16",
"lr": "0.01",
"backend": "gloo",
"sagemaker_program": "train.py",
"sagemaker_submit_directory": "s3://${var.bucket_name}/scripts/train.tar.gz"
},
"TrainingJobName.$": "$.JobName"
},
"Type": "Task",
"End": true
}
}
}
EOF
}