简体   繁体   中英

Format JSON output from AWS Lambda function

Lambda Program

import json
import boto3
from pprint import pprint
def lambda_handler(event, context):
    # TODO implement
    #instance = event['instanceid']
    client = boto3.client("ec2")
    status = client.describe_instance_status(InstanceIds=[
        'i-0c52lidc87f',
    ],)
    #pprint(status)
    for i in status["InstanceStatuses"]:
        print("AvailabilityZone :", i["AvailabilityZone"])
        print("InstanceId :", i["InstanceId"])
        print("InstanceState :", i["InstanceState"])
        print("InstanceStatus", i["InstanceStatus"])   
    return {
        'body': ("Instance Status :", i["InstanceState"],i["InstanceId"],i["AvailabilityZone"])
    }

Output

{
  "statusCode": 200,
  "body": [
    "Instance Status :",
    {
      "Code": 16,
      "Name": "running"
    },
    "i-0c52lidc87f",
    "ca-central-1a"
  ]
}

I'm getting the above response from my lambda function on AWS - how can change this to readable format - just Instance ID: i-0c5e8c3c87f and Status: Running

Please help!

Youre JSON isnt formatted properly, if Instance Status is supposed to be a tuple, try:

return {
  "body": {
    "Instance Status":{
"InstanceState": i["InstanceState"]["Name"],
"InstanceId": i["InstanceId"],
"AvailabilityZone": i["AvailabilityZone"]
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM