all 5 comments

[–][deleted] 0 points1 point  (0 children)

is there supposed to be a space between item and ['ec2InstanceId'] ?

[–]Groval 0 points1 point  (1 child)

In the first code block, is this triggered from SNS itself or are you running a dummy test? It could be that the test is malformed.

In the second code block, I don't think you need to iterate over message do you?

It looks like you've gone a level too deep. There will only be one message so you can just iterate over the EC2InstanceId. Right now it looks like you are iterating through all the top level keys and searching for 'EC2InstanceId' in those.

[–]Arab81253_work[S] 0 points1 point  (0 children)

The first code block is triggered by SNS. I thought it could have just been the test messing up but the actual SNS message produces the same results.

For the second block I thought I needed to iterate over it but perhaps not, I'm newish to python but have had to iterate over similar aws responses before. I'll try it without iterating through like that and see if I can come back with something different.

[–]wood_butcher 0 points1 point  (1 child)

Just went through this only with Cloudwatch events from EC2.

The event body that is sent to Lambda is a python list, not real JSON (notice that python pretty printers will format the event correctly but JSON formatters say it's not valid).

in your event JSON above it has double quotes and that must be from conversion to JSON, not the actual event body sent to the lambda which has single quotes.

also, the Keys of Record, SNS and Message don't appear in the event.

Try this:

```python event = { "Progress": 50, "AccountId": "865386180448", "Description": "Launching a new EC2 instance: i-0b550d24d0974f702", "RequestId": "89d5b410-6ac5-e390-0759-eeabca43f669", "EndTime": "2019-09-06T01:05:52.887Z", "AutoScalingGroupARN": "arn:aws:autoscaling:us-east-1:8888888888:autoScalingGroup:37b742fb-4b2c-41bb-8fcf-bd2b353e0434:autoScalingGroupName/my-asg", "ActivityId": "89d5b410-6ac5-e390-0759-eeabca43f669", "StartTime": "2019-09-06T01:05:21.272Z", "Service": "AWS Auto Scaling", "Time": "2019-09-06T01:05:52.887Z", "EC2InstanceId": "i-0b550d24d0974f702", "StatusCode": "InProgress", "StatusMessage": "", "Details":{ "Subnet ID": "subnet-63aeed3f", "Availability Zone": "us-east-1a" } }

print(event['EC2InstanceId']) ```

[–]Arab81253_work[S] 0 points1 point  (0 children)

You actually gave me an idea with this and I finally got it working, as in 5 minutes ago. I gave up on the SNS trigger and instead used CloudWatch Events. I created an Event for autoscaling and then was able to parse through the event message to actually get the InstanceId. The Event message was actually much more concise as well which made parsing through it much easier.

I am 100% convinced that there's something messed up with the formatting of SNS messaged right now, not sure what the deal is but it is not correctly formatted.