我正在使用Serverless:创建CloudWatch面板
PublishingDashboard:
Type: AWS::CloudWatch::Dashboard
Properties:
DashboardName: publishing-dashboard-${self:provider.targetStack}
DashboardBody: ${file(./Dashboards/fetchDashboard.js):publishingDashboard}
fetchDashboard.publishingDashboard如下所示:
module.exports.publishingDashboard = async (serverless) => {
let file = await fsPromises.readFile(path.join(__dirname, 'publishing-dashboard.json'), 'utf-8');
file = file.replace(/{{TARGET_STACK}}/g, serverless.variables.options.targetStack);
file = file.replace(/{{REGION}}/g, serverless.variables.options.region);
return file;
};
我想显示的一个小部件给我带来了问题:
{
"height": 6,
"width": 3,
"y": 0,
"x": 9,
"type": "metric",
"properties": {
"metrics": [
[ { "expression": "SEARCH('{AWS/States,StateMachineArn} StateMachineArn=*{{TARGET_STACK}}productName* MetricName=ExecutionTime', 'Maximum', 86400)", "label": "", "id": "e1", "region": "{{REGION}}" } ],
[ "AWS/States", "ExecutionTime", "StateMachineArn", "arn:aws:states:{{REGION}}:123467890:stateMachine:First-StepFunction-{{TARGET_STACK}}productName", { "id": "m1", "label": "First", "visible": false } ],
[ "...", "arn:aws:states:{{REGION}}:123467890:stateMachine:Second-StepFunction-{{TARGET_STACK}}productName", { "id": "m2", "label": "Second", "visible": false } ],
[ "...", "arn:aws:states:{{REGION}}:123467890:stateMachine:Third-StepFunction-{{TARGET_STACK}}productName", { "id": "m3", "label": "Third", "visible": false } ],
],
"view": "pie",
"region": "{{REGION}}",
"stat": "Average",
"period": 300,
"legend": {
"position": "hidden"
},
"title": "Execution Time",
"labels": {
"visible": false
},
"liveData": true,
"setPeriodToTimeRange": true,
"stacked": false
}
正文引用了accountId,但serverless参数不包括它。是否可以去掉正文中的accountId或获取要包含在serverless参数中的帐户id。
您可以使用${aws:accountId}
引用您的帐户ID。这在无服务器变量文档的引用AWS特定变量部分进行了描述。
当使用JavaScript函数解析变量时,它会接收一个对象作为参数,该对象具有一个名为resolveVariable
的函数,可用于解析提供的变量字符串。请参见此处。查看您的代码,我怀疑您使用的是不支持此功能的不推荐使用的解析器。您必须通过在YAML的service
部分中声明variablesResolutionMode: 20210326
进行升级。
函数还可以通过可变解析过程访问配置属性。完整示例:
module.exports.publishingDashboard = async ({resolveVariable}) => {
let file = await fsPromises.readFile(path.join(__dirname, 'publishing-dashboard.json'), 'utf-8');
file = file.replace(/{{TARGET_STACK}}/g, resolveVariable('opt:targetStack'));
file = file.replace(/{{REGION}}/g, resolveVariable('opt:region'));
file = file.replace(/{{ACCOUNT_ID}}/g, resolveVariable('aws:accountId'));
return file;
};