AWS 在 JavaScript 和 Python 中提供了示例自定义小组件。您可以使用此列表中各个小组件的链接来创建这些示例小组件。您也可以使用 CloudWatch 控制台创建和自定义小组件。此列表中的链接会打开 AWS CloudFormation 控制台并使用 AWS CloudFormation 快速创建链接来创建自定义小组件。
您还可以在 GitHub
在此列表之后,显示了每种语言的 Echo 小组件的完整示例。
JavaScript 中的示例自定义小组件
-
Echo
– 一个基本的回显器,可用于测试 HTML 在自定义小组件中的显示方式,而无需编写新的小组件。 -
Hello world
– 一个非常基本的入门小组件。 -
自定义小组件调试程序
– 一个调试程序小组件,会显示有关 Lambda 运行时环境的有用信息。 -
查询 CloudWatch Logs Insights
– 运行和编辑 CloudWatch Logs Insights 查询。 -
运行 Amazon Athena 查询
– 运行和编辑 Athena 查询。 -
调用 AWS API
– 调用任何只读 AWS API 并以 JSON 格式显示结果。 -
快速 CloudWatch 位图图表
– 在服务器端使用渲染 CloudWatch 图表,以便快速显示。 -
CloudWatch 控制面板中的文本小组件
– 显示指定 CloudWatch 控制面板中的第一个文本小组件。 -
CloudWatch 指标数据作为表
– 在表中显示原始 CloudWatch 指标数据。 -
Amazon EC2 表
– 按 CPU 使用率显示排名靠前的 EC2 实例。此小组件还包括一个重启按钮,该按钮默认处于禁用状态。 -
AWS CodeDeploy 部署
– 显示 CodeDeploy 部署。 -
AWS Cost Explorer 报告
– 显示选定时间范围内每项 AWS 服务的成本报告。 -
显示外部 URL 的内容
– 显示可从外部访问的 URL 的内容。 -
显示 Amazon S3 对象
– 显示您账户中 Amazon S3 存储桶中的对象。 -
简单的 SVG 饼图
– 基于 SVG 的图表小组件示例。
JavaScript 中的 Echo 小组件
以下是 JavaScript 中的 Echo 示例小组件。
const DOCS = `
## Echo
A basic echo script. Anything passed in the \`\`\`echo\`\`\` parameter is returned as the content of the custom widget.
### Widget parameters
Param | Description
---|---
**echo** | The content to echo back
### Example parameters
\`\`\` yaml
echo: <h1>Hello world</h1>
\`\`\`
`;
exports.handler = async (event) => {
if (event.describe) {
return DOCS;
}
let widgetContext = JSON.stringify(event.widgetContext, null, 4);
widgetContext = widgetContext.replace(/</g, '<');
widgetContext = widgetContext.replace(/>/g, '>');
return `${event.echo || ''}<pre>${widgetContext}</pre>`;
};
Python 中的 Echo 小组件
以下是 Python 中的 Echo 示例小组件。
import json
DOCS = """
## Echo
A basic echo script. Anything passed in the ```echo``` parameter is returned as the content of the custom widget.
### Widget parameters
Param | Description
---|---
**echo** | The content to echo back
### Example parameters
``` yaml
echo: <h1>Hello world</h1>
```"""
def lambda_handler(event, context):
if 'describe' in event:
return DOCS
echo = event.get('echo', '')
widgetContext = event.get('widgetContext')
widgetContext = json.dumps(widgetContext, indent=4)
widgetContext = widgetContext.replace('<', '<')
widgetContext = widgetContext.replace('>', '>')
return f'{echo}<pre>{widgetContext}</pre>'
Java 中的 Echo 小组件
以下是 Java 中的 Echo 示例小组件。
package example;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Handler implements RequestHandler<Event, String>{
static String DOCS = ""
+ "## Echo\n"
+ "A basic echo script. Anything passed in the ```echo``` parameter is returned as the content of the custom widget.\n"
+ "### Widget parameters\n"
+ "Param | Description\n"
+ "---|---\n"
+ "**echo** | The content to echo back\n\n"
+ "### Example parameters\n"
+ "```yaml\n"
+ "echo: <h1>Hello world</h1>\n"
+ "```\n";
Gson gson = new GsonBuilder().setPrettyPrinting().create();
@Override
public String handleRequest(Event event, Context context) {
if (event.describe) {
return DOCS;
}
return (event.echo != null ? event.echo : "") + "<pre>" + gson.toJson(event.widgetContext) + "</pre>";
}
}
class Event {
public boolean describe;
public String echo;
public Object widgetContext;
public Event() {}
public Event(String echo, boolean describe, Object widgetContext) {
this.describe = describe;
this.echo = echo;
this.widgetContext = widgetContext;
}
}