v0.2.4 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2020 License: MIT Imports: 18 Imported by: 0



Package task provides simple functions to run task on ECS.


import "github.com/h3poteto/ecs-task/task"

Run a task

When you want to run a task on ECS, please use this package as follows.

At first, you have to get a task definition. The task definition is used to run a task.

For example:

t, err := task.NewTask("cluster-name", "container-name", "task-definition-arn or family", "commands", false, "", 300 * time.Second, "profile", "region")

// At first you have to get a task definition.
taskDef, err := t.taskDefinition.DescribeTaskDefinition(t.TaskDefinitionName)
if err != nil {
    return err

ctx, cancel := context.WithTimeout(context.Background(), t.Timeout)
defer cancel()

// Call run task API.
tasks, err := t.RunTask(ctx, taskDef)
if err != nil {
    return err

// And wait to completion of task execution.
err = t.WaitTask(ctx, tasks)

Polling CloudWatch Logs

You can polling CloudWatch Logs log stream.

For example:

// Get log group.
group, streamPrefix, err := t.taskDefinition.GetLogGroup(taskDef, "Container Name")
if err != nil {
    return err

w := NewWatcher(group, streamPrefix+"/" + "Container Name" + "Task ID", "AWS profile name", "ap-northeast-1")
err = w.Polling(ctx)
if err != nil {
    return err



This section is empty.


This section is empty.


This section is empty.


type Task

type Task struct {

	// ECS Cluster where you want to run the task.
	Cluster string
	// Container name which you want to run. Sometimes Task Definition has some container. So this package have to determine the container for run task.
	Container string
	// Name of Task Definition. You can provide full ARN, family or family:revision.
	TaskDefinitionName string

	// Command which you want to run.
	Command []*string
	// If you set 0, timeout is ignored.
	Timeout time.Duration
	// EC2 or Fargate
	LaunchType string
	// If you set Fargate as launch type, you have to set your subnet IDs.
	// Because Fargate demands awsvpc as network configuration, so subnet IDs are required.
	Subnets []*string
	// If you want to attach the security groups to ENI of the task, please set this.
	SecurityGroups []*string
	// If you don't enable this flag, the task access the internet throguth NAT gateway.
	// Please read more information: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-networking.html
	AssignPublicIP string
	// contains filtered or unexported fields

Task has target ECS information, client of aws-sdk-go, command and timeout seconds.

func NewTask

func NewTask(cluster, container, taskDefinitionName, command string, fargate bool, subnetIDs, securityGroupIDs string, timeout time.Duration, profile, region string) (*Task, error)

NewTask returns a new Task struct, and initialize aws ecs API client. If you want to run the task as Fargate, please provide fargate flag to true, and your subnet IDs for awsvpc. If you don't want to run the task as Fargate, please provide empty string for subnetIDs.

func (*Task) Run

func (t *Task) Run() error

Run a command on AWS ECS and output the log.

func (*Task) RunTask

func (t *Task) RunTask(ctx context.Context, taskDefinition *ecs.TaskDefinition) ([]*ecs.Task, error)

RunTask calls run-task API. This function does not wait to completion of the task.

func (*Task) WaitTask

func (t *Task) WaitTask(ctx context.Context, tasks []*ecs.Task) error

WaitTask waits completion of the task execition. If timeout occures, the function exits.

type TaskDefinition

type TaskDefinition struct {
	// contains filtered or unexported fields

TaskDefinition has client of aws-sdk-go.

func NewTaskDefinition

func NewTaskDefinition(profile, region string) *TaskDefinition

NewTaskDefinition returns a new TaskDefinition struct, and initialize aws ecs API client.

func (*TaskDefinition) DescribeTaskDefinition

func (d *TaskDefinition) DescribeTaskDefinition(taskDefinitionName string) (*ecs.TaskDefinition, error)

DescribeTaskDefinition gets a task definition. The family for the latest ACTIVE revision, family and revision (family:revision) for a specific revision in the family, or full Amazon Resource Name (ARN) of the task definition to describe.

func (*TaskDefinition) GetLogGroup

func (d *TaskDefinition) GetLogGroup(taskDef *ecs.TaskDefinition, containerName string) (string, string, error)

GetLogGroup gets cloudwatch logs group and stream prefix.

type Watcher

type Watcher struct {
	Group  string
	Stream string
	// contains filtered or unexported fields

Watcher has log group information and CloudWatchLogs Client.

func NewWatcher

func NewWatcher(group, stream, profile, region string) *Watcher

NewWatcher returns a Watcher struct.

func (*Watcher) GetStreams

func (w *Watcher) GetStreams() ([]*cloudwatchlogs.LogStream, error)

GetStreams get cloudwatch logs streams according to log group name and stream prefix.

func (*Watcher) Polling

func (w *Watcher) Polling(ctx context.Context) error

Polling get log stream and print the logs with streaming.

func (*Watcher) WaitStream

func (w *Watcher) WaitStream(ctx context.Context) (*cloudwatchlogs.LogStream, error)

WaitStream waits until the log stream is generated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL