Skip to content

Instantly share code, notes, and snippets.

@jacurtis
Forked from jeffbrl/describe_instances.py
Last active December 1, 2021 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacurtis/a14725df1505524b125c88a137916178 to your computer and use it in GitHub Desktop.
Save jacurtis/a14725df1505524b125c88a137916178 to your computer and use it in GitHub Desktop.
How to make boto3 responses with datetime.datetime json serializable - boto3 ec2 describe_instances or rds describe_db_instances
# The file that does the voodoo magic
# Inspired by:
# - https://stackoverflow.com/questions/35869985/datetime-datetime-is-not-json-serializable
# - https://gist.github.com/jeffbrl/67eed588f2d32afcaf3bf779bd91f7a7
import json
import datetime
def pretty_print(data):
print(json.dumps(data, default=__datetime_handler, indent=4))
def __datetime_handler(x):
if isinstance(x, datetime.datetime):
return x.isoformat()
raise TypeError("Unknown Type")
import boto3
import aws_json # import anytime you know you need to parse annoying aws json
ec2 = boto3.client('ec2')
ec2_res = ec2.describe_instances()
aws_json.pretty_print(ec2_res)
rds = boto3.client('rds')
rds_res = rds.describe_db_instances()
aws_json.pretty_print(rds_res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment