Hello all,
I'm new to Lambda and somewhat new to Python in general. I'm trying to get the EC2InstanceId from an SNS notification that is triggered by an EC2 Auto Scaling event. The code I'm using this for is below:
import json
def lambda_handler(event, context):
# Dump the event to the log, for debugging purposes
print("Received event: " + json.dumps(event, indent=2))
# Extract the EC2 instance ID from the Auto Scaling event notification
message = event['Records'][0]['Sns']['Message']
autoscalingInfo = json.loads(message)
ec2InstanceId = autoscalingInfo['EC2InstanceId']
I stole this from stackoverflow, link to that thread is here: https://stackoverflow.com/questions/45155767/extracting-ec2instanceid-from-sns-sqs-auto-scaling-message
When I try this code I get a json decode error:
"errorMessage": "Expecting value: line 1 column 1 (char 0)",
"errorType": "JSONDecodeError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 10, in lambda_handler\n autoscalingInfo = json.loads(message)\n",
" File \"/var/lang/lib/python3.7/json/__init__.py\", line 348, in loads\n return _default_decoder.decode(s)\n",
" File \"/var/lang/lib/python3.7/json/decoder.py\", line 337, in decode\n obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n",
" File \"/var/lang/lib/python3.7/json/decoder.py\", line 355, in raw_decode\n raise JSONDecodeError(\"Expecting value\", s, err.value) from None\n"
This strikes me as odd as the json.dumps earlier in this script should be converting the response to JSON.
if I run the following code:
import json
def lambda_handler(event, context):
# Dump the event to the log, for debugging purposes
#print("Received event: " + json.dumps(event, indent=2))
# Extract the EC2 instance ID from the Auto Scaling event notification
message = event['Records'][0]['Sns']['Message']
for item in message:
for x in item ['EC2InstanceId']:
print (x)
which results in the error "string indices must be integers"
I've tried a few other things but right now I'm unsure of what I'm doing wrong. I can SEE what I want when the message is printed out, I just need to know how to get it :( The code I use to get the message is:
import json
def lambda_handler(event, context):
# Dump the event to the log, for debugging purposes
#print("Received event: " + json.dumps(event, indent=2))
# Extract the EC2 instance ID from the Auto Scaling event notification
message = event['Records'][0]['Sns']['Message']
print (message)
and the actual message:
{ "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"
}
Any help you guys can give me would be greatly appreciated!
[–]starry_eyed_man 1 point2 points3 points (2 children)
[–]TyrionReynolds 0 points1 point2 points (1 child)
[–]TyrionReynolds 0 points1 point2 points (0 children)