Sample custom widgets - Amazon CloudWatch

Sample custom widgets

AWS provides sample custom widgets in both JavaScript and Python. You can create these sample widgets by using the link for each widget in this list. Alternatively, you can create and customize a widget by using the CloudWatch console. The links in this list open an AWS CloudFormation console and use an AWS CloudFormation quick-create link to create the custom widget.

You can also access the custom widget samples on GitHub.

Following this list, complete examples of the Echo widget are shown for each language.

JavaScript
Sample custom widgets in JavaScript
Python
Sample custom widgets in Python

Echo widget in JavaScript

The following is the Echo sample widget in JavaScript.

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, '&lt;'); widgetContext = widgetContext.replace(/>/g, '&gt;'); return `${event.echo || ''}<pre>${widgetContext}</pre>`; };

Echo widget in Python

The following is the Echo sample widget in Python.

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('<', '&lt;') widgetContext = widgetContext.replace('>', '&gt;') return f'{echo}<pre>{widgetContext}</pre>'

Echo widget in Java

The following is the Echo sample widget in Java.

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; } }