我正在尝试使用cloudwatch_event并将其转换为go结构。我有一个CloudwatchEvent结构,里面是一个json的blob,我需要进入另一个结构。结构的第一级似乎工作正常,但在尝试访问嵌套的json时存在解析错误。
这是我的示例活动。我想要了解详细信息> EC2InstanceId我想我还需要状态代码。
{
"version": "0",
"id": "3e3c153a-8339-4e30-8c35-687ebef853fe",
"detail-type": "EC2 Instance Launch Successful",
"source": "aws.autoscaling",
"account": "123456789012",
"time": "2015-11-11T21:31:47Z",
"region": "us-east-1",
"resources": [
"arn:aws:autoscaling:us-east-1:123456789012:autoScalingGroup:eb56d16b-bbf0-401d-b893-d5978ed4a025:autoScalingGroupName/sampleLuanchSucASG",
"arn:aws:ec2:us-east-1:123456789012:instance/i-b188560f"
],
"detail": {
"StatusCode": "InProgress",
"AutoScalingGroupName": "sampleLuanchSucASG",
"ActivityId": "9cabb81f-42de-417d-8aa7-ce16bf026590",
"Details": {
"Availability Zone": "us-east-1b",
"Subnet ID": "subnet-95bfcebe"
},
"RequestId": "9cabb81f-42de-417d-8aa7-ce16bf026590",
"EndTime": "2015-11-11T21:31:47.208Z",
"EC2InstanceId": "i-b188560f",
"StartTime": "2015-11-11T21:31:13.671Z",
"Cause": "At 2015-11-11T21:31:10Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 1. At 2015-11-11T21:31:11Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 1."
}
}
由于aws-go-lambda库似乎没有处理这些事件,我创建了两个结构。
type CloudWatchEvent struct {
Version string `json:"version"`
ID string `json:"id"`
DetailType string `json:"detail-type"`
Source string `json:"source"`
AccountID string `json:"account"`
Time time.Time `json:"time"`
Region string `json:"region"`
Resources []string `json:"resources"`
Detail CloudWatchDetails `json:"detail"`
}
type CloudWatchDetails struct {
Detail struct {
StatusCode string `json:"StatusCode"`
AutoScalingGroupName string `json:"AutoScalingGroupName"`
ActivityID string `json:"ActivityId"`
Details struct {
AvailabilityZone string `json:"Availability Zone"`
SubnetID string `json:"Subnet ID"`
} `json:"Details"`
RequestID string `json:"RequestId"`
EndTime time.Time `json:"EndTime"`
EC2InstanceID string `json:"EC2InstanceId"`
StartTime time.Time `json:"StartTime"`
Cause time.Time `json:"Cause"`
} `json:"detail"`
}
我似乎能够解决event.Version或event.Id很好,但是当我尝试解决event.Detail.EC2InstanceId时,我得到的东西看起来像一个字节对象。
答案 0 :(得分:2)
你是双重嵌套的Detail属性。此外,JSON中的“Cause”属性是一个字符串而不是time.Time,您可能想要更改它。
这应该可以解决问题。
type CloudWatchEvent struct {
Version string `json:"version"`
ID string `json:"id"`
DetailType string `json:"detail-type"`
Source string `json:"source"`
AccountID string `json:"account"`
Time time.Time `json:"time"`
Region string `json:"region"`
Resources []string `json:"resources"`
Detail CloudWatchDetails `json:"detail"`
}
type CloudWatchDetails struct {
StatusCode string `json:"StatusCode"`
AutoScalingGroupName string `json:"AutoScalingGroupName"`
ActivityID string `json:"ActivityId"`
Details struct {
AvailabilityZone string `json:"Availability Zone"`
SubnetID string `json:"Subnet ID"`
} `json:"Details"`
RequestID string `json:"RequestId"`
EndTime time.Time `json:"EndTime"`
EC2InstanceID string `json:"EC2InstanceId"`
StartTime time.Time `json:"StartTime"`
Cause string `json:"Cause"`
}