Skip to content

Instantly share code, notes, and snippets.

@1davidmichael
Forked from jeffbrl/describe_instances.py
Created May 20, 2019 18:40
Show Gist options
  • Save 1davidmichael/3d2a6ba1b4f7f8f99bfbdfd28d482382 to your computer and use it in GitHub Desktop.
Save 1davidmichael/3d2a6ba1b4f7f8f99bfbdfd28d482382 to your computer and use it in GitHub Desktop.
How to make datetime.datetime json serializable - boto3 ec2 describe_instances
# Adapted from https://stackoverflow.com/questions/35869985/datetime-datetime-is-not-json-serializable
import datetime
import json
import boto3
def datetime_handler(x):
if isinstance(x, datetime.datetime):
return x.isoformat()
raise TypeError("Unknown type")
client = boto3.client('ec2')
data = client.describe_instances()
print(json.dumps(data, default=datetime_handler))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment