DEV Community

JetSquirrel
JetSquirrel

Posted on

How to Use Send AWS CloudWatch Metrics to GreptimeCloud by Grafana Alloy

Image description

Introduction

CloudWatch is AWS monitoring service for storing and querying metrics, logs, and traces from your cloud platform. To save or analyze this data independently, you can export and store it using the CloudWatch API. Tools like Datadog and Sysdig use similar methods for AWS monitoring.
Prometheus is a widely used monitoring platform known for its simplicity and ease of use. However, it faces challenges with high availability (HA) and high cardinality. The commonly used HA solution, Thanos, adds complexity to maintenance. In contrast, GreptimeDB, a modern time-series database, natively supports high availability and is fully compatible with the Prometheus protocol.
This article explains how to work with AWS CloudWatch metrics. It covers using Grafana Alloy to collect and transform CloudWatch data into Prometheus format, storing it in GreptimeCloud, and visualizing the results using the GreptimeCloud Console.
Core Tools
AWS CloudWatch: A service for collecting and monitoring AWS resources.
Grafana Alloy: A opensource tool metricsdata collection, transformation, and transmission.
GreptimeCloud: A high-performance platform for storing and analyzing time-series data.

Steps

Create IAM User in AWS and Attach Required Policy for Alloy

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1674249227793",
      "Action": [
        "tag:GetResources",
        "cloudwatch:GetMetricData",
        "cloudwatch:GetMetricStatistics",
        "cloudwatch:ListMetrics",
        "ec2:DescribeTags",
        "ec2:DescribeInstances",
        "ec2:DescribeRegions",
        "iam:ListAccountAliases",
        "ec2:DescribeTransitGateway*",
        "apigateway:GET",
        "dms:DescribeReplicationInstances",
        "dms:DescribeReplicationTasks"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Attach this policy to the corresponding AWS IAM User and save the user's credentials to the ~/.aws/credentials file on the instance running Grafana Alloy.

  1. Configure Grafana Alloy Download and install according to the official documentation. Create a config.alloy file in the same directory as Alloy with the following structure:
prometheus.exporter.cloudwatch "aws_ec2_metrics" {
    ......
}
prometheus.scrape "awsmonitoring" {
    ......
}
prometheus.remote_write "greptimedb" {
    ......
}
Enter fullscreen mode Exit fullscreen mode

2.1 prometheus.exporter.cloudwatch​, configure to pull metrics from CloudWatch

prometheus.exporter.cloudwatch "aws_ec2_metrics" {
sts_region = "ap-east-1"
    discovery {
        type    = "AWS/EC2"
        regions = ["ap-east-1"]
        metric {
            name       = "CPUUtilization"
            statistics = ["Average"]
            period     = "5m"
        }
        metric {
            name       = "NetworkPacketsIn"
            statistics = ["Average"]
            period     = "5m"
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

CPUUtilization: The average CPU usage over a 5-minute period.
NetworkPacketsIn: The average number of incoming network packets over a 5-minute period.

2.2 prometheus.scrape "awsmonitoring", configure capture targets and write objects
prometheus.scrape "awsmonitoring" {
targets = prometheus.exporter.cloudwatch.aws_ec2_metrics.targets
forward_to = [prometheus.remote_write.greptimedb.receiver]
}‍
2.3 prometheus.remote_write "greptimedb" ​,configure the target for writing
Please login to GreptimeCloud's console, create or select the corresponding service, select Alloy in Connect, and copy the configuration to config.alloy.

Image description

Write to the Alloy counterpart prometheus.remote_write section

prometheus.remote_write "greptimedb" {
    endpoint {
        url = "https://**************.us-west-2.aws.greptime.cloud/v1/prometheus/write?db=**************"basic_auth {
        username = "************************"
        password = "************************"
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Start Alloy Run the following command to start Alloy: alloy run config.alloy
  2. Verification and Visualization Log into GreptimeCloud and check if data is being uploaded successfully

Image description

You can use PromQL to query data in Greptime

Image description

Connect to visualization platforms like Grafana,use GreptimeCloud Console Workbench for visualization

Image description

In Greptime Console, create a new aws_ec2.json file in Workbench with the provided JSON configuration for dashboard setup.

{
  "kind": "Dashboard",
  "metadata": {
    "name": "aws_ec2",
    "project": "default",
    "version": 0
  },
  "spec": {
    "display": {
      "name": "aws_ec2"
    },
    "panels": {
      "CPUUtilization": {
        "kind": "Panel",
        "spec": {
          "display": {
            "name": "CPUUtilization"
          },
          "plugin": {
            "kind": "TimeSeriesChart",
            "spec": {
              "yAxis": {
                "show": true,
                "label": "",
                "format": {
                  "unit": "percent"
                }
              },
              "legend": {
                "position": "bottom"
              }
            }
          },
          "queries": [
            {
              "kind": "TimeSeriesQuery",
              "spec": {
                "plugin": {
                  "kind": "PrometheusTimeSeriesQuery",
                  "spec": {
                    "query": "aws_ec2_cpuutilization_average",
                    "seriesNameFormat": "{{ dimension_InstanceId}}"
                  }
                }
              }
            }
          ]
        }
      },
      "CPUUtilization-1": {
        "kind": "Panel",
        "spec": {
          "display": {
            "name": "NetworkPacketsIn"
          },
          "plugin": {
            "kind": "TimeSeriesChart",
            "spec": {
              "yAxis": {
                "show": true,
                "label": "",
                "format": {
                  "unit": "bytes"
                }
              },
              "legend": {
                "position": "bottom"
              }
            }
          },
          "queries": [
            {
              "kind": "TimeSeriesQuery",
              "spec": {
                "plugin": {
                  "kind": "PrometheusTimeSeriesQuery",
                  "spec": {
                    "query": "aws_ec2_network_packets_in_average",
                    "seriesNameFormat": "{{ dimension_InstanceId}}"
                  }
                }
              }
            }
          ]
        }
      }
    },
    "layouts": [
      {
        "kind": "Grid",
        "spec": {
          "display": {
            "title": "Panel Group",
            "collapse": {
              "open": true
            }
          },
          "items": [
            {
              "x": 0,
              "y": 0,
              "width": 13,
              "height": 7,
              "content": {
                "$ref": "#/spec/panels/CPUUtilization"
              }
            },
            {
              "x": 0,
              "y": 7,
              "width": 13,
              "height": 7,
              "content": {
                "$ref": "#/spec/panels/CPUUtilization-1"
              }
            }
          ]
        }
      }
    ],
    "variables": [],
    "duration": "1h",
    "refreshInterval": "0s"
  }
}
Enter fullscreen mode Exit fullscreen mode

Summary

We've shown how to pull, transform, store, and visualize metrics from AWS CloudWatch using GreptimeCloud and Grafana Alloy.
This solution is easy to set up and maintain, making it ideal for beginners and experts alike. By leveraging the powerful features of GreptimeDB, you can create a reliable monitoring platform that adds significant value to your cloud monitoring and operations.

Top comments (0)