【问题标题】:Reference AWS API Gateway's hostname within the serverless.yml file for other CloudFormation resources?在 serverless.yml 文件中引用 AWS API Gateway 的主机名以获取其他 CloudFormation 资源?
【发布时间】:2017-11-15 23:25:13
【问题描述】:

我正在使用无服务器框架通过 S3、Lambda 和 API Gateway 创建图像大小调整服务。这类似于 here 的概念,但是这将使用无服务器来设置和配置整个堆栈。

现在我需要找到一种体面的方法来引用 serverless.yml 文件中生成的 API Gateway 的主机名。这是我在我的资源下拥有的。 (只是一个sn-p)

imageBucket:
  Type: AWS::S3::Bucket
  Properties:
    BucketName: ${env:IMAGE_BUCKET}
    WebsiteConfiguration:
      RoutingRules:
        -
          RedirectRule:
            Hostname: ${HOSTNAME HERE}
            HttpRedirectCode: 302
          RoutingRuleCondition:
            HttpErrorCodeReturnedEquals: 404

${HOSTNAME HERE} 我需要成为无服务器框架生成的 API Gateway API 的主机名。

现在,我能想到的最好方法是在某处创建一个 CNAME 别名并在我的 API 前面使用它。然后我将该 CNAME 作为环境变量传递,然后在此文件中引用它。但这对我来说并不理想。我希望有人能够将这个项目拉下来,并能够只用一个存储桶名称来运行它。有没有办法做到这一点?

【问题讨论】:

    标签: amazon-web-services aws-api-gateway serverless-framework serverless-architecture amazon-cloudformation


    【解决方案1】:

    cloudformation 不会直接暴露主机名,因此您必须根据已创建资源的输出值来构建它。 无服务器框架的所有资源都遵循一定的命名约定: AWS - Resources

    有了这些信息,您可以通过以下语句创建您的主机名:

    imageBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${env:IMAGE_BUCKET}
        WebsiteConfiguration:
          RoutingRules:
            -
              RedirectRule:
                Hostname: 
                  Fn::Join:
                    - ""
                    - - Ref: ApiGatewayRestApi
                      - ".execute-api."
                      - Ref: AWS::Region
                      - ".amazonaws.com"
                HttpRedirectCode: 302
              RoutingRuleCondition:
                HttpErrorCodeReturnedEquals: 404
    

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 2021-06-03
      • 2019-02-22
      • 2017-08-03
      • 2020-08-30
      • 2018-02-21
      • 1970-01-01
      • 2020-08-21
      • 2017-11-08
      相关资源
      最近更新 更多