20

I am trying to export and than import AWS Gateway API, following the instructions in https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-migrate-accounts-regions/.

Export is working:

aws apigateway get-export --parameters extensions='apigateway' --rest-api-id MY_REST_API_ID --stage-name Prod --export-type swagger my-api-apigateway.json

Identical files for my API are generated with --parameters extensions='apigateway' and --parameters extensions='integrations'.

But when I try to do the import from the exported file:

aws apigateway import-rest-api --fail-on-warnings --body file://%cd%/my-api-gateway.json

, I am always getting "Invalid base64: " error. Like this:

Invalid base64: "{
  "swagger" : "2.0",
  "info" : {
    "version" : "1.0",
    "title" : "my-stack-name"
  },
  "host" : "MY_REST_API_ID.execute-api.eu-central-1.amazonaws.com",
  "basePath" : "/Prod",
  ...

No documentation and no examples on google say that body should be Base64.

The same JSON seems to work when I import it via UI (Actions -> Import API).

I've also tried to use --cli-input-json:

my-api-apigateway-cli-json.json file (according to aws apigateway import-rest-api --generate-cli-skeleton):

{
  "failOnWarnings": true,
  "parameters": {
    "endpointConfigurationTypes": "REGIONAL"
  },
  "body": {... JSON FROM EXPORT ...}
}

Import command:

aws apigateway import-rest-api --cli-input-json file://./my-api-apigateway-cli-json.json

, but it says

Parameter validation failed:
Invalid type for parameter body, value: {'swagger': '2.0', ...

So, the questions are:

  • Should we encode the json as base64?
  • Why is this error and behaviour not documented (or if yes, then where?)?
  • How to successfully execute import from cli?

Useful links

None of the links say that body response should be Base64

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34

2 Answers2

35

When importing rest-api into AWS API Gateway using the AWS CLI its not mandatory to encode the json as base64.

I suspect you are using AWS CLI v2 and this issue you are facing, I believe it's as a result of changes introduced in AWS CLI version 2. i.e

AWS CLI version 2 now passes all binary input and binary output parameters as base64-encoded strings by default

Resolution:

You will need add --cli-binary-format raw-in-base64-out so that it tells AWS CLI v2 to revert to the AWS CLI v1 behavior:

aws apigateway import-rest-api --cli-binary-format raw-in-base64-out --body file://my-api-apigateway.json
aksyuma
  • 2,957
  • 1
  • 15
  • 29
  • Cool, this seems to work. Really non-obvious. Looks like my aws-cli version is actually 2.0: `aws --version` shows `aws-cli/2.0.0dev5 Python/3.7.5 Windows/10 botocore/2.0.0dev4`. I thought that aws-cli 2 will be actually executed as `aws2` (this is wrong according to https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-windows.html), and AFAIR I have installed both of them. – Dmitriy Popov Mar 19 '20 at 16:13
  • 5
    This works for other options for apigateway such as `put-rest-api` – l33tHax0r Jan 04 '21 at 17:44
  • 1
    This should totally be documented in the edge help pages. e.g. `aws apigateway put-rest-api help` – Matt Beckman Oct 04 '21 at 21:24
7

Using fileb:// instead of file:// worked for me. For example,

aws apigateway import-rest-api --body fileb://my-api.json
user3113045
  • 3,243
  • 2
  • 16
  • 10