Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify service module to support external ECS execution and task roles #106

Closed
wants to merge 9 commits into from
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 = element(concat(aws_iam_role.ecs-execution.*.arn, data.aws_iam_role.ecs-execution-external.*.arn), var.external_ecs_execution_role == "" ? 0 : 1)
task_role_arn = element(concat(aws_iam_role.task-execution.*.arn, data.aws_iam_role.task-execution-external.*.arn), var.external_ecs_task_execution_role == "" ? 0 : 1)
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)
])
}
}
26 changes: 23 additions & 3 deletions .modules/service/policy.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ data "aws_iam_policy_document" "ecs-role-policy" {
}

resource "aws_iam_role" "ecs-execution" {
count = var.external_ecs_execution_role == "" ? 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" {
role = aws_iam_role.ecs-execution.id
count = var.external_ecs_execution_role == "" ? 1 : 0

role = element(concat(aws_iam_role.ecs-execution.*.id, tolist([""])), var.external_ecs_execution_role == "" ? 0 : 1)
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}

Expand Down Expand Up @@ -46,11 +50,27 @@ data "aws_iam_policy_document" "task-assume-role" {
}

resource "aws_iam_role" "task-execution" {
count = var.external_ecs_task_execution_role == "" ? 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.external_ecs_task_execution_role == "" ? 1 : 0

policy = data.aws_iam_policy_document.task-policy.json
role = aws_iam_role.task-execution.id
}
role = element(concat(aws_iam_role.task-execution.*.id, tolist([""])), var.external_ecs_task_execution_role == "" ? 0 : 1)
}

data "aws_iam_role" "ecs-execution-external" {
count = var.external_ecs_execution_role == "" ? 0 : 1

name = var.external_ecs_execution_role
}

data "aws_iam_role" "task-execution-external" {
count = var.external_ecs_task_execution_role == "" ? 0 : 1

name = var.external_ecs_task_execution_role
}
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 "external_ecs_execution_role" {
description = "The name of an external ECS execution role to use"
type = string
default = ""
}

variable "external_ecs_task_execution_role" {
description = "The name of an external ECS task execution role to use"
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 = aws_iam_role.ecs-execution.name
ecs_task_execution_role = aws_iam_role.task-execution.name
}

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 = aws_iam_role.ecs-execution.name
ecs_task_execution_role = aws_iam_role.task-execution.name
}

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 = aws_iam_role.ecs-execution.name
ecs_task_execution_role = aws_iam_role.task-execution.name
}
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
external_ecs_execution_role = var.ecs_execution_role
external_ecs_task_execution_role = var.ecs_task_execution_role
}
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" {
description = "The name of the ECS execution role"
type = string
}

variable "ecs_task_execution_role" {
description = "The name of the ECS task execution role"
type = string
}