You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何无法访问AWS API Gateway的REST API?Amplify配置问题解析

AWS Amplify REST API配置错误:"API MyAPI does not exist"原因分析

问题场景

调用AWS API Gateway中的REST API时,收到错误提示:API MyAPI does not exist。按照官方文档手动编写的Amplify配置调用失败,但使用Amplify CLI自动生成的aws-exports格式配置后,API调用可正常执行。

两种配置对比

调用失败的手动配置

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',
        }
    });
}

正常执行的CLI生成配置

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,
        },
    });
}

原因分析

手动配置的核心问题

第一种配置的API结构不符合Amplify对REST API的配置规范:

  • Amplify的REST API配置需要将API定义放在API.REST字段下,以API名称为键存储单个API的配置(比如MyAPI作为键,对应包含endpoint、region的对象)。
  • 直接在API根节点下定义name、endpoint等属性,Amplify无法识别这是有效的REST API配置,因此找不到名为MyAPI的API,抛出错误。

CLI配置能正常工作的原因

aws_cloud_logic_custom是Amplify早期版本用于自定义REST API的配置字段,虽然新版本推荐使用API.REST结构,但Amplify依然保留对该旧字段的兼容性,因此CLI生成的配置能被正确解析。

修正后的正确手动配置

如果要手动配置REST API,应使用API.REST的标准结构:

import { Amplify } from 'aws-amplify';

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

内容的提问来源于stack exchange,提问作者Kronax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 18:12:39