17

从命令行部署后如何找到 API 网关的 URL 地址?

我使用类似于下面的脚本来部署我的 API Gateway 和 Authorizer,并且部署得很好。

https://github.com/floodfx/aws-lambda-proxy-using-sam-local/blob/master/deploy.sh

我试图弄清楚如何从命令行部署后获取 API 网关的地址

API 网关被创建,我可以看到堆栈:

aws cloudformation describe-stacks
"Stacks": [
        {
            "StackId": "arn:aws:cloudformation:us-east-1:761861444952:stack/mygateway912/72100720-6e67-11e8-93e9-500c28604c4a", 
            "Description": "An example serverless \"Hello World2 \" application with a custom authorizer.", 
            "Tags": [], 
            "CreationTime": "2018-06-12T17:38:40.946Z", 
            "Capabilities": [
                "CAPABILITY_IAM"
            ], 
            "StackName": "mygateway912", 
            "NotificationARNs": [], 
            "StackStatus": "CREATE_COMPLETE", 
            "DisableRollback": false, 
            "ChangeSetId": "arn:aws:cloudformation:us-east-1:76161444952:changeSet/awscli-cloudformation-package-deploy-1528825120/352f7c7a-2870-44ea-9e7f-40d16c0015df", 
            "LastUpdatedTime": "2018-06-12T17:38:46.411Z"
        }

我必须有一个简单的命令才能得到这个。

4

2 回答 2

25

我只是有时间正确回答。有 API 网关定义:

Resources:
  ...
  ServerlessRestApi:
    Type: AWS::Serverless::Api
    DeletionPolicy: "Retain"
    Properties:
      StageName: Prod
  ...

你可以输出

Outputs:
  ProdDataEndpoint:
    Description: "API Prod stage endpoint"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
于 2018-06-12T19:00:42.233 回答
11

我有单独AWS::ApiGateway::RestApiAWS::ApiGateway::Stage资源,所以我的输出看起来有点不同,因为我没有/不能硬编码阶段名称:

Outputs:
  ProdEndpoint:
    Value: !Sub "https://${ApiGw}.execute-api.${AWS::Region}.amazonaws.com/${ApiGwStage}/"

Resources:
  ApiGw:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Name: 'Serverless Ipsum #noServerNovember challenge'
      FailOnWarnings: true

  ApiGwDeployment:
    Type: AWS::ApiGateway::Deployment
    # Required -- see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-deployment.html
    DependsOn: ApiGwMethod
    Properties:
      RestApiId: !Ref ApiGw

  ApiGwStage:
    Type: AWS::ApiGateway::Stage
    Properties:
      DeploymentId: !Ref ApiGwDeployment
      MethodSettings:
        - DataTraceEnabled: true
          HttpMethod: '*'
          LoggingLevel: INFO
          ResourcePath: '/*'
      RestApiId: !Ref ApiGw
      StageName: prod

  ApiGwResource:
    Type: AWS::ApiGateway::Resource
    Properties:
      RestApiId: !Ref ApiGw
      ParentId: !GetAtt ["ApiGw", "RootResourceId"]
      PathPart: "{proxy+}"

  ApiGwMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      RestApiId: !Ref ApiGw
      ResourceId: !Ref ApiGwResource
      HttpMethod: ANY
      AuthorizationType: NONE
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ServerlessIpsumFunction.Arn}/invocations"
于 2018-11-07T19:23:32.277 回答