|
| 1 | +# Creating a CloudWatch dashboard with function name as a variable |
| 2 | + |
| 3 | +This tutorial guides you through creating a CloudWatch dashboard that uses a property variable to display metrics for different Lambda functions. You'll learn how to create a dashboard with a dropdown menu that allows you to switch between Lambda functions without creating separate dashboards for each function. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before you begin this tutorial, make sure you have the following: |
| 8 | + |
| 9 | +1. The AWS CLI. If you need to install it, follow the [AWS CLI installation guide](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html). |
| 10 | +2. Configured your AWS CLI with appropriate credentials. Run `aws configure` if you haven't set up your credentials yet. |
| 11 | +3. At least one Lambda function in your AWS account. If you don't have any Lambda functions, this tutorial includes steps to create a simple test function. |
| 12 | +4. Sufficient permissions to create and manage CloudWatch dashboards and Lambda functions in your AWS account. |
| 13 | + |
| 14 | +### Cost considerations |
| 15 | + |
| 16 | +This tutorial uses AWS resources that are either included in the AWS Free Tier or have minimal costs: |
| 17 | + |
| 18 | +- CloudWatch Dashboards: First 3 dashboards are free. Additional dashboards cost $3.00 per dashboard per month. |
| 19 | +- CloudWatch Metrics: Standard metrics for AWS services like Lambda are included at no additional charge. |
| 20 | +- CloudWatch API Calls: First 1 million API calls per month are free. |
| 21 | + |
| 22 | +If you follow the cleanup instructions at the end of this tutorial, you should incur no charges or minimal charges. |
| 23 | + |
| 24 | +## Create a CloudWatch dashboard |
| 25 | + |
| 26 | +First, let's create a basic CloudWatch dashboard that will serve as the foundation for our dynamic dashboard with variables. |
| 27 | + |
| 28 | +**Create an empty dashboard** |
| 29 | + |
| 30 | +The following command creates a new empty CloudWatch dashboard: |
| 31 | + |
| 32 | +```bash |
| 33 | +aws cloudwatch put-dashboard --dashboard-name LambdaMetricsDashboard --dashboard-body '{ |
| 34 | + "widgets": [] |
| 35 | +}' |
| 36 | +``` |
| 37 | + |
| 38 | +This command creates a dashboard named "LambdaMetricsDashboard" with no widgets. The dashboard body is specified as a JSON string that defines the layout and content of the dashboard. |
| 39 | + |
| 40 | +## Add Lambda metrics widgets with a function name variable |
| 41 | + |
| 42 | +Now, let's create a more comprehensive dashboard that includes Lambda metrics widgets and a function name variable. We'll define the dashboard body in a JSON file for better readability. |
| 43 | + |
| 44 | +**Create the dashboard body JSON file** |
| 45 | + |
| 46 | +First, create a JSON file that defines the dashboard layout, widgets, and variables. Replace `us-east-1` in the region fields with your preferred AWS region: |
| 47 | + |
| 48 | +```bash |
| 49 | +cat > dashboard-body.json << EOF |
| 50 | +{ |
| 51 | + "widgets": [ |
| 52 | + { |
| 53 | + "type": "metric", |
| 54 | + "x": 0, |
| 55 | + "y": 0, |
| 56 | + "width": 12, |
| 57 | + "height": 6, |
| 58 | + "properties": { |
| 59 | + "metrics": [ |
| 60 | + [ "AWS/Lambda", "Invocations", "FunctionName", "\${FunctionName}" ], |
| 61 | + [ ".", "Errors", ".", "." ], |
| 62 | + [ ".", "Throttles", ".", "." ] |
| 63 | + ], |
| 64 | + "view": "timeSeries", |
| 65 | + "stacked": false, |
| 66 | + "region": "us-east-1", |
| 67 | + "title": "Lambda Function Metrics for \${FunctionName}", |
| 68 | + "period": 300 |
| 69 | + } |
| 70 | + }, |
| 71 | + { |
| 72 | + "type": "metric", |
| 73 | + "x": 0, |
| 74 | + "y": 6, |
| 75 | + "width": 12, |
| 76 | + "height": 6, |
| 77 | + "properties": { |
| 78 | + "metrics": [ |
| 79 | + [ "AWS/Lambda", "Duration", "FunctionName", "\${FunctionName}", { "stat": "Average" } ] |
| 80 | + ], |
| 81 | + "view": "timeSeries", |
| 82 | + "stacked": false, |
| 83 | + "region": "us-east-1", |
| 84 | + "title": "Duration for \${FunctionName}", |
| 85 | + "period": 300 |
| 86 | + } |
| 87 | + }, |
| 88 | + { |
| 89 | + "type": "metric", |
| 90 | + "x": 12, |
| 91 | + "y": 0, |
| 92 | + "width": 12, |
| 93 | + "height": 6, |
| 94 | + "properties": { |
| 95 | + "metrics": [ |
| 96 | + [ "AWS/Lambda", "ConcurrentExecutions", "FunctionName", "\${FunctionName}" ] |
| 97 | + ], |
| 98 | + "view": "timeSeries", |
| 99 | + "stacked": false, |
| 100 | + "region": "us-east-1", |
| 101 | + "title": "Concurrent Executions for \${FunctionName}", |
| 102 | + "period": 300 |
| 103 | + } |
| 104 | + } |
| 105 | + ], |
| 106 | + "periodOverride": "auto", |
| 107 | + "variables": [ |
| 108 | + { |
| 109 | + "type": "property", |
| 110 | + "id": "FunctionName", |
| 111 | + "property": "FunctionName", |
| 112 | + "label": "Lambda Function", |
| 113 | + "inputType": "select", |
| 114 | + "values": [ |
| 115 | + { |
| 116 | + "value": "my-lambda-function", |
| 117 | + "label": "my-lambda-function" |
| 118 | + } |
| 119 | + ] |
| 120 | + } |
| 121 | + ] |
| 122 | +} |
| 123 | +EOF |
| 124 | +``` |
| 125 | + |
| 126 | +This JSON file defines a dashboard with three metric widgets that display different Lambda metrics: Invocations, Errors, Throttles, Duration, and Concurrent Executions. The dashboard also includes a variable named "FunctionName" that allows you to select different Lambda functions from a dropdown menu. |
| 127 | + |
| 128 | +**Apply the dashboard configuration** |
| 129 | + |
| 130 | +Now, apply this dashboard configuration using the following command: |
| 131 | + |
| 132 | +```bash |
| 133 | +aws cloudwatch put-dashboard --dashboard-name LambdaMetricsDashboard --dashboard-body file://dashboard-body.json |
| 134 | +``` |
| 135 | + |
| 136 | +This command creates a dashboard with the specified widgets and variable. The `file://` prefix tells the AWS CLI to read the dashboard body from the specified file rather than treating it as a literal string. |
| 137 | + |
| 138 | +## Verify the dashboard |
| 139 | + |
| 140 | +After creating the dashboard, you can verify that it was created successfully and check its configuration. |
| 141 | + |
| 142 | +**List all dashboards** |
| 143 | + |
| 144 | +To see a list of all your CloudWatch dashboards, use the following command: |
| 145 | + |
| 146 | +```bash |
| 147 | +aws cloudwatch list-dashboards |
| 148 | +``` |
| 149 | + |
| 150 | +This command returns a list of all dashboards in your account, including the one you just created. |
| 151 | + |
| 152 | +**Get dashboard details** |
| 153 | + |
| 154 | +To view the details of your specific dashboard, use the following command: |
| 155 | + |
| 156 | +```bash |
| 157 | +aws cloudwatch get-dashboard --dashboard-name LambdaMetricsDashboard |
| 158 | +``` |
| 159 | + |
| 160 | +This command returns the full configuration of your dashboard, including the dashboard body JSON. You can verify that the variable and widgets are configured correctly. |
| 161 | + |
| 162 | +## Access and use the dashboard in the console |
| 163 | + |
| 164 | +While you've created the dashboard using the AWS CLI, you'll need to use the CloudWatch console to interact with the dropdown variable. |
| 165 | + |
| 166 | +1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/ |
| 167 | +2. In the navigation pane, choose **Dashboards** |
| 168 | +3. Select your **LambdaMetricsDashboard** |
| 169 | +4. You should see a dropdown menu labeled "Lambda Function" at the top of the dashboard |
| 170 | +5. Use this dropdown to select different Lambda functions and see their metrics displayed in the dashboard widgets |
| 171 | + |
| 172 | +The dashboard will automatically update all widgets to show metrics for the selected Lambda function. |
| 173 | + |
| 174 | +## Understanding the dashboard configuration |
| 175 | + |
| 176 | +Let's break down the key components of the dashboard configuration: |
| 177 | + |
| 178 | +**Widgets** |
| 179 | + |
| 180 | +Each widget in the dashboard is configured to display specific Lambda metrics. The `${FunctionName}` placeholder in the metrics configuration is replaced with the value selected in the dropdown menu. |
| 181 | + |
| 182 | +**Variables** |
| 183 | + |
| 184 | +The `variables` section defines a property variable with the following attributes: |
| 185 | + |
| 186 | +- `type`: "property" indicates this is a property variable |
| 187 | +- `id`: The unique identifier for the variable |
| 188 | +- `property`: The CloudWatch metric dimension that will be changed (FunctionName) |
| 189 | +- `label`: The display label for the dropdown menu |
| 190 | +- `inputType`: "select" creates a dropdown menu |
| 191 | +- `values`: An array of values to populate the dropdown menu |
| 192 | + |
| 193 | +When you select a different function from the dropdown, all widgets that use `${FunctionName}` in their configuration will update to show metrics for the selected function. |
| 194 | + |
| 195 | +## Troubleshooting |
| 196 | + |
| 197 | +Here are solutions to common issues you might encounter: |
| 198 | + |
| 199 | +**Dashboard validation errors** |
| 200 | + |
| 201 | +If you receive validation errors when creating the dashboard, check: |
| 202 | +- The JSON syntax in your dashboard body |
| 203 | +- That all required fields are present in the variable definition |
| 204 | +- That the region specified in the widgets is valid |
| 205 | + |
| 206 | +**Lambda functions not appearing in dropdown** |
| 207 | + |
| 208 | +If Lambda functions don't appear in your dropdown: |
| 209 | +- Verify that you have Lambda functions in your account |
| 210 | +- Check that the functions have metrics available in CloudWatch |
| 211 | +- Ensure you have permissions to view the Lambda metrics |
| 212 | + |
| 213 | +**Metrics not displaying** |
| 214 | + |
| 215 | +If metrics don't display for selected functions: |
| 216 | +- Confirm the function has been invoked recently (Lambda metrics only appear after function invocation) |
| 217 | +- Check that you're looking at the appropriate time range in the dashboard |
| 218 | +- Verify that the region in the widget configuration matches the region where your Lambda functions are deployed |
| 219 | + |
| 220 | +## Going to production |
| 221 | + |
| 222 | +This tutorial demonstrates how to create a CloudWatch dashboard with a function name variable for educational purposes. When implementing this in a production environment, consider these additional best practices: |
| 223 | + |
| 224 | +**Security considerations:** |
| 225 | +- Implement proper IAM permissions to restrict who can view and modify dashboards |
| 226 | +- Consider using resource tags to organize and control access to your dashboards |
| 227 | +- Implement CloudWatch alarms for critical metrics to receive notifications when issues occur |
| 228 | + |
| 229 | +**Architecture best practices:** |
| 230 | +- For large environments, organize multiple dashboards by application or team |
| 231 | +- Implement automated dashboard creation and updates using AWS CloudFormation or other IaC tools |
| 232 | +- Consider cross-account and cross-region monitoring for distributed applications |
| 233 | +- Implement a tagging strategy for Lambda functions to enable more sophisticated filtering |
| 234 | + |
| 235 | +For more information on building production-ready monitoring solutions: |
| 236 | +- [AWS Well-Architected Framework - Operational Excellence Pillar](https://docs.aws.amazon.com/wellarchitected/latest/operational-excellence-pillar/welcome.html) |
| 237 | +- [AWS Well-Architected Framework - Reliability Pillar](https://docs.aws.amazon.com/wellarchitected/latest/reliability-pillar/welcome.html) |
| 238 | +- [CloudWatch Best Practices](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html) |
| 239 | + |
| 240 | +## Clean up resources |
| 241 | + |
| 242 | +When you're finished with the dashboard, you can delete it to avoid cluttering your CloudWatch console. |
| 243 | + |
| 244 | +**Delete the dashboard** |
| 245 | + |
| 246 | +To delete the dashboard, use the following command: |
| 247 | + |
| 248 | +```bash |
| 249 | +aws cloudwatch delete-dashboards --dashboard-names LambdaMetricsDashboard |
| 250 | +``` |
| 251 | + |
| 252 | +This command removes the dashboard from your account. The `delete-dashboards` command accepts multiple dashboard names, allowing you to delete multiple dashboards at once if needed. |
| 253 | + |
| 254 | +Don't forget to delete the JSON file if you no longer need it: |
| 255 | + |
| 256 | +```bash |
| 257 | +rm dashboard-body.json |
| 258 | +``` |
| 259 | + |
| 260 | +## Next steps |
| 261 | + |
| 262 | +Now that you've learned how to create a CloudWatch dashboard with a function name variable, you can explore other CloudWatch features: |
| 263 | + |
| 264 | +1. [Create composite alarms](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Create_Composite_Alarm.html) to monitor multiple metrics and conditions. |
| 265 | +2. [Create anomaly detection alarms](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Create_Anomaly_Detection_Alarm.html) to automatically detect unusual behavior in your metrics. |
| 266 | +3. [Use metric math](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/using-metric-math.html) to perform calculations on your metrics and create more advanced visualizations. |
| 267 | +4. [Create cross-account dashboards](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-crossaccount-dashboard.html) to monitor resources across multiple AWS accounts. |
| 268 | +5. [Use CloudWatch Logs Insights](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html) to analyze and visualize your log data alongside your metrics. |
0 commit comments