【问题标题】:Referencing REST API resource from CDK in amplify custom resource在放大自定义资源中从 CDK 引用 REST API 资源
【发布时间】:2022-06-21 23:09:39
【问题描述】:

我需要为 REST API 端点自定义方法处理程序并将其指向 SQS 而不是 Lambda 函数。我正在拖延尝试获得对 RestApi 对象的完整引用...

我从RestApi.fromRestApiId 得到的 RestApi 对象不完整;我不能这样做:

const restApi = apigateway.RestApi.fromRestApiId(this, 'RestApi', dependencies.api.rest.ApiId);
const queueResource = restApi.root.resourceForPath('/webhooks');

...没有收到此错误: Error: root is not configured when imported using fromRestApiId(). Use fromRestApiAttributes() API instead.

我不能使用RestApi.fromRestApiAttributes,因为它需要rootResourceId -- 我似乎找不到参考RestApi.fromRestApiAttributes 的文档显示了这一点,但我没有 props

const api = RestApi.fromRestApiAttributes(this, 'RestApi', {
  restApiId: props.restApiId,
  rootResourceId: props.rootResourceId,
});

有谁知道如何访问rootResourceId

【问题讨论】:

    标签: aws-api-gateway aws-amplify aws-cdk


    【解决方案1】:

    root resource (/) id 是一个字母数字字符串,例如 4cfzeywftb,可以在控制台面包屑中找到:

    APIs > API (076t2zozc0) > Resources> / (4cfzeywftb)
    

    或致电get-resources:

    aws apigateway get-resources --rest-api-id 076t2zozc0
    
    {
        "items": [
            {
                "id": "4cfzeywftb",
                "path": "/",
                "resourceMethods": {
                    "ANY": {}
                }
            },
            {
                "id": "36g7tq",
                "parentId": "4cfzeywftb",
                "pathPart": "{proxy+}",
                "path": "/{proxy+}",
                "resourceMethods": {
                    "ANY": {}
                }
            }
        ]
    }
    

    【讨论】:

    • 控制台无法工作——我正在尝试将其整合到 IaC 堆栈中,以便为开发人员等提供这些环境。难道没有直接使用 CDK 的方法吗?
    • @Ryan 你从哪里采购props.restApiId?那将是简短的答案。但是,如果您不能将资源 ID 视为静态值,则另一个惯用选项是在您的 CDK 应用程序代码中使用 JS SDK 调用 GetResources 以获取 rootResourceIdat synth-time
    • 抱歉,props.restApiId 来自文档。使用AmplifyHelpers.addResourceDependency获取api信息包含ApiId但不包含restResourceId...
    • @Ryan 请将此新信息添加到 OP。另外,console.log 您从帮助程序获得的 API ID。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    相关资源
    最近更新 更多