Skip to content

Commit

Permalink
ecs-run-task: Support task overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
EdoFoco authored and hamstah committed Jan 25, 2024
1 parent c03bfe9 commit 8321af7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions ecs/run-task/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Flags:
--task-definition=TASK-DEFINITION
ECS task definition
--cluster=CLUSTER ECS cluster
--task-overrides-json=TASK-OVERRIDES-JSON
Path to a JSON file with the task overrides to use
--assume-role-arn=ASSUME-ROLE-ARN
Role to assume
--assume-role-external-id=ASSUME-ROLE-EXTERNAL-ID
Expand Down
33 changes: 30 additions & 3 deletions ecs/run-task/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package main

import (
"encoding/json"
"os"

kingpin "github.com/alecthomas/kingpin/v2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/hamstah/awstools/common"
)

var (
taskDefinition = kingpin.Flag("task-definition", "ECS task definition").Required().String()
cluster = kingpin.Flag("cluster", "ECS cluster").Required().String()
taskDefinition = kingpin.Flag("task-definition", "ECS task definition").Required().String()
cluster = kingpin.Flag("cluster", "ECS cluster").Required().String()
taskOverridesJSON = kingpin.Flag("task-overrides-json", "Path to a JSON file with the task overrides to use").String()
)

func main() {
Expand All @@ -21,10 +25,33 @@ func main() {

ecsClient := ecs.New(session, conf)

_, err := ecsClient.RunTask(&ecs.RunTaskInput{
taskOverrides, err := resolveTaskOverrides(*taskOverridesJSON)
common.FatalOnError(err)

_, err = ecsClient.RunTask(&ecs.RunTaskInput{
TaskDefinition: taskDefinition,
Cluster: cluster,
Count: aws.Int64(1),
Overrides: taskOverrides,
})

common.FatalOnError(err)
}

func resolveTaskOverrides(taskOverridesJSON string) (*ecs.TaskOverride, error) {
if taskOverridesJSON == "" {
return nil, nil
}

b, err := os.ReadFile(taskOverridesJSON)
if err != nil {
return nil, err
}

taskOverrides := &ecs.TaskOverride{}
if err := json.Unmarshal(b, taskOverrides); err != nil {
return nil, err
}

return taskOverrides, nil
}

0 comments on commit 8321af7

Please sign in to comment.