Skip to content

Commit

Permalink
Modify service module to support external ECS execution and task roles
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Sep 27, 2024
1 parent 1b9d95a commit 6697ce0
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .modules/service/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ resource "aws_ecs_task_definition" "task" {
family = var.service_name
cpu = var.ecs_cpu
memory = var.ecs_memory
execution_role_arn = aws_iam_role.ecs-execution.arn
task_role_arn = aws_iam_role.task-execution.arn
execution_role_arn = coalesce(var.ecs_execution_role_arn, aws_iam_role.ecs-execution.arn)
task_role_arn = coalesce(var.ecs_task_execution_role_arn, aws_iam_role.task-execution.arn)
network_mode = "awsvpc"
requires_compatibilities = [var.launch_type]

Expand Down Expand Up @@ -56,4 +56,4 @@ resource "aws_ecs_task_definition" "task" {
}
}, var.container_definitions)
])
}
}
10 changes: 9 additions & 1 deletion .modules/service/policy.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ data "aws_iam_policy_document" "ecs-role-policy" {
}

resource "aws_iam_role" "ecs-execution" {
count = var.ecs_execution_role_arn == "" ? 1 : 0

name = "${var.service_name}-ExecutionRole-role"
assume_role_policy = data.aws_iam_policy_document.ecs-role-policy.json
}

resource "aws_iam_role_policy_attachment" "ecs-execution-managed" {
count = var.ecs_execution_role_arn == "" ? 1 : 0

role = aws_iam_role.ecs-execution.id
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
Expand Down Expand Up @@ -46,11 +50,15 @@ data "aws_iam_policy_document" "task-assume-role" {
}

resource "aws_iam_role" "task-execution" {
count = var.ecs_task_execution_role_arn == "" ? 1 : 0

name = "${var.service_name}-TaskRole-role"
assume_role_policy = data.aws_iam_policy_document.task-assume-role.json
}

resource "aws_iam_role_policy" "task-role" {
count = var.ecs_task_execution_role_arn == "" ? 1 : 0

policy = data.aws_iam_policy_document.task-policy.json
role = aws_iam_role.task-execution.id
}
}
12 changes: 12 additions & 0 deletions .modules/service/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ variable "rolling_updates" {
default = false
type = bool
}

variable "ecs_execution_role_arn" {
description = "The ARN of the ECS execution role"
type = string
default = ""
}

variable "ecs_task_execution_role_arn" {
description = "The ARN of the ECS task role"
type = string
default = ""
}
24 changes: 15 additions & 9 deletions stun_server/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,29 @@ provider "aws" {
module "us_east_1" {
source = "./region"

region = "us-east-1"
domain_name = var.domain_name
image_tag = var.image_tag
region = "us-east-1"
domain_name = var.domain_name
image_tag = var.image_tag
ecs_execution_role_arn = aws_iam_role.ecs-execution.arn
ecs_task_execution_role_arn = aws_iam_role.task-execution.arn
}

module "eu_central_1" {
source = "./region"

region = "eu-central-1"
domain_name = var.domain_name
image_tag = var.image_tag
region = "eu-central-1"
domain_name = var.domain_name
image_tag = var.image_tag
ecs_execution_role_arn = aws_iam_role.ecs-execution.arn
ecs_task_execution_role_arn = aws_iam_role.task-execution.arn
}

module "ap_southeast_1" {
source = "./region"

region = "ap-southeast-1"
domain_name = var.domain_name
image_tag = var.image_tag
region = "ap-southeast-1"
domain_name = var.domain_name
image_tag = var.image_tag
ecs_execution_role_arn = aws_iam_role.ecs-execution.arn
ecs_task_execution_role_arn = aws_iam_role.task-execution.arn
}
52 changes: 52 additions & 0 deletions stun_server/policy.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
data "aws_iam_policy_document" "ecs-role-policy" {
statement {
actions = ["sts:AssumeRole"]

principals {
identifiers = ["ecs-tasks.amazonaws.com"]
type = "Service"
}
}
}

resource "aws_iam_role" "ecs-execution" {

name = "stun-server-ExecutionRole-role"
assume_role_policy = data.aws_iam_policy_document.ecs-role-policy.json
}

resource "aws_iam_role_policy_attachment" "ecs-execution-managed" {

role = aws_iam_role.ecs-execution.id
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}

data "aws_iam_policy_document" "task-policy" {
statement {
actions = ["cloudwatch:putMetricData"]
resources = ["*"]
}
}

data "aws_iam_policy_document" "task-assume-role" {
statement {
actions = ["sts:AssumeRole"]

principals {
identifiers = ["ecs-tasks.amazonaws.com"]
type = "Service"
}
}
}

resource "aws_iam_role" "task-execution" {

name = "stun-server-TaskRole-role"
assume_role_policy = data.aws_iam_policy_document.task-assume-role.json
}

resource "aws_iam_role_policy" "task-role" {

policy = data.aws_iam_policy_document.task-policy.json
role = aws_iam_role.task-execution.id
}
1 change: 0 additions & 1 deletion stun_server/region/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ resource "aws_ecs_service" "stun-server" {
desired_count = 1
deployment_minimum_healthy_percent = 100
deployment_maximum_percent = 200
health_check_grace_period_seconds = 90
launch_type = "FARGATE"

# Required to fetch the public IP address of the ECS service
Expand Down
4 changes: 3 additions & 1 deletion stun_server/region/module.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ module "stun_server" {
}
],
}
webservice = true
webservice = true
ecs_execution_role_arn = var.ecs_execution_role_arn
ecs_task_execution_role_arn = var.ecs_task_execution_role_arn
}
10 changes: 10 additions & 0 deletions stun_server/region/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ variable "image_tag" {
description = "Version of the Stun server to deploy"
type = string
}

variable "ecs_execution_role_arn" {
description = "The ARN of the ECS execution role"
type = string
}

variable "ecs_task_execution_role_arn" {
description = "The ARN of the ECS task execution role"
type = string
}

0 comments on commit 6697ce0

Please sign in to comment.