Integrating CloudWatch metrics with Amazon Managed Service for Prometheus
It can help to have all your metrics in one place. Amazon Managed Service for Prometheus does not automatically ingest Amazon CloudWatch metrics. However, you can use Amazon Data Firehose and AWS Lambda to push CloudWatch metrics to Amazon Managed Service for Prometheus.
This section describes how to instrument a Amazon CloudWatch metric stream and use Amazon Data Firehose and AWS Lambda to ingest metrics into Amazon Managed Service for Prometheus.
You will set up a stack using AWS Cloud Development
Kit (CDK)
Infrastructure
The first thing you must do is set up the infrastructure for this recipe.
CloudWatch metric streams allow forwarding of the streaming metric data to an HTTP endpoint or Amazon S3 bucket.
Setting up the infrastructure will consist of 4 steps:
-
Configuring prerequisites
-
Creating an Amazon Managed Service for Prometheus workspace
-
Installing dependencies
-
Deploying the stack
Prerequisites
-
The AWS CLI is installed and configured in your environment.
-
The AWS CDK Typescript is installed in your environment.
-
Node.js and Go are installed in your environment.
-
The AWS observability CloudWatch metrics exporter github repository
( CWMetricsStreamExporter
) has been cloned to your local machine.
To create a Amazon Managed Service for Prometheus workspace
-
The demo application in this recipe will be running on top of Amazon Managed Service for Prometheus. Create your Amazon Managed Service for Prometheus Workspace via the following command:
aws amp create-workspace --alias prometheus-demo-recipe
-
Ensure your workspace has been created with the following command:
aws amp list-workspaces
For more information about Amazon Managed Service for Prometheus, see Amazon Managed Service for Prometheus User Guide.
To install dependencies
-
Install dependencies
From the root of the
aws-o11y-recipes
repository, change your directory toCWMetricStreamExporter
using the command:cd sandbox/CWMetricStreamExporter
This will now be considered the root of the repo, going forward.
-
Change directory to
/cdk
via the following command:cd cdk
-
Install the CDK dependencies via the following command:
npm install
-
Change directory back to the root of the repo, and then change directory to
/lambda
using the following command:cd lambda
-
Once in the
/lambda
folder, install the Go dependencies using:go get
All the dependencies are now installed.
To deploy the stack
-
In the root of the repo, open
config.yaml
and modify the Amazon Managed Service for Prometheus workspace URL by replacing the{workspace}
with the newly created workspace id, and the region your Amazon Managed Service for Prometheus workspace is in.For example, modify the following with:
AMP: remote_write_url: "https://aps-workspaces.us-east-2.amazonaws.com/workspaces/{workspaceId}/api/v1/remote_write" region: us-east-2
Change the names of the Firehose delivery stream and Amazon S3 bucket to your liking.
-
To build the AWS CDK and the Lambda code, in the root of the repo run the following commend:
npm run build
This build step ensures that the Go Lambda binary is built, and deploys the CDK to CloudFormation.
-
To complete the deployment, review and accept the IAM changes that the stack requires.
-
(Optional) You can very if that the stack has been created by running the following command.
aws cloudformation list-stacks
A stack named
CDK Stack
will be in the list.
Creating a Amazon CloudWatch stream
Now that you have a lambda function to handle the metrics, you can create the metrics stream from Amazon CloudWatch.
To create an CloudWatch metrics stream
-
Navigate to the CloudWatch console, at https://console.aws.amazon.com/cloudwatch/home#metric-streams:streamsList
and select Create metric stream. -
Select the metrics needed, either all metrics, or only from selected namespaces.
-
Under
Configuration
, choose Select an existing Firehose owned by your account. -
You will be using the Firehose created earlier by the CDK. In the Select your Kinesis data Firehose stream drop down, select the stream created earlier. It will have a name like
CdkStack-KinesisFirehoseStream123456AB-sample1234
. -
Change the output format to JSON.
-
Give the metric stream a name that is meaningful to you.
-
Choose Create metric stream.
-
(Optional) To verify the Lambda function invocation, navigate to the Lambda console
and choose the function KinesisMessageHandler
. Select the Monitor tab and Logs subtab, and under Recent Invocations there should be entries of the Lambda function being triggered.Note
It may take up to 5 minutes before invocations begin to show in the Monitor tab.
Your metrics are now being streamed from Amazon CloudWatch to Amazon Managed Service for Prometheus.
Cleanup
You may want to clean up the resources that were used in this example. The following procedure explains how to do so. This will stop the metrics stream that you created.
To clean up resources
-
Start by deleting the CloudFormation stack with the following commands:
cd cdk cdk destroy
-
Remove the Amazon Managed Service for Prometheus workspace:
aws amp delete-workspace --workspace-id \ `aws amp list-workspaces --alias prometheus-sample-app --query 'workspaces[0].workspaceId' --output text`
-
Finally, remove the Amazon CloudWatch metric stream using the Amazon CloudWatch console
.