Search code examples
amazon-web-servicesaws-api-gatewayaws-amplify

Why can't I access my REST API in AWS API Gateway?


I am trying to call a method on my REST API on AWS API Gateway but I am getting the following error message: API MyAPI does not exist

This is how I configure AWS Amplify from my code and call it from my App.tsx similarly to what is documented on https://docs.amplify.aws/lib/restapi/getting-started/q/platform/js/#configure-your-application:

import { Amplify } from 'aws-amplify';

export const AwsConfig = () => {

    Amplify.configure({
        Auth: {
            region: 'xxxxxxxx',
            userPoolId: 'xxxxxxxx',
            userPoolWebClientId: 'xxxxxxxx',
            mandatorySignIn: true,
        },
        API: {
            name: "MyAPI",
            endpoint: "https://xxxxxxx.execute-api.xxxxxxx.amazonaws.com/test",
            region: 'xxxxxxxx',
        }
    });
}

I then tried using the following based on what the ./aws-exports file that Amplify CLI automatically generates and my REST API call works fine:

import { Amplify } from 'aws-amplify';

export const AwsConfig = () => {

    Amplify.configure({
        aws_project_region: 'xxxxxxxx',
        aws_cloud_logic_custom: [
            {
              name: 'MyAPI',
              endpoint: 'https://xxxxxxxx.execute-api.xxxxxxxx.amazonaws.com/test',
              region: 'xxxxxxxx'
            }
          ],
        Auth: {
            region: 'xxxxxxxx',
            userPoolId: 'xxxxxxxx',
            userPoolWebClientId: 'xxxxxxxx',
            mandatorySignIn: true,
        },
    });
}

Why is that and what is wrong with the first approach?


Solution

  • I think in the first approach you need to specify API as having the endpoints list (with single api in it). The region value might also be causing it to not work (this only seems to be needed when using regional endpoints / being pointed at a lambda (as opposed to API Gateway). I'd try the following:

        API: { 
            endpoints: [
                {
                    name: "MyAPI",
                    endpoint: "https://xxxxxxx.executeapi.xxxxxxx.amazonaws.com/test",
                }
            ]
        }