【问题标题】:AWS Cloudformation Cloudwatch Dashboard : Ref ApiGateway TitleAWS Cloudformation Cloudwatch 仪表板:参考 ApiGateway 标题
【发布时间】:2018-10-18 05:51:32
【问题描述】:

我有一个 Cloudformation 模板,其中定义了 API 网关,标题为 Employee Management API。当我为 API Gateway 定义 Cloudwatch 仪表板时,我喜欢引用这个标题。现在,我将 API Gateway 的标题硬编码到仪表板指标中。相反,如果我能够引用 API 网关的 title 属性,那就更好了。

下面粘贴的是定义 API 网关和仪表板的 Cloudformation 模板的一部分。

API 网关的 Cloudformation 模板:

EmployeeApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      DefinitionBody:
        swagger: "2.0"
        info:
          description: "This API allows clients to query and manage employees"
          version: "1.0.0"
          title: "Employee Management API"
          contact:
            email: "me@me.com"
        basePath: "/v1"
        tags:
        - name: "employee"
          description: "Operations related to a employee"
        schemes:
        - "https"
        paths:
          /brands:
.
.
.

API 网关仪表板的 Cloudformation 模板

EmployeeAPIDashboard:
    Type: AWS::CloudWatch::Dashboard
    Properties:
      DashboardName: "EmployeeAPIDashboard"
      DashboardBody:
        Fn::Sub: '{
              "widgets": [
                  {
                     "type": "metric",
                     "x": 0,
                     "y": 0,
                     "width": 6,
                     "height": 6,
                     "properties": {
                         "view": "timeSeries",
                         "stacked": false,
                         "metrics": [
                             [ "AWS/ApiGateway", "IntegrationLatency", "ApiName", "Employee Management API", "Stage", "Prod", { "period": 86400, "yAxis": "right", "stat": "Sum" } ],
                             [ ".", "Count", ".", ".", ".", ".", { "period": 86400, "stat": "Sum"} ],
                             [ ".", "Latency", ".", ".", ".", ".", { "period": 86400, "yAxis": "right", "stat": "Sum"} ],
                             [ ".", "5XXError", ".", ".", ".", ".", { "period": 86400, "stat": "Sum", "color": "#FF0000" } ],
                             [ ".", "4XXError", ".", ".", ".", ".", { "period": 86400, "stat": "Sum", "color": "#FFA500" } ]
                         ],
                         "region": "us-west-2",
                         "period": 300,
                         "title": "API Gateway"
                     }
                  }
              ]
          }'

【问题讨论】:

    标签: yaml aws-api-gateway amazon-cloudformation amazon-cloudwatch


    【解决方案1】:

    截至 2018 年 10 月,此功能不受支持。 Return values for AWS::ApiGateway::RestApi 不包括 ApiName。此外,Cloudwatch metrics for ApiGateway 仅支持对 ApiName 进行过滤。我们使用的解决方法是在两个地方引用相同的堆栈参数。所以在你的情况下,你可以做类似的事情

    Parameters:
      MyApiName:
        Type: String
        Default: "Employee Management API"
    Resources:
      EmployeeApiGatewayApi:
        Type: AWS::Serverless::Api
          Properties:
            DefinitionBody:
              info:
                title: !Ref MyApiName
    
      EmployeeAPIDashboard:
        Type: AWS::CloudWatch::Dashboard
          Properties:
            DashboardBody:
              Fn::Sub: '{
                "widgets": [
                  {
                     "properties": {
                         "metrics": [
                             [ "ApiName", ${MyApiName} ]
    

    【讨论】:

      【解决方案2】:

      我相信您可以像这样跨多个堆栈引用堆栈Output

      http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/walkthrough-crossstackref.html

      【讨论】:

      • 这似乎不相关
      猜你喜欢
      • 2022-01-13
      • 2021-07-07
      • 2015-03-04
      • 2022-08-03
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      相关资源
      最近更新 更多