You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Should the events.CloudWatchEvent type populate for scheduled cloudwatch events?
I have three cloudwatch events running on a schedule with different values set for the detail value.
The idea is to have one lambda function do three different tasks as they have a lot of shared code.
however the events.CloudWatchEvent struct is being passed to my handler empty.
Is there something else I need to do to get this to populate or is it just not set when using scheduled events?
Cloudwatch logs:
START RequestId: 9c1f6e4f-afef-11e8-a063-7369fa288f45 Version: $LATEST
2018/09/04 03:07:09 Starting check in , in account:
2018/09/04 03:07:09 Raw event Data: { 0001-01-01 00:00:00 +0000 UTC [] []}
2018/09/04 03:07:09 Raw event detail:
END RequestId: 9c1f6e4f-afef-11e8-a063-7369fa288f45
Code that gets called:
func main() {
lambda.Start(HandleRequest)
}
func HandleRequest(event events.CloudWatchEvent) {
var awsObjects types.AssuranceCheckList
var err error
checkStartTime := time.Now()
log.Printf("Starting check in %v, in account: %v", event.Region, event.AccountID)
log.Printf("Raw event Data: %v", event)
log.Printf("Raw event detail: %v", string(event.Detail))
var eventDetails types.EventDetail
jsonErr := json.Unmarshal(event.Detail, &eventDetails)
if jsonErr != nil {
panic(err) // This is where it is being killed
}
log.Printf("Type of event is: %v", eventDetails.Type)
...
}
Creating a test event works as expected:
Test event data:
START RequestId: f947eb4f-afed-11e8-802f-e5580a33f9b3 Version: $LATEST
2018/09/04 02:55:26 Starting Backup assurance check in us-west-2, in account: 0123456789
2018/09/04 02:55:26 Raw event Data: { cdc73f9d-aea9-11e3-9d5a-835b769c0d9c Scheduled Event aws.events 0123456789 1970-01-01 00:00:00 +0000 UTC us-west-2 [arn:aws:events:us-west-2:0123456789:rule/RdsCheck] [123 34 116 121 112 101 34 58 34 114 100 115 34 125]}
2018/09/04 02:55:26 Raw event detail: {"type":"rds"}
2018/09/04 02:55:26 Type of event is: rds
2018/09/04 02:55:26 About to do some RDS Related stuff!
2018/09/04 02:55:27 No Objects Found for alerting
END RequestId: f947eb4f-afed-11e8-802f-e5580a33f9b3
The text was updated successfully, but these errors were encountered:
Hope this helps others who come across the same issue...
In my case, I wanted CloudWatch Events to pass constant JSON to lambda as input as shown below:
Turns out, when Constant (JSON text) is being used as input, the events.CloudWatchEvent struct I get in lambda handler is empty. The Detail field of the struct also is empty.
Handling it is simple. Either of these can be used:
funchandleRequest(ctx context.Context, b json.RawMessage) {
// json.RawMessage is basically []byte i.e raw json // that can be unmarshalled// json.RawMessage can be used to check what's being passed
}
When CloudWatch scheduled event is triggering Lambda written in Golang events.CloudWatchEvent struct is empty.
As in @ppai-plivo example no data defined in "Constant(JSON text)" field can be received inside Lambda which is making CloudWatch -> Lambda combination unusable for certain scenarios.
@Tensho ,
The documentation you've added here is describing broken functionality!
Should the
events.CloudWatchEvent
type populate for scheduled cloudwatch events?I have three cloudwatch events running on a schedule with different values set for the detail value.
The idea is to have one lambda function do three different tasks as they have a lot of shared code.
however the
events.CloudWatchEvent
struct is being passed to my handler empty.Is there something else I need to do to get this to populate or is it just not set when using scheduled events?
Cloudwatch logs:
Code that gets called:
Creating a test event works as expected:
Test event data:
Log output:
The text was updated successfully, but these errors were encountered: