Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws apigateway import-rest-api returns "Invalid base64" error

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

  • https://docs.aws.amazon.com/apigateway/api-reference/link-relation/restapi-import/ -- here body is clearly documented as // raw byte array representing the api definition
like image 202
Dmitriy Popov Avatar asked Mar 18 '20 16:03

Dmitriy Popov


2 Answers

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
like image 124
syumaK Avatar answered Nov 14 '22 23:11

syumaK


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

aws apigateway import-rest-api --body fileb://my-api.json
like image 41
user3113045 Avatar answered Nov 15 '22 00:11

user3113045