Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Amplify REST API cannot find the API

I'm playing with AWS Amplify since I've to introduce some of its feature in a legacy application that has no framework (no React, no Angular: just vanilla JS).

I used successfully the Auth module so I recreated a simple sign up/in/out strategy. What I want now is to call a REST API, using REST API module. So I created an API using API Gateway PetStore example and I would interact with it.

enter image description here

So I created the configuration:

import Amplify from '@aws-amplify/core';

Amplify.configure({
    Auth: {
       ...
    },
    API: {
        endpoints: [
            {
                name: "PetStore", // name of the API in API Gateway console
                endpoint: "https://XXX.execute-api.eu-central-1.amazonaws.com/dev",
                region: "eu-central-1",
                paths: ['/']
            }
        ]
    }
});

and somewhere else:

import Api from '@aws-amplify/api-rest'; 

Api.get('PetStore', '/pets', {})
   .then(response => {
       console.log(response)
   }).catch(error => {
       console.log(error);
   });

But, when executed, I get always the same error:

API PetStore does not exist

Any idea? Remember that:

  • API Already exist
  • I don't want to use Amplify CLI to create AWS resources
like image 742
BAD_SEED Avatar asked Dec 08 '20 09:12

BAD_SEED


People also ask

Does amplify use API gateway?

The Amplify API category provides an interface for making requests to your backend. The Amplify CLI deploys REST APIs and handlers using Amazon API Gateway and AWS Lambda.

How do I configure amplify manually?

To do this you simply install the amplify library as normal. After that you need to manually configure any features you're going to use with Amplify. configure();. You can find the manual configuration in the Amplify documentation for each library you plan to use.

How do I call an API gateway API from AWS?

To call a deployed API, clients submit requests to the URL for the API Gateway component service for API execution, known as execute-api . The base URL for REST APIs is in the following format: https:// {restapi_id} .execute-api. {region} .amazonaws.com/ {stage_name} /.

How do I deploy a REST API in API gateway?

Before you can invoke an API, you must deploy it in API Gateway. To do that, follow the instructions in Deploying a REST API in Amazon API Gateway . You can find a REST API's root URL in the Stage Editor for the API in the API Gateway console. It's listed as the Invoke URL at the top.

How do I call an AWS API from an HTML page?

You can handle these in a script behind an HTML page or in a client application using one of the AWS SDKs. For testing, you can use the API Gateway console to call an API by using the API Gateway's TestInvoke feature, which bypasses the Invoke URL and allows API testing before the API is deployed.

Why can't I use amplify push on my API?

Try changing the version to a previous version you know worked. API * does not exist usually means you haven't pushed the REST API you created. If you can't use amplify push have you manually created and amplify API through the console?


3 Answers

I'd like to add that @Deiv was correct to suggest API.configure(), this worked for me. After trying amplify pull and amplify push I still received the error API <api-name> does not exist.

So my code now looks like this:

import Amplify, { API } from 'aws-amplify';
import awsconfig from './aws-exports';

Amplify.configure(awsconfig);
API.configure(awsconfig);

I am using npm package: aws-amplify 3.3.21

like image 154
Isaac I9 Avatar answered Oct 23 '22 16:10

Isaac I9


I had this same issue and I managed to resolve it by configuring the exports on the "Api" object directly:

API.configure(aws_exports);

Previously it worked for me with just setting it on Amplify.configure, but it seems a newer version of Amplify requires it on the sub module itself.

Some more bits and pieces can be found here in this long-standing issue (in which the same issue popped up for Auth, but I applied the same to both in my case and it fixed it): https://github.com/aws-amplify/amplify-js/issues/4315

like image 30
Deiv Avatar answered Oct 23 '22 18:10

Deiv


You need to call API.configure() after Amplify.configure is called, or else your API setup won't be applied, hence it returns API PetStore does not exist

import Amplify, { API } from 'aws-amplify'; // this part depends on which Amplify you are using. but make sure to import API


Amplify.configure({
    Auth: {
       ...
    },
    API: {
        endpoints: [
            {
                name: "PetStore", // name of the API in API Gateway console
                endpoint: "https://XXX.execute-api.eu-central-1.amazonaws.com/dev",
                region: "eu-central-1",
                paths: ['/']
            }
        ]
    }
});

// Call this 
API.configure(); // no awsconfig required as you have set your own
like image 1
tomoima525 Avatar answered Oct 23 '22 18:10

tomoima525