aws-cloudformation - 如何在 CloudWatch Dashboard 的 CloudFormation 模板中引用 AWS HTTP API (v2)?

标签 aws-cloudformation amazon-cloudwatch serverless serverless-framework aws-serverless

我正在尝试使用无服务器框架构建 AWS HTTP API (API Gateway v2),并且还想创建一个 CloudWatch 控制面板,该控制面板使用 CloudWatch 控制面板的 CloudFormation 模板来显示有关此 HTTP API 的指标。

根据 AWS 的说法,他们不支持按 API 名称列出的 HTTP API 指标。他们仅通过 API ID 支持。问题是,有时 API ID 在部署时会发生变化,因此它不可靠,我无法将其硬编码到 CloudWatch 模板中。是否可以从无服务器文件引用 HTTP API 并检索其 ID,以便将其传递到 CloudWatch CloudFormation 模板?

下面是我的代码片段:

provider:
  httpApi:
    metrics: true

resources:
  Resources:
    MyDashboard:
      Type: AWS::CloudWatch::Dashboard
      Properties: 
        DashboardName: MyDashboard
        DashboardBody: '
        {
          "widgets": [
            {
            "type": "metric",
            "properties": {
                "metrics": [
                    [
                        "AWS/ApiGateway",
                        "4xx",
                        "Resource",
                        "/{proxy+}",
                        "Stage",
                        "$default",
                        "Method",
                        "ANY",
                        "ApiId",
                        "f8d7a6fd8", ============> I want this to be a variable such as ${self:provider.myApiId}
                        {
                            "yAxis": "left"
                        }
                    ]
                ],
                "view": "bar",
                "stacked": false,
                "region": "us-west-2",
                "period": 300,
                "stat": "Sum",
                "setPeriodToTimeRange": true
            }
          }
          ]
        }
        '

最佳答案

尝试子函数: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html

在这种情况下:

      DashboardBody: 
        !Sub |
        {
          "widgets": [
            {
            "type": "metric",
            "properties": {
                "metrics": [
                    [
                        "AWS/ApiGateway",
                        "4xx",
                        "Resource",
                        "/{proxy+}",
                        "Stage",
                        "$default",
                        "Method",
                        "ANY",
                        "ApiId",
                        "${self:provider.myApiId}",
                        {
                            "yAxis": "left"
                        }
                    ]
                ],
                "view": "bar",
                "stacked": false,
                "region": "us-west-2",
                "period": 300,
                "stat": "Sum",
                "setPeriodToTimeRange": true
            }
          }
          ]
        }

关于aws-cloudformation - 如何在 CloudWatch Dashboard 的 CloudFormation 模板中引用 AWS HTTP API (v2)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73095546/

相关文章:

monitoring - 实现类似指标聚合器的云监视以实时监控应用程序

javascript - Zeit (Vercel) 现在无服务器身份验证请求因 CORS 而失败

.net-core - 如何登录到 Azure Functions 2.0 中的其他日志提供程序

amazon-web-services - 资源必须采用 ARN 格式或 "*"

amazon-web-services - 为什么我得到这个 {"message": "Internal server error" } from Postman

aws-cloudformation - 当 Elasticsearch 请求过高时设置 AWS cloudwatch 警报

python - 使用 Azure Functions 的无服务器 Django

amazon-web-services - 如果所需的容器数量大于 1,则 ECS 服务处于挂起状态

amazon-web-services - 如何在 Service Catalog 的 AWS CloudFormation 参数中列出所有私有(private) AMI

amazon-web-services - 在 Cloudformation 模板中使用 AWS API Gateway 的 IAM 角色