Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CloudWatchEvent not populating for scheduled rule. #112

Open
phcyso opened this issue Sep 4, 2018 · 3 comments
Open

CloudWatchEvent not populating for scheduled rule. #112

phcyso opened this issue Sep 4, 2018 · 3 comments

Comments

@phcyso
Copy link

phcyso commented Sep 4, 2018

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:

{
  "account": "0123456789",
  "region": "us-west-2",
  "detail": {
    "type": "rds"
  },
  "detail-type": "Scheduled Event",
  "source": "aws.events",
  "time": "1970-01-01T00:00:00Z",
  "id": "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c",
  "resources": [
    "arn:aws:events:us-west-2:0123456789:rule/RdsCheck"
  ]
}

Log output:

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
@ppai-plivo
Copy link
Contributor

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:

cw

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:

type Request {
    CountryISO string `json:"country_iso"`
}

func handleRequest(ctx context.Context, req Request) {
}

or

func handleRequest(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
}

@apyatkov
Copy link

It's 2021 and this issue is still unresolved!

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!

@apyatkov
Copy link

The problem was also described here : events example was requested and provided, but not the actual functionality...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants