Newer
Older

Nuwan Rajika Kumarasiri
committed
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
data "template_file" "container" {
template = file("./templates/container.tpl")
vars = {
container_name = var.container_name
image_name = var.image_name
fargate_cpu = var.fargate_cpu
fargate_memory = var.fargate_memory
app_port1 = var.container_app_port[0]
app_port2 = var.container_app_port[1]
app_port3 = var.container_app_port[2]
network_mode = var.container_network_mode
}
}
data "aws_iam_role" "ecs-task-execution" {
name = "ecsTaskExecutionRole"
}
resource "aws_ecs_task_definition" "task" {
family = var.ecs_task_name
execution_role_arn = data.aws_iam_role.ecs-task-execution.arn
network_mode = var.container_network_mode
requires_compatibilities = ["FARGATE"]
cpu = var.fargate_cpu
memory = var.fargate_memory
container_definitions = data.template_file.container.rendered
}
resource "aws_ecs_service" "service" {
name = var.ecs_service_name
cluster = aws_ecs_cluster.cluster.id
task_definition = aws_ecs_task_definition.task.arn
desired_count = 1
launch_type = "FARGATE"
network_configuration {
assign_public_ip = false
subnets = [data.aws_subnet_ids.subnet-a.id,data.aws_subnet_ids.subnet-b.id]
security_groups = [data.aws_security_group.sec-group.id]
}
}
resource "aws_ecs_cluster" "cluster" {
name = var.ecs_cluster_name
tags = var.ecs_cluster_tags
}