如何在cloudformation脚本中指定API网关URL

问题描述 投票:0回答:1

我正在使用 cloudformation 脚本 yaml 文件在 AWS 开发帐户上创建资源。

我的目标是创建一个带有 API 网关端点的 lambda 函数。 lambda 函数及其内部的网关已成功创建。但是当我尝试在脚本中指定 API 端点 URL 时,出现以下错误。

Resource handler returned message: "Invalid API identifier specified 442645024664:test-api- 
gateway-ms (Service: ApiGateway, Status Code: 404, Request ID: 47e0c115-02c4-4dfd-b13a- 
b149c72807cf)" (RequestToken: a3fdecd7-41fd-cfef-f538-2da3bac33ec2, HandlerErrorCode: 
NotFound)

下面是我的 template.yaml 堆栈。

Parameters:
  MyApi:
  Type: String
  Description: "This is a demo API"
  AllowedValues: [ "dev-demo-lambda-api-poc" ]

Resources:
  MyDemoLambdaApiFunction:
  Type: AWS::Serverless::Function
  Properties:
    Description: >
    Currently does not support S3 upload event.
  Handler: app.lambda_handler
  Runtime: python3.11
  CodeUri: .
  MemorySize: 1028
  Events:
    MyDemoAPI:
      Type: Api
      Properties:
        Path: /test
        Method: GET
  Tracing: Active

Deployment:
  Type: AWS::ApiGateway::Deployment
  Properties:
    Description: Demo Lambda API Gateway deployment
    RestApiId: !Ref MyApi

Outputs:
  MyApiUrl:
  Description: API Gateway URL
  Value:
    Fn::Sub: https://dev-demo-lambda-api-poc

抛开错误不谈,在为 API 创建端点时我有点困惑。在 CFN 脚本中提供端点 URL 的正确方法是什么?

我相信我应该先部署它,然后才能得到终点?

非常感谢任何帮助。

python amazon-web-services aws-lambda aws-cloudformation aws-api-gateway
1个回答
0
投票

输出

要输出默认 API 网关端点,您需要遵循以下格式:

https://{api-id}.execute-api.{region}.amazonaws.com/{stage}

Outputs:
  MyApiDefaultUrl:
    Description: API Gateway URL
    Value: !Sub "https://${MyApiGateway}.execute-api.${AWS::Region}.amazonaws.com/<stageName>"

# Where:
# ${MyApiGateway} is the reference to you API Gateway resource
# <stageName> is the Stage Name

# If you have a separate AWS::ApiGateway::Stage
Outputs:
  MyApiDefaultUrl:
  Description: My API Gateway Default URL
  Value: !Sub "https://${MyApiGateway}.execute-api.${AWS::Region}.amazonaws.com/${MyApiStage}"

请参阅:https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html


自定义域名

要为您的 API 提供自定义域名,您需要使用

AWS::ApiGateway::DomainName

请参阅 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-domainname.html 了解模板。

请参阅为 REST API 设置自定义域名了解先决条件。

© www.soinside.com 2019 - 2024. All rights reserved.