I want to use either the Amazon API Gateway console or the AWS Command Line Interface (AWS CLI) to backup and restore my API Gateway APIs.
Resolution
From API Gateway, export a REST API or HTTP API to a Swagger or OpenAPI 3 definition file. Then, import the API to API Gateway.
Note: If you receive errors when you run AWS CLI commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.
Export the REST API or HTTP API
API Gateway console
Complete the following steps:
- Open the API Gateway console.
- Select your REST or HTTP API.
- For REST APIs, deploy your API to a stage.
- In the Stage Editor pane, choose Stage actions, and then choose Export.
- For API specification type, choose Swagger or OpenAPI 3.
- For Format, choose either JSON or YAML based on your preferred file type for the API definition file download.
- For Extensions, choose one of the following options:
Export without extensions
Export with API Gateway extensions
Export with Postman extensions
- Choose Export API.
AWS CLI
To export a REST API, run the following get-export command:
aws apigateway get-export \ --rest-api-id your-api-id \ --stage-name your-stage-name \ --export-type swagger \ --accepts application/json \ --output output.json
Note: Replace your-api-id with your API ID and your-stage-name with your stage name.
To export an HTTP API, run the following export-api command:
aws apigatewayv2 export-api \ --api-id your-api-id \ --output-type JSON \ --specification HTTP \ --no-include-extensions > http-api-backup.json
Note: Replace your-api-id with your API ID.
Import the REST API
API Gateway console
Complete the following steps:
- Open the API Gateway console.
- Choose Create API.
- Under REST API, choose Import.
- Under API definition, use one of the following options to import your definition file:
Choose Choose file, and then select your updated API definition file.
-or-
Enter your updated API definition into the text editor field.
- Under API endpoint type, choose one of the following types:
Regional
Edge-optimized
Private
- Choose Fail on warnings to stop an import when there's an error or warning during import. Or, choose Ignore warnings to import the API when there's an error or warning during import.
- Choose Create API.
AWS CLI
Run the following import-rest-api command:
aws apigateway import-rest-api --body 'fileb:///path/to/API_Swagger_template.json'
Import the HTTP API
API Gateway console
Complete the following steps:
- Open the API Gateway console.
- Choose Create API.
- Under HTTP API, choose Import.
- Under Import an OpenAPI 3 definition, use one of the following options to import your definition file:
Choose Select an OpenAPI 3 definition file, and then select your updated API definition file.
-or-
Enter your updated API definition into the text editor field.
- Choose Create API.
AWS CLI
Run the following import-api command:
aws apigatewayv2 import-api \ --body file://api-definition.yaml
Related information
Tutorial: Create a REST API by importing an example
How do I migrate API Gateway REST APIs between AWS accounts or Regions?
Develop REST APIs using OpenAPI in API Gateway