# Exporting CloudWatch Metric Streams via Firehose and AWS Lambda to Amazon Managed Service for Prometheus !!! warning This site is being merged into the broader [Observability Best Practices](https://aws-observability.github.io/observability-best-practices/recipes/) content. Please head over there for the latest updates, plus prescriptive guidance on the use of AWS observability tools. !!! warning This site will be kept as-is until January 2023, when it will be decommissioned. *** In this recipe we show you how to instrument a [CloudWatch Metric Stream](https://console.aws.amazon.com/cloudwatch/home#metric-streams:streamsList) and use [Kinesis Data Firehose](https://aws.amazon.com/kinesis/data-firehose/) and [AWS Lambda](https://aws.amazon.com/lambda) to ingest metrics into [Amazon Managed Service for Prometheus (AMP)](https://aws.amazon.com/prometheus/). We will be setting up a stack using [AWS Cloud Development Kit (CDK)](https://aws.amazon.com/cdk/) to create a Firehose Delivery Stream, Lambda, and a S3 Bucket to demonstrate a complete scenario. !!! note This guide will take approximately 30 minutes to complete. ## Infrastructure In the following section we will be setting up the infrastructure for this recipe. CloudWatch Metric Streams allow forwarding of the streaming metric data to a HTTP endpoint or [S3 bucket](https://aws.amazon.com/s3). ### Prerequisites * The AWS CLI is [installed](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) and [configured](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) in your environment. * The [AWS CDK Typescript](https://docs.aws.amazon.com/cdk/latest/guide/work-with-cdk-typescript.html) is installed in your environment. * Node.js and Go. * The [repo](https://github.com/aws-observability/aws-o11y-recipes/) has been cloned to your local machine. ### Create an AMP workspace Our demo application in this recipe will be running on top of AMP. Create your AMP 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 ``` !!! info For more details check out the [AMP Getting started](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP-getting-started.html) guide. ### Install dependencies From the root of the aws-o11y-recipes repository, change your directory to CWMetricStreamExporter via 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 following command: ``` cd lambda ``` Once in the `/lambda` folder, install the Go dependencies using: ``` go get ``` All the dependencies are now installed. ### Modify config file In the root of the repo, open `config.yaml` and modify the AMP workspace URL by replacing the `{workspace}` with the newly created workspace id, and the region your AMP 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 S3 Bucket to your liking. ### Deploy stack Once the `config.yaml` has been modified with the AMP workspace ID, it is time to deploy the stack to CloudFormation. To build the 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. Accept the following IAM changes to deploy the stack: ![Screen shot of the IAM Changes when deploying the CDK](../images/cdk-amp-iam-changes.png) Verify that the stack has been created by running the following command: ``` aws cloudformation list-stacks ``` A stack by the name `CDK Stack` should have been created. ## Create CloudWatch stream Navigate to the CloudWatch consoloe, for example `https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#metric-streams:streamsList` and click "Create metric stream". Select the metics needed, either all metrics of only from selected namespaces. Configure the Metric Stream by using an existing Firehose which was created by the CDK. Change the output format to JSON instead of OpenTelemetry 0.7. Modify the Metric Stream name to your liking, and click "Create metric stream": ![Screen shot of the Cloudwatch Metric Stream Configuration](../images/cloudwatch-metric-stream-configuration.png) To verify the Lambda function invocation, navigate to the [Lambda console](https://console.aws.amazon.com/lambda/home) and click the function `KinesisMessageHandler`. Click the `Monitor` tab and `Logs` subtab, and under `Recent Invocations` there should be entries of the Lambda function being triggered. !!! note It may take upto 5 minutes for invocations to show in the Monitor tab. That is it! Congratulations, your metrics are now being streamed from CloudWatch to Amazon Managed Service for Prometheus. ## Cleanup First, delete the CloudFormation stack: ``` cd cdk cdk destroy ``` Remove the AMP workspace: ``` aws amp delete-workspace --workspace-id \ `aws amp list-workspaces --alias prometheus-sample-app --query 'workspaces[0].workspaceId' --output text` ``` Last but not least, remove the CloudWatch Metric Stream by removing it from the console.