Skip to content

Commit 94e318c

Browse files
committed
feat(ecs-background): add IAM policy for running ECS tasks
- Introduced a new IAM policy module to allow external execution of ECS task definitions. - Added output for the ARN of the created run policy. - Defined a new variable to control the creation of the run task role.
1 parent d9e47e9 commit 94e318c

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

modules/ecs-background/main.tf

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ module "ecs_task" {
140140
context = module.this.context
141141

142142
ecs_service_enabled = var.ecs_service_enabled
143-
container_definition_json = "[${module.container.json_map_encoded}]"
143+
container_definition_json = "[${module.container.json_map_encoded}]"
144144
ecs_cluster_arn = var.ecs_cluster_arn
145145
capacity_provider_strategies = var.ecs_capacity_provider_strategies
146146
launch_type = var.ecs_launch_type
@@ -184,6 +184,58 @@ module "ecs_task" {
184184
task_cpu = var.ecs_task_cpu
185185
}
186186

187+
//////////
188+
// Optional policy to allow external running of this task definition
189+
// Intended to be attached to a role created outside this module
190+
//////////
191+
192+
// IAM policy to allow running the task via RunTask on the specified cluster
193+
module "ecs_task_run_policy" {
194+
source = "cloudposse/iam-policy/aws"
195+
version = "2.0.2"
196+
197+
enabled = module.this.enabled && var.create_run_task_role
198+
199+
attributes = ["task", "run", "policy"]
200+
201+
# Create the policy
202+
iam_policy_enabled = true
203+
204+
iam_policy = [{
205+
version = "2012-10-17"
206+
policy_id = "ecs-task-run-policy"
207+
statements = [
208+
{
209+
sid = "PassRole"
210+
effect = "Allow"
211+
actions = ["iam:PassRole"]
212+
resources = [
213+
module.ecs_task.task_exec_role_arn,
214+
module.ecs_task.task_role_arn
215+
]
216+
},
217+
{
218+
sid = "RunTask"
219+
effect = "Allow"
220+
actions = ["ecs:RunTask"]
221+
resources = ["${module.ecs_task.task_definition_arn_without_revision}:*"]
222+
conditions = [{
223+
test = "ArnEquals"
224+
variable = "ecs:cluster"
225+
values = [var.ecs_cluster_arn]
226+
}]
227+
}
228+
]
229+
}]
230+
231+
context = module.this.context
232+
}
233+
234+
235+
//////////
236+
// ECS Task Role Policies
237+
//////////
238+
187239
// Allow ECS task to access SSM parameters
188240
resource "aws_iam_role_policy_attachment" "ecs_task" {
189241
count = module.this.enabled ? 1 : 0

modules/ecs-background/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ output "ecs_task_definition_family" {
1313
value = module.ecs_task.task_definition_family
1414
}
1515

16+
output "ecs_task_run_policy_arn" {
17+
description = "ARN of the policy to allow running ECS tasks"
18+
value = one(module.ecs_task_run_policy[*]["policy_arn"])
19+
}
20+
1621
output "log_groups" {
1722
value = local.log_groups
1823
description = "ECS log groups"

modules/ecs-background/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ variable "ecs_service_enabled" {
4545
default = true
4646
}
4747

48+
variable "create_run_task_role" {
49+
type = bool
50+
description = "Whether to create an IAM policy to allow running the task via RunTask"
51+
default = false
52+
}
4853

4954
variable "ecs_security_group_ids" {
5055
type = list(string)

0 commit comments

Comments
 (0)