部署Lambda函数时如何从无服务器获取API网关ID作为输出部分 [英] How to get API Gateway ID as output section from the serverless when lambda functions are deployed

查看:82
本文介绍了部署Lambda函数时如何从无服务器获取API网关ID作为输出部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在无服务器的输出部分中获取API网关的ID,然后将其转换为API URL"https://fgh5t4tjm2.execute-api.us-east-1.amazonaws.com/dev" ;我在js代码中使用的代码.

I wanted to get the ID of the API Gateway in the output section of the serverless and then convert it into API URL "https://fgh5t4tjm2.execute-api.us-east-1.amazonaws.com/dev" which I am using in js code.

以下是我在无服务器脚本中通过lambda创建的api网关设置.我认为这是无服务器所做的更隐藏的创作之一,只需在lambda上设置一个事件即可为您创建一个api网关.我不确定它是否在serverless.yml的输出部分输出API网关ID.

Below is my api gateway setup by the lambda creation in the serverless script. I think It’s one of the more hidden creations that serverless does where just by setting up an event on the lambda it creates an api gateway for you. I’m not sure if it outputs the API gateway ID at the output section of the serverless.yml.

service: my-service

provider:
  name: aws
  stage: ${opt:stage, 'dev'}
  runtime: nodejs12.x
  region: ${env:AWS_REGION}
  stackName: my-stack-name

plugins:
  - serverless-dotenv-plugin

functions:
  GetCallHistory:
    handler: lambdas/getCallHistory.GetCallHistory
    environment:
      CH_CW_LOG_GROUP_NAME: ${env:CH_CW_LOG_GROUP_NAME}
    description: This lambda queries cloudwatch logs and retrieves the call history using a contactId.
    role: GetCallHistoryRole
    events:
      - http:
          path: callhistory/{contactId}
          method: get
          cors: true
          authorizer:
            name: CognitoPoolAuthorizer
            type: COGNITO_USER_POOLS
            arn: ${env:COGNITO_USER_POOL_ARN}

推荐答案

好问题:我仍然有一个可工作的template.yml(尽管它在JSON中,但假装它是yml)!

Great question: I still have a working template.yml around (though it's in JSON but just pretend it's yml)!

仅参考"ApiGateWay"这是在您的资源之内

Just reference to the "ApiGateWay" that's within your resources

"ApiGatewayApi": {
  "Type": "AWS::Serverless::Api",
  "Properties": {
    "StageName": { "Ref": "environment" },
    "DefinitionBody": "",
    "EndpointConfiguration": "REGIONAL"
  }
},

下面是一个有关如何在URL中进行引用并输出资源的示例:

Below an example on how I reference within a url and output the resources:

"Outputs" : {
    "ApiURL" : {
        "Description" : "API endpoint URL for ${environment} environment",
        "Value" : { "Fn::Sub" : "https://${ApiGatewayApi}.execute-api.${AWS::Region}.amazonaws.com/${environment}/" }
    },
    "RestApiId" : {
        "Value" : { "Ref" : "ApiGatewayApi" } 

这篇关于部署Lambda函数时如何从无服务器获取API网关ID作为输出部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆