gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:39:39 24 4
gpt4 key购买 nike

我正在尝试使用无服务器框架构建 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com