Documentation ¶
Overview ¶
Package events is used for unmarshaling AWS CloudWatch Events from JSON. At the moment it has only the very limited set of of event types needed by elasticsearch-asg, but it could be extended easily (and would probably lend itself very well to being generated from example JSON files).
For examples of events that come via CloudWatch Events, see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html
See also: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloudWatchEvents.html
Example ¶
package main import ( "encoding/json" "fmt" "github.com/mintel/elasticsearch-asg/v2/pkg/events" ) func main() { msg := `{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Spot Instance Interruption Warning", "source": "aws.ec2", "account": "123456789012", "time": "2019-09-26T12:55:24Z", "region": "us-east-2", "resources": ["arn:aws:ec2:us-east-2:123456789012:instance/i-1234567890abcdef0"], "detail": { "instance-id": "i-1234567890abcdef0", "instance-action": "terminate" } }` e := &events.CloudWatchEvent{} if err := json.Unmarshal([]byte(msg), e); err != nil { panic(err) } fmt.Println(e.Detail.(*events.EC2SpotInterruption).InstanceID) }
Output: i-1234567890abcdef0
Index ¶
- Variables
- func MustRegisterDetailType(source, detailType string, t reflect.Type)
- func RegisterDetailType(source, detailType string, t reflect.Type) error
- type AZSubnet
- type AutoScalingLifecycleTerminateAction
- type AutoScalingLifecycleTerminateSuccessful
- type AutoScalingLifecycleTerminateUnsuccessful
- type CloudWatchEvent
- type EC2SpotInterruption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidCloudWatchEvent is returned when unmarshaling a // CloudWatchEvent and the JSON did not contain an ID or // DetailType. ErrInvalidCloudWatchEvent = errors.New("invalid CloudWatch event") // ErrDetailTypeAlreadyRegistered is returned by RegisterDetailType() when a // detail type has already been registered. ErrDetailTypeAlreadyRegistered = errors.New("detail type already registered") )
Functions ¶
func MustRegisterDetailType ¶
RegisterDetailType can be used to register custom CloudWatch event types. It panics if source and detailType has already been registered.
func RegisterDetailType ¶
RegisterDetailType can be used to register custom CloudWatch event types. It returns ErrDetailTypeAlreadyRegistered if source and detailType has already been registered.
Example ¶
package main import ( "encoding/json" "fmt" "reflect" "github.com/mintel/elasticsearch-asg/v2/pkg/events" ) func main() { // MyDetailType is a custom CloudWatch Event type. type MyDetailType struct { Key1 string `json:"key1"` Key2 string `json:"key2"` } events.MustRegisterDetailType( "com.mycompany.myapp", "myDetailType", reflect.TypeOf(MyDetailType{}), ) data := []byte(`{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "myDetailType", "source": "com.mycompany.myapp", "account": "123456789012", "time": "2019-09-26T12:55:24Z", "region": "us-east-2", "resources": ["resource1", "resource2"], "detail": { "key1": "value1", "key2": "value2" } }`) v := &events.CloudWatchEvent{} if err := json.Unmarshal(data, v); err != nil { panic(err) } details := v.Detail.(*MyDetailType) fmt.Println(details.Key1) fmt.Println(details.Key2) }
Output: value1 value2
Types ¶
type AZSubnet ¶
type AZSubnet struct { // Example: "us-west-2b" AvailabilityZone string `json:"Availability Zone"` // Example: "subnet-12345678 SubnetID string `json:"Subnet ID"` }
AZSubnet is nested in the details of several AutoScaling events.
Example:
"Details": { "Availability Zone": "us-west-2b", "Subnet ID": "subnet-12345678" }
type AutoScalingLifecycleTerminateAction ¶
type AutoScalingLifecycleTerminateAction struct { // Example: "87654321-4321-4321-4321-210987654321" LifecycleActionToken string `json:"LifecycleActionToken"` // Example: "my-asg" AutoScalingGroupName string `json:"AutoScalingGroupName"` // Example: "my-lifecycle-hook" LifecycleHookName string `json:"LifecycleHookName"` // Example: "i-1234567890abcdef0" EC2InstanceID string `json:"EC2InstanceId"` // Example: "autoscaling:EC2_INSTANCE_TERMINATING" LifecycleTransition string `json:"LifecycleTransition"` // Example: "additional-info" NotificationMetadata string `json:"NotificationMetadata"` }
AutoScalingLifecycleTerminateAction is one possible value for the CloudWatchEvent.Detail field.
Amazon EC2 Auto Scaling moved an instance to a Terminating:Wait state due to a lifecycle hook.
Example:
{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Instance-terminate Lifecycle Action", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn" ], "detail": { "LifecycleActionToken":"87654321-4321-4321-4321-210987654321", "AutoScalingGroupName":"my-asg", "LifecycleHookName":"my-lifecycle-hook", "EC2InstanceId":"i-1234567890abcdef0", "LifecycleTransition":"autoscaling:EC2_INSTANCE_TERMINATING", "NotificationMetadata":"additional-info" } }
type AutoScalingLifecycleTerminateSuccessful ¶
type AutoScalingLifecycleTerminateSuccessful struct { // Example: "InProgress" StatusCode string `json:"StatusCode"` // Example: "Terminating EC2 instance: i-12345678" Description string `json:"Description"` // Example: "my-auto-scaling-group" AutoScalingGroupName string `json:"AutoScalingGroupName"` // Example: "87654321-4321-4321-4321-210987654321" ActivityID string `json:"ActivityId"` Details AZSubnet `json:"Details"` // Example: "12345678-1234-1234-1234-123456789012" RequestID string `json:"RequestId"` // Example: "" StatusMessage string `json:"StatusMessage"` // Example: "yyyy-mm-ddThh:mm:ssZ" EndTime time.Time `json:"EndTime"` // Example: "i-1234567890abcdef0" EC2InstanceID string `json:"EC2InstanceId"` // Example: "yyyy-mm-ddThh:mm:ssZ" StartTime time.Time `json:"StartTime"` // Example: "description-text" Cause string `json:"Cause"` }
AutoScalingLifecycleTerminateSuccessful is one possible value for the CloudWatchEvent.Detail field.
Amazon EC2 Auto Scaling successfully terminated an instance.
Example:
{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Instance Terminate Successful", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn", "instance-arn" ], "detail": { "StatusCode": "InProgress", "Description": "Terminating EC2 instance: i-12345678", "AutoScalingGroupName": "my-auto-scaling-group", "ActivityId": "87654321-4321-4321-4321-210987654321", "Details": { "Availability Zone": "us-west-2b", "Subnet ID": "subnet-12345678" }, "RequestId": "12345678-1234-1234-1234-123456789012", "StatusMessage": "", "EndTime": "yyyy-mm-ddThh:mm:ssZ", "EC2InstanceId": "i-1234567890abcdef0", "StartTime": "yyyy-mm-ddThh:mm:ssZ", "Cause": "description-text" } }
See also: https://docs.aws.amazon.com/autoscaling/ec2/userguide/cloud-watch-events.html#terminate-successful
type AutoScalingLifecycleTerminateUnsuccessful ¶
type AutoScalingLifecycleTerminateUnsuccessful struct { // Example: "Failed" StatusCode string `json:"StatusCode"` // Example: "my-auto-scaling-group" AutoScalingGroupName string `json:"AutoScalingGroupName"` // Example: "87654321-4321-4321-4321-210987654321" ActivityID string `json:"ActivityId"` Details AZSubnet `json:"Details"` // Example: "12345678-1234-1234-1234-123456789012" RequestID string `json:"RequestId"` // Example: "message-text" StatusMessage string `json:"StatusMessage"` // Example: "yyyy-mm-ddThh:mm:ssZ" EndTime time.Time `json:"EndTime"` // Example: "i-1234567890abcdef0" EC2InstanceID string `json:"EC2InstanceId"` // Example: "yyyy-mm-ddThh:mm:ssZ" StartTime time.Time `json:"StartTime"` // Example: "description-text" Cause string `json:"Cause"` }
AutoScalingLifecycleTerminateUnsuccessful is one possible value for the CloudWatchEvent.Detail field.
Amazon EC2 Auto Scaling failed to terminate an instance.
Example:
{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Instance Terminate Unsuccessful", "source": "aws.autoscaling", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-west-2", "resources": [ "auto-scaling-group-arn", "instance-arn" ], "detail": { "StatusCode": "Failed", "AutoScalingGroupName": "my-auto-scaling-group", "ActivityId": "87654321-4321-4321-4321-210987654321", "Details": { "Availability Zone": "us-west-2b", "Subnet ID": "subnet-12345678" }, "RequestId": "12345678-1234-1234-1234-123456789012", "StatusMessage": "message-text", "EndTime": "yyyy-mm-ddThh:mm:ssZ", "EC2InstanceId": "i-1234567890abcdef0", "StartTime": "yyyy-mm-ddThh:mm:ssZ", "Cause": "description-text" } }
See also: https://docs.aws.amazon.com/autoscaling/ec2/userguide/cloud-watch-events.html#terminate-unsuccessful
type CloudWatchEvent ¶
type CloudWatchEvent struct { // Example: "0", Version string `json:"version"` // Example: "12345678-1234-1234-1234-123456789012", ID string `json:"id"` // Example: "EC2 Spot Instance Interruption Warning", DetailType string `json:"detail-type"` // Example: "aws.ec2", Source string `json:"source"` // Example: "123456789012", AccountID string `json:"account"` // Example: "2019-09-26T12:55:24Z", Time time.Time `json:"time"` // Example: "us-east-2", Region string `json:"region"` // Example: ["arn:aws:ec2:us-east-2:123456789012:instance/i-1234567890abcdef0"], Resources []string `json:"resources"` // The exact type of Detail depends on the // Source and DetailType. If it is a known event type, // this will be a pointer to one of the detail type structs // in this package. If the event type is not known // this will default to a map[string]interface{}. Detail interface{} `json:"detail"` }
CloudWatchEvent is the outer structure of an event sent via CloudWatch Events. It is meant to be unmarshaled via encoding/json.
If the Source and DetailType fields are not defined, unmarshaling will return A ErrInvalidCloudWatchEvent.
func (*CloudWatchEvent) UnmarshalJSON ¶
func (e *CloudWatchEvent) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json Unmarshaler interface.
type EC2SpotInterruption ¶
type EC2SpotInterruption struct { // The ID of the EC2 spot instance that is about // to be interrupted. InstanceID string `json:"instance-id"` // One of: "hibernate", "stop", "terminate". InstanceAction string `json:"instance-action"` }
SpotInterruptionEventDetail is one possible value for the CloudWatchEvent.Detail field.
When Amazon EC2 is going to interrupt your Spot Instance, it emits an event two minutes prior to the actual interruption.
Example:
{ "version": "0", "id": "12345678-1234-1234-1234-123456789012", "detail-type": "EC2 Spot Instance Interruption Warning", "source": "aws.ec2", "account": "123456789012", "time": "yyyy-mm-ddThh:mm:ssZ", "region": "us-east-2", "resources": ["arn:aws:ec2:us-east-2:123456789012:instance/i-1234567890abcdef0"], "detail": { "instance-id": "i-1234567890abcdef0", "instance-action": "action" } }