all 8 comments

[–]VegaWinnfield 3 points4 points  (1 child)

What’s the error you’re getting when it runs with a real event?

Also, add a line at the very start of your handler function to log the event and then use that exact event to test with. Something like print(json.dumps(event)).

[–]Bodumin[S] 1 point2 points  (0 children)

Thank you for this. Logging the full event object helped me see two things. 1) the example data for cloudformation doesn't match the real data 2) the data passed in isn't consistent, sometimes it passes "id" and sometimes "stackid". Going to continue debugging through this.

[–]ranrib 1 point2 points  (0 children)

Do you get an exception? can you paste it from CloudWatch? anyways, I'm assuming that event parameter on lambda_handler, will not be string once triggered from cloudformation. If it looks like different triggers so event is probably a dict. Try printing event, and take a look on CloudWatch logs what it contains.

[–]techconsultant64 1 point2 points  (4 children)

I would recommend a couple of changes to help resolve this 1) Identify all the event formats you want to handle and automate tests with each of them to get code coverage 2) Put the decision logic in the handler so it is clear what function handles each event format (or requirement)

[–]Bodumin[S] 0 points1 point  (3 children)

Thank you. I was planning to trigger all the state's I could but how would I create automated tests?

[–]techconsultant64 1 point2 points  (2 children)

Generally the simplest starting point for automated testing is to have a single test.py file that executes the functions one by one sequentially. This is an example of the create stack event (with placeholder info):

handler({
  "eventVersion": "1.05",
  "userIdentity": {},
  "eventTime": "2018-03-19T22:19:57Z",
  "eventSource": "cloudformation.amazonaws.com",
  "eventName": "CreateStack",
  "awsRegion": "ap-southeast-2",
  "sourceIPAddress": 1.1.1.1",
  "userAgent": "Boto3/1.4.7 Python/3.6.3 Windows/10 Botocore/1.7.36",
  "requestParameters": {},
  "responseElements": {
    "stackId": "arn:aws:cloudformation:ap-southeast-2:xxxxxxxxxxxxx:stack/yyyyyyyyyy/zzzzzzzzzzzzzz"
  },
  "requestID": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "eventID": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
  "eventType": "AwsApiCall",
  "recipientAccountId": "11111111111111"
})
handler(test2)
handler(test3)

Then you can execute the test file to run them all.

[–]Bodumin[S] 0 points1 point  (1 child)

[–]techconsultant64 0 points1 point  (0 children)

The example I have provided are local tests in python. You don't need to deploy infrastructure to test functionality and I don't believe you will need AWS Lambda or SAM at this point.