Skip to content

How do I backup and restore API Gateway APIs?

3 minute read
0

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:

  1. Open the API Gateway console.
  2. Select your REST or HTTP API.
  3. For REST APIs, deploy your API to a stage.
  4. In the Stage Editor pane, choose Stage actions, and then choose Export.
  5. For API specification type, choose Swagger or OpenAPI 3.
  6. For Format, choose either JSON or YAML based on your preferred file type for the API definition file download.
  7. For Extensions, choose one of the following options: 
    Export without extensions
    Export with API Gateway extensions
    Export with Postman extensions
  8. 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:

  1. Open the API Gateway console.
  2. Choose Create API.
  3. Under REST API, choose Import.
  4. 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.
  5. Under API endpoint type, choose one of the following types: 
    Regional
    Edge-optimized

    Private
  6. 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.
  7. 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:

  1. Open the API Gateway console.
  2. Choose Create API.
  3. Under HTTP API, choose Import.
  4. 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.
  5. 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