This is the json data I am receiving from aws sns notifications. I want to access deploymentGroupName
which is inside the Records->Sns->Message
In my lambda python code I am trying to do like this.
eventName = json.loads(event.Records[0].Sns.Message).deploymentGroupName;
This is the json I received.
{
'Records': [{
'EventSource': 'aws:sns',
'EventVersion': '1.0',
'EventSubscriptionArn': 'arn:aws:sns:us-east-1:1236542:project-Deploy-Success:123-654-12-b177-123654',
'Sns': {
'Type': 'Notification',
'MessageId': '6ef313fa-46d2-5841-b162-4805edfb421c',
'TopicArn': 'arn:aws:sns:us-east-1:428219256379:project-Deploy-Success',
'Subject': 'SUCCEEDED: AWS CodeDeploy d-E8BYQ65CL in us-east-1 to project-code-deploy',
'Message': '{"region":"us-east-1","accountId":"213321213321","eventTriggerName":"Sandbox-Deployment-Triggered","applicationName":"project-code-deploy","deploymentId":"d-E8BYQ65CL","deploymentGroupName":"Sandbox-ec2-deployment","createTime":"Tue Nov 29 06:38:20 UTC 2022","completeTime":"Tue Nov 29 06:38:33 UTC 2022","deploymentOverview":"{\\"Succeeded\\":1,\\"Failed\\":0,\\"Skipped\\":0,\\"InProgress\\":0,\\"Pending\\":0}","status":"SUCCEEDED"}',
'Timestamp': '2022-11-29T06:38:33.558Z',
}
}]
}
Right now giving this error.
[ERROR] NameError: name 'json' is not defined Traceback (most recent call last): File "/var/task/lambda_function.py", line 12, in lambda_handler eventName = json.loads(event.Records[0].Sns.Message).deploymentGroupName;
CodePudding user response:
If event
has no ""
surrounding, it has converted to dict
already. However, you need to deal with json
for Message
.
import json
msg = event['Records'][0]['Sns']['Message']
deploymentGroupName = json.loads(msg)['deploymentGroupName']
deploymentGroupName
output:
'Sandbox-ec2-deployment'