Skip to content

Instantly share code, notes, and snippets.

@jcaxmacher
Created May 12, 2021 20:00
Show Gist options
  • Save jcaxmacher/15881bf469372d8b9922f05bfe153150 to your computer and use it in GitHub Desktop.
Save jcaxmacher/15881bf469372d8b9922f05bfe153150 to your computer and use it in GitHub Desktop.
Get list of API Gateway REST APIs
import csv
import boto3
rest_apis = []
ec2 = boto3.client('ec2', region_name='us-east-1')
for region in ec2.describe_regions()['Regions']:
print(f"Checking region {region['RegionName']}")
session = boto3.Session(region_name=region['RegionName'])
apigw = session.client('apigateway')
for api in apigw.get_rest_apis()['items']:
print(f"Getting endpoints for {api['name']}")
rest_api = {
'region': region['RegionName'],
'id': api['id'],
'name': api['name'],
'endpointConfiguration': api['endpointConfiguration']
}
for stage in apigw.get_stages(restApiId=rest_api['id'])['item']:
url = f"https://{rest_api['id']}.execute-api.{region['RegionName']}.amazonaws.com/{stage['stageName']}"
stage_details = rest_api.copy()
stage_details.update({
'deploymentId': stage['deploymentId'],
'stageName': stage['stageName'],
'url': url
})
rest_apis.append(stage_details)
keys = rest_apis[0].keys()
with open('apigateway-rest-apis.csv', 'w') as f:
dw = csv.DictWriter(f, fieldnames=list(keys))
dw.writeheader()
dw.writerows(rest_apis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment