diff --git a/events/README_ECS_ContainerInstance.md b/events/README_ECS_ContainerInstance.md new file mode 100644 index 00000000..36d7e1b6 --- /dev/null +++ b/events/README_ECS_ContainerInstance.md @@ -0,0 +1,26 @@ +# Sample Function + +The following is a sample class and Lambda function that receives Amazon ECS Container instance state change events record data as an input and writes some of the record data to CloudWatch Logs. (Note that anything written to stdout or stderr will be logged as CloudWatch Logs events.) + +```go + +package main + +import ( + "context" + "fmt" + + "github.com/aws/aws-lambda-go/events" + "github.com/aws/aws-lambda-go/lambda" +) + +func handler(ctx context.Context, ecsEvent events.ECSContainerInstanceEvent) { + outputJSON, _ := json.MarshalIndent(ecsEvent, "", " ") + fmt.Printf("Data = %s", outputJSON) +} + +func main() { + lambda.Start(handler) +} + +``` diff --git a/events/ecs_container_instance.go b/events/ecs_container_instance.go new file mode 100644 index 00000000..4ef48b78 --- /dev/null +++ b/events/ecs_container_instance.go @@ -0,0 +1,70 @@ +package events + +import ( + "encoding/json" + "time" +) + +type ECSContainerInstanceEvent struct { + Version string `json:"version"` + ID string `json:"id"` + DetailType string `json:"detail-type"` + Source string `json:"source"` + Account string `json:"account"` + Time time.Time `json:"time"` + Region string `json:"region"` + Resources []string `json:"resources"` + Detail ECSContainerInstanceEventDetailType `json:"detail"` +} + +type ECSContainerInstanceEventDetailType struct { + AgentConnected bool `json:"agentConnected"` + Attributes []ECSContainerInstanceEventAttribute `json:"attributes"` + ClusterARN string `json:"clusterArn"` + ContainerInstanceARN string `json:"containerInstanceArn"` + EC2InstanceID string `json:"ec2InstanceId"` + RegisteredResources []ECSContainerInstanceEventResource `json:"registeredResources"` + RemainingResources []ECSContainerInstanceEventResource `json:"remainingResources"` + Status string `json:"status"` + Version int `json:"version"` + VersionInfo ECSContainerInstanceEventVersionInfo `json:"versionInfo"` + UpdatedAt time.Time `json:"updatedAt"` +} + +type ECSContainerInstanceEventAttribute struct { + Name string `json:"name"` +} + +type ECSContainerInstanceEventResource struct { + Name string `json:"name"` + Type string `json:"type"` + IntegerValue int `json:"integerValue,omitempty"` + StringSetValue []*string `json:"stringSetValue,omitempty"` +} + +type ECSContainerInstanceEventVersionInfo struct { + AgentHash string `json:"agentHash"` + AgentVersion string `json:"agentVersion"` + DockerVersion string `json:"dockerVersion"` +} + +// MarshalJSON implements custom marshaling to marshal the struct into JSON format while preserving an empty string slice in `StringSetValue` field. +func (r ECSContainerInstanceEventResource) MarshalJSON() ([]byte, error) { + type Alias ECSContainerInstanceEventResource + aux := struct { + StringSetValue json.RawMessage `json:"stringSetValue,omitempty"` + Alias + }{ + Alias: (Alias)(r), + } + + if r.StringSetValue != nil { + b, err := json.Marshal(r.StringSetValue) + if err != nil { + return nil, err + } + aux.StringSetValue = b + } + + return json.Marshal(&aux) +} diff --git a/events/ecs_container_instance_test.go b/events/ecs_container_instance_test.go new file mode 100644 index 00000000..1e7229ac --- /dev/null +++ b/events/ecs_container_instance_test.go @@ -0,0 +1,94 @@ +// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. +package events + +import ( + "encoding/json" + "testing" + "time" + + "github.com/aws/aws-lambda-go/events/test" + "github.com/stretchr/testify/assert" +) + +func TestECSContainerInstanceEventMarshaling(t *testing.T) { + // 1. read JSON from file + inputJSON := test.ReadJSONFromFile(t, "./testdata/ecs-container-instance-state-change.json") + + // 2. de-serialize into Go object + var inputEvent ECSContainerInstanceEvent + if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { + t.Errorf("could not unmarshal event. details: %v", err) + } + + // 3. Verify values populated into Go Object, at least one validation per data type + assert.Equal(t, "0", inputEvent.Version) + assert.Equal(t, "8952ba83-7be2-4ab5-9c32-6687532d15a2", inputEvent.ID) + assert.Equal(t, "ECS Container Instance State Change", inputEvent.DetailType) + assert.Equal(t, "aws.ecs", inputEvent.Source) + assert.Equal(t, "111122223333", inputEvent.Account) + assert.Equal(t, "us-east-1", inputEvent.Region) + assert.Equal(t, "arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315", inputEvent.Resources[0]) + testTime, err := time.Parse(time.RFC3339, "2016-12-06T16:41:06Z") + if err != nil { + t.Errorf("Failed to parse time: %v", err) + } + assert.Equal(t, testTime, inputEvent.Time) + + var detail = inputEvent.Detail + assert.True(t, detail.AgentConnected) + assert.Equal(t, "com.amazonaws.ecs.capability.logging-driver.syslog", detail.Attributes[0].Name) + assert.Equal(t, "arn:aws:ecs:us-east-1:111122223333:cluster/default", detail.ClusterARN) + assert.Equal(t, "arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315", detail.ContainerInstanceARN) + assert.Equal(t, "i-f3a8506b", detail.EC2InstanceID) + assert.Equal(t, "CPU", detail.RegisteredResources[0].Name) + assert.Equal(t, "INTEGER", detail.RegisteredResources[0].Type) + assert.Equal(t, 2048, detail.RegisteredResources[0].IntegerValue) + assert.Equal(t, "MEMORY", detail.RegisteredResources[1].Name) + assert.Equal(t, "INTEGER", detail.RegisteredResources[1].Type) + assert.Equal(t, 3767, detail.RegisteredResources[1].IntegerValue) + assert.Equal(t, "PORTS", detail.RegisteredResources[2].Name) + assert.Equal(t, "STRINGSET", detail.RegisteredResources[2].Type) + assert.Equal(t, []*string{ptr("22"), ptr("2376"), ptr("2375"), ptr("51678"), ptr("51679")}, detail.RegisteredResources[2].StringSetValue) + assert.Equal(t, "PORTS_UDP", detail.RegisteredResources[3].Name) + assert.Equal(t, "STRINGSET", detail.RegisteredResources[3].Type) + assert.Equal(t, []*string{}, detail.RegisteredResources[3].StringSetValue) + assert.Equal(t, "CPU", detail.RemainingResources[0].Name) + assert.Equal(t, "INTEGER", detail.RemainingResources[0].Type) + assert.Equal(t, 1988, detail.RemainingResources[0].IntegerValue) + assert.Equal(t, "MEMORY", detail.RemainingResources[1].Name) + assert.Equal(t, "INTEGER", detail.RemainingResources[1].Type) + assert.Equal(t, 767, detail.RemainingResources[1].IntegerValue) + assert.Equal(t, "PORTS", detail.RemainingResources[2].Name) + assert.Equal(t, "STRINGSET", detail.RemainingResources[2].Type) + assert.Equal(t, []*string{ptr("22"), ptr("2376"), ptr("2375"), ptr("51678"), ptr("51679")}, detail.RemainingResources[2].StringSetValue) + assert.Equal(t, "PORTS_UDP", detail.RemainingResources[3].Name) + assert.Equal(t, "STRINGSET", detail.RemainingResources[3].Type) + assert.Equal(t, []*string{}, detail.RemainingResources[3].StringSetValue) + assert.Equal(t, "ACTIVE", detail.Status) + assert.Equal(t, 14801, detail.Version) + assert.Equal(t, "aebcbca", detail.VersionInfo.AgentHash) + assert.Equal(t, "1.13.0", detail.VersionInfo.AgentVersion) + assert.Equal(t, "DockerVersion: 1.11.2", detail.VersionInfo.DockerVersion) + testUpdateTime, err := time.Parse(time.RFC3339, "2016-12-06T16:41:06Z") + if err != nil { + t.Errorf("Failed to parse time: %v", err) + } + assert.Equal(t, testUpdateTime, inputEvent.Time) + + // 4. serialize to JSON + outputJSON, err := json.Marshal(inputEvent) + if err != nil { + t.Errorf("could not marshal event. details: %v", err) + } + + // 5. check result + assert.JSONEq(t, string(inputJSON), string(outputJSON)) +} + +func ptr(s string) *string { + return &s +} + +func TestECSContainerInstanceMarshalingMalformedJson(t *testing.T) { + test.TestMalformedJson(t, ECSContainerInstanceEvent{}) +} diff --git a/events/testdata/ecs-container-instance-state-change.json b/events/testdata/ecs-container-instance-state-change.json new file mode 100644 index 00000000..1cc31b25 --- /dev/null +++ b/events/testdata/ecs-container-instance-state-change.json @@ -0,0 +1,102 @@ +{ + "version": "0", + "id": "8952ba83-7be2-4ab5-9c32-6687532d15a2", + "detail-type": "ECS Container Instance State Change", + "source": "aws.ecs", + "account": "111122223333", + "time": "2016-12-06T16:41:06Z", + "region": "us-east-1", + "resources": [ + "arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315" + ], + "detail": { + "agentConnected": true, + "attributes": [ + { + "name": "com.amazonaws.ecs.capability.logging-driver.syslog" + }, + { + "name": "com.amazonaws.ecs.capability.task-iam-role-network-host" + }, + { + "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" + }, + { + "name": "com.amazonaws.ecs.capability.logging-driver.json-file" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.17" + }, + { + "name": "com.amazonaws.ecs.capability.privileged-container" + } + ], + "clusterArn": "arn:aws:ecs:us-east-1:111122223333:cluster/default", + "containerInstanceArn": "arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315", + "ec2InstanceId": "i-f3a8506b", + "registeredResources": [ + { + "name": "CPU", + "type": "INTEGER", + "integerValue": 2048 + }, + { + "name": "MEMORY", + "type": "INTEGER", + "integerValue": 3767 + }, + { + "name": "PORTS", + "type": "STRINGSET", + "stringSetValue": [ + "22", + "2376", + "2375", + "51678", + "51679" + ] + }, + { + "name": "PORTS_UDP", + "type": "STRINGSET", + "stringSetValue": [] + } + ], + "remainingResources": [ + { + "name": "CPU", + "type": "INTEGER", + "integerValue": 1988 + }, + { + "name": "MEMORY", + "type": "INTEGER", + "integerValue": 767 + }, + { + "name": "PORTS", + "type": "STRINGSET", + "stringSetValue": [ + "22", + "2376", + "2375", + "51678", + "51679" + ] + }, + { + "name": "PORTS_UDP", + "type": "STRINGSET", + "stringSetValue": [] + } + ], + "status": "ACTIVE", + "version": 14801, + "versionInfo": { + "agentHash": "aebcbca", + "agentVersion": "1.13.0", + "dockerVersion": "DockerVersion: 1.11.2" + }, + "updatedAt": "2016-12-06T16:41:06.991Z" + } +}