Documentation ¶
Overview ¶
Package task provides simple functions to run task on ECS.
Usage:
import "github.com/h3poteto/ecs-task/pkg/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 }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ECSClient ¶ added in v0.2.10
type ECSClient interface { RunTask(ctx context.Context, params *ecs.RunTaskInput, optFns ...func(*ecs.Options)) (*ecs.RunTaskOutput, error) DescribeTasks(ctx context.Context, params *ecs.DescribeTasksInput, optFns ...func(*ecs.Options)) (*ecs.DescribeTasksOutput, error) StopTask(ctx context.Context, params *ecs.StopTaskInput, optFns ...func(*ecs.Options)) (*ecs.StopTaskOutput, error) }
type LogsClient ¶ added in v0.2.10
type LogsClient interface { DescribeLogStreams(ctx context.Context, params *cloudwatchlogs.DescribeLogStreamsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeLogStreamsOutput, error) GetLogEvents(ctx context.Context, params *cloudwatchlogs.GetLogEventsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.GetLogEventsOutput, error) }
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 ecstypes.LaunchType // 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 set Fargate as launch type, you have to set your Platform Version. PlatformVersion 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 ecstypes.AssignPublicIp // 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, platformVersion string, timeout time.Duration, timestampFormat, 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.
type TaskDefinition ¶
type TaskDefinition struct {
// contains filtered or unexported fields
}
TaskDefinition has client of aws-sdk-go.
func NewTaskDefinition ¶
func NewTaskDefinition(awsECS *ecs.Client) *TaskDefinition
NewTaskDefinition returns a new TaskDefinition struct, and initialize aws ecs API client.
func (*TaskDefinition) DescribeTaskDefinition ¶
func (d *TaskDefinition) DescribeTaskDefinition(ctx context.Context, taskDefinitionName string) (*ecstypes.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 *ecstypes.TaskDefinition, containerName string) (string, string, error)
GetLogGroup gets cloudwatch logs group and stream prefix.
type TaskDefinitionClient ¶ added in v0.2.10
type TaskDefinitionClient interface {
DescribeTaskDefinition(ctx context.Context, params *ecs.DescribeTaskDefinitionInput, optFns ...func(*ecs.Options)) (*ecs.DescribeTaskDefinitionOutput, error)
}
type Watcher ¶
Watcher has log group information and CloudWatchLogs Client.
func NewWatcher ¶
func NewWatcher(group, stream string, awsLogs *cloudwatchlogs.Client, timestampFormat string) *Watcher
NewWatcher returns a Watcher struct.
func (*Watcher) GetStreams ¶
GetStreams get cloudwatch logs streams according to log group name and stream prefix.