Skip to content

Commit ba02b02

Browse files
authored
chore: add flag for task run execution role and remove default task role from cfn template (#1196)
This PR contains the following changes: 1. allowing user to specify a specific `execution-role` for `task run` 2. removing `DefaultTaskRole` from task's cfn template since it doesn't require a task role in order to run a task <!-- Issue number, if available. E.g. "Fixes #31", "Addresses #42, 77" --> By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 2e43aba commit ba02b02

6 files changed

Lines changed: 25 additions & 15 deletions

File tree

internal/pkg/cli/flag.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const (
5959
memoryFlag = "memory"
6060
imageFlag = "image"
6161
taskRoleFlag = "task-role"
62+
executionRoleFlag = "execution-role"
6263
subnetsFlag = "subnets"
6364
securityGroupsFlag = "security-groups"
6465
envVarsFlag = "env-vars"
@@ -142,6 +143,7 @@ Must be of the format '<keyName>:<dataType>'.`
142143
memoryFlagDescription = "Optional. The amount of memory to reserve in MiB for each task."
143144
imageFlagDescription = "Optional. The image to run instead of building a Dockerfile."
144145
taskRoleFlagDescription = "Optional. The role for the task to use."
146+
executionRoleFlagDescription = "Optional. The role that grants the container agent permission to make AWS API calls."
145147
subnetsFlagDescription = "Optional. The subnet IDs for the task to use. Can be specified multiple times."
146148
securityGroupsFlagDescription = "Optional. The security group IDs for the task to use. Can be specified multiple times."
147149
envVarsFlagDescription = "Optional. Environment variables specified by key=value separated with commas."

internal/pkg/cli/task_run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type runTaskVars struct {
6868
imageTag string
6969

7070
taskRole string
71+
executionRole string
7172

7273
subnets []string
7374
securityGroups []string
@@ -355,6 +356,7 @@ func (o *runTaskOpts) deploy() error {
355356
Memory: o.memory,
356357
Image: o.image,
357358
TaskRole: o.taskRole,
359+
ExecutionRole: o.executionRole,
358360
Command: o.command,
359361
EnvVars: o.envVars,
360362
})
@@ -483,6 +485,7 @@ Run a task with a command.
483485
cmd.Flags().StringVar(&vars.imageTag, imageTagFlag, "", taskImageTagFlagDescription)
484486

485487
cmd.Flags().StringVar(&vars.taskRole, taskRoleFlag, "", taskRoleFlagDescription)
488+
cmd.Flags().StringVar(&vars.executionRole, executionRoleFlag, "", executionRoleFlagDescription)
486489

487490
cmd.Flags().StringVar(&vars.appName, appFlag, "", appFlagDescription)
488491
cmd.Flags().StringVar(&vars.env, envFlag, "", envFlagDescription)

internal/pkg/deploy/cloudformation/stack/task.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424

2525
taskContainerImageParamKey = "ContainerImage"
2626
taskTaskRoleParamKey = "TaskRole"
27+
taskExecutionRoleParamKey = "ExecutionRole"
2728
taskCommandParamKey = "Command"
2829

2930
taskLogRetentionInDays = "1"
@@ -88,6 +89,10 @@ func (t *taskStackConfig) Parameters() ([]*cloudformation.Parameter, error) {
8889
ParameterKey: aws.String(taskTaskRoleParamKey),
8990
ParameterValue: aws.String(t.TaskRole),
9091
},
92+
{
93+
ParameterKey: aws.String(taskExecutionRoleParamKey),
94+
ParameterValue: aws.String(t.ExecutionRole),
95+
},
9196
{
9297
ParameterKey: aws.String(taskCommandParamKey),
9398
ParameterValue: aws.String(t.Command),

internal/pkg/deploy/cloudformation/stack/task_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ func TestTaskStackConfig_Parameters(t *testing.T) {
9696
ParameterKey: aws.String(taskTaskRoleParamKey),
9797
ParameterValue: aws.String("task-role"),
9898
},
99+
{
100+
ParameterKey: aws.String(taskExecutionRoleParamKey),
101+
ParameterValue: aws.String("execution-role"),
102+
},
99103
{
100104
ParameterKey: aws.String(taskCommandParamKey),
101105
ParameterValue: aws.String("echo hooray"),
@@ -109,6 +113,7 @@ func TestTaskStackConfig_Parameters(t *testing.T) {
109113

110114
Image: "7456.dkr.ecr.us-east-2.amazonaws.com/my-task:0.1",
111115
TaskRole: "task-role",
116+
ExecutionRole: "execution-role",
112117
Command: "echo hooray",
113118
}
114119

internal/pkg/deploy/task.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type CreateTaskResourcesInput struct {
1313

1414
Image string
1515
TaskRole string
16+
ExecutionRole string
1617
Command string
1718
EnvVars map[string]string
1819

templates/task/cf.yml

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ Parameters:
1515
Type: String
1616
TaskRole:
1717
Type: String
18+
ExecutionRole:
19+
Type: String
1820
Command:
1921
Type: String
2022
Conditions:
2123
# NOTE: Image cannot be pushed until the ECR repo is created, at which time ContainerImage would be "".
2224
HasImage:
2325
!Not [!Equals [!Ref ContainerImage, ""]]
24-
UseDefaultTaskRole:
25-
!Equals [!Ref TaskRole, ""]
26+
HasTaskRole:
27+
!Not [!Equals [!Ref TaskRole, ""]]
28+
HasExecutionRole:
29+
!Not [!Equals [!Ref ExecutionRole, ""]]
2630
HasCommand:
2731
!Not [!Equals [!Ref Command, ""]]
2832
Resources:
@@ -51,20 +55,10 @@ Resources:
5155
NetworkMode: awsvpc
5256
Cpu: !Ref TaskCPU
5357
Memory: !Ref TaskMemory
54-
ExecutionRoleArn: !Ref ExecutionRole
58+
ExecutionRoleArn: !If [HasExecutionRole, !Ref ExecutionRole, !Ref DefaultExecutionRole]
5559
TaskRoleArn:
56-
!If [UseDefaultTaskRole, !Ref DefaultTaskRole, !Ref TaskRole]
57-
DefaultTaskRole:
58-
Condition: UseDefaultTaskRole
59-
Type: AWS::IAM::Role
60-
Properties:
61-
AssumeRolePolicyDocument:
62-
Statement:
63-
- Effect: Allow
64-
Principal:
65-
Service: ecs-tasks.amazonaws.com
66-
Action: 'sts:AssumeRole'
67-
ExecutionRole:
60+
!If [HasTaskRole, !Ref TaskRole, !Ref "AWS::NoValue"]
61+
DefaultExecutionRole:
6862
Type: AWS::IAM::Role
6963
Properties:
7064
AssumeRolePolicyDocument:

0 commit comments

Comments
 (0)