-
Notifications
You must be signed in to change notification settings - Fork 0
/
fargate.tf
113 lines (100 loc) · 2.82 KB
/
fargate.tf
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
provider "aws" {
region = var.aws_region # fill in the AWS region
}
# create an ECS task execution IAM role
resource "aws_iam_role" "ecs_task_execution_role" {
name = "ecs-task-execution-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_policy" {
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
# create an ECS cluster
resource "aws_ecs_cluster" "my_cluster" {
name = "my-cluster"
}
resource "aws_cloudwatch_log_group" "ecs_log_group" {
name = "/ecs/my-log-group"
retention_in_days = 14
}
# create an ECS task definition
resource "aws_ecs_task_definition" "ecs_task" {
family = "my-task-family"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
container_definitions = jsonencode([{
name = "tfserving_token_price_prediction"
image = var.container_image # fill in your container image name and tag in ECR
portMappings = [
{
containerPort = 8500
hostPort = 8500
protocol = "tcp" # gRPC
},
{
containerPort = 8501
hostPort = 8501
protocol = "tcp" # HTTP
}
]
environment = [{
name = "MODEL_NAME"
value = "TokenPricePredictionModel"
}]
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = aws_cloudwatch_log_group.ecs_log_group.name
awslogs-region = var.aws_region # fill in the AWS region
awslogs-stream-prefix = "ecs"
}
}
}])
}
# create an ECS service
resource "aws_security_group" "ecs_tasks_sg" {
name = "ecs_tasks_sg"
description = "Allow all inbound traffic on the container ports and all outbound traffic"
ingress {
from_port = 8500
to_port = 8501
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_ecs_service" "my_service" {
name = "my-service"
cluster = aws_ecs_cluster.my_cluster.id
task_definition = aws_ecs_task_definition.ecs_task.arn
launch_type = "FARGATE"
network_configuration {
subnets = var.subnets # fill in your subnets' IDs as a list
assign_public_ip = true
security_groups = [aws_security_group.ecs_tasks_sg.id]
}
desired_count = 1
}