Can you help me write some monitoring code using AWS CDK and the monitoring construct to…
- User
Can you help me write some monitoring code using AWS CDK and the monitoring construct to create a dashboard and allow cross account access to said dashboard to another account. In the relevant account, we have a lambda listening to an sqs queue (with a sqs dead letter queue attached). This queue is subscribed to an sns topic from another account. What we want to monitor is the processing of these sqs events and the lambda invocations for issues or errors especially but generally these two components.
Assistant
Certainly! Below is a step-by-step guide to writing AWS CDK code to create a monitoring dashboard using CloudWatch and allowing cross-account access. We'll monitor an SQS queue and a Lambda function, including error metrics.
Step 1: Setup AWS CDK Project
First, ensure you have the AWS CDK installed. If not, install it using:
npm install -g aws-cdkCreate a new CDK project:
mkdir monitoring-dashboard cd monitoring-dashboard cdk init app --language typescriptStep 2: Install Necessary Packages
Install the necessary CDK packages:
npm install @aws-cdk/aws-cloudwatch @aws-cdk/aws-lambda @aws-cdk/aws-sqs @aws-cdk/aws-sns @aws-cdk/aws-sns-subscriptions @aws-cdk/aws-iamStep 3: Write the CDK Code
Replace the contents of
lib/monitoring-dashboard-stack.tswith the following code:import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as lambda from 'aws-cdk-lib/aws-lambda'; import * as sqs from 'aws-cdk-lib/aws-sqs'; import * as sns from 'aws-cdk-lib/aws-sns'; import * as subscriptions from 'aws-cdk-lib/aws-sns-subscriptions'; import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch'; import * as iam from 'aws-cdk-lib/aws-iam'; export class MonitoringDashboardStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // SQS Queue const deadLetterQueue = new sqs.Queue(this, 'DeadLetterQueue', { queueName: 'dead-letter-queue', }); const sqsQueue = new sqs.Queue(this, 'SqsQueue', { queueName: 'main-queue', deadLetterQueue: { maxReceiveCount: 10, // Move messages to DLQ after 10 failed attempts queue: deadLetterQueue, }, }); // Lambda Function const lambdaFunction = new lambda.Function(this, 'LambdaFunction', { functionName: 'queue-processor', runtime: lambda.Runtime.NODEJS_16_X, handler: 'index.handler', code: lambda.Code.fromInline(` exports.handler = async (event) => { console.log("Processing event:", event); // Your processing logic here return { statusCode: 200, body: "Success" }; }; `), environment: { QUEUE_URL: sqsQueue.queueUrl, }, }); // Grant the Lambda function permission to access the SQS queue sqsQueue.grantConsumeMessages(lambdaFunction); deadLetterQueue.grantSendMessages(lambdaFunction); // SNS Topic from another account const snsTopicArn = 'arn:aws:sns:<region>:<account-id>:<topic-name>'; // Replace with actual ARN const snsTopic = sns.Topic.fromTopicArn(this, 'ImportedSnsTopic', snsTopicArn); // Subscribe SQS queue to the SNS topic snsTopic.addSubscription(new subscriptions.SqsSubscription(sqsQueue)); // CloudWatch Dashboard const dashboard = new cloudwatch.Dashboard(this, 'Dashboard', { dashboardName: 'SQS-Lambda-Monitoring', }); // SQS Metrics const sqsApproximateAgeOfOldestMessage = new cloudwatch.Metric({ namespace: 'AWS/SQS', metricName: 'ApproximateAgeOfOldestMessage', dimensionsMap: { QueueName: sqsQueue.queueName }, }); const sqsNumberOfMessagesVisible = new cloudwatch.Metric({ namespace: 'AWS/SQS', metricName: 'ApproximateNumberOfMessagesVisible', dimensionsMap: { QueueName: sqsQueue.queueName }, }); const sqsNumberOfMessagesNotVisible = new cloudwatch.Metric({ namespace: 'AWS/SQS', metricName: 'ApproximateNumberOfMessagesNotVisible', dimensionsMap: { QueueName: sqsQueue.queueName }, }); // Lambda Metrics const lambdaErrors = new cloudwatch.Metric({ namespace: 'AWS/Lambda', metricName: 'Errors', dimensionsMap: { FunctionName: lambdaFunction.functionName }, }); const lambdaInvocations = new cloudwatch.Metric({ namespace: 'AWS/Lambda', metricName: 'Invocations', dimensionsMap: { FunctionName: lambdaFunction.functionName }, }); const lambdaDuration = new cloudwatch.Metric({ namespace: 'AWS/Lambda', metricName: 'Duration', dimensionsMap: { FunctionName: lambdaFunction.functionName }, statistic: 'Average', }); // Add widgets to the dashboard dashboard.addWidgets( new cloudwatch.TextWidget({ width: 24, height: 2, markdown: '# SQS and Lambda Monitoring Dashboard', }), new cloudwatch.GraphWidget({ title: 'SQS Queue Metrics', left: [sqsApproximateAgeOfOldestMessage, sqsNumberOfMessagesVisible,sqsNumberOfMessagesNotVisible], }), new cloudwatch.GraphWidget({ title: 'Lambda Metrics', left: [lambdaInvocations, lambdaErrors,lambdaDuration], }) ); // Allow cross-account access to the dashboard const crossAccountPrincipal = new iam.AccountPrincipal('<account-id>'); // Replace with the account ID that needs access const dashboardArn = `arn:aws:cloudwatch:${this.region}:${this.account.accountId}:dashboard/${dashboard.dashboardName}`; const dashboardPolicy = new iam.PolicyStatement({ actions: ['cloudwatch:GetDashboard', 'cloudwatch:ListDashboards'], resources: [dashboardArn], principals: [crossAccountPrincipal], }); const dashboardPolicyStatement = new iam.Policy(this, 'DashboardPolicy', { statements: [dashboardPolicy], }); } }Step 4: Deploy the Stack
Synthesize and deploy the stack:
cdk synth cdk deployExplanation:
-
SQS Queue and Dead Letter Queue:
- We create an SQS queue with a dead letter queue to capture messages that fail processing after a specified number of attempts.
-
Lambda Function:
- A simple Lambda function is created to process messages from the SQS queue. The function is granted the necessary permissions to read from the SQS queue and send messages to the dead letter queue.
-
SNS Topic Subscription:
- We subscribe the SQS queue to an SNS topic from another account. Replace the
snsTopicArnwith the actual ARN of the SNS topic.
- We subscribe the SQS queue to an SNS topic from another account. Replace the
-
CloudWatch Dashboard:
- A CloudWatch dashboard is created to monitor the SQS queue and Lambda function.
- Metrics for the SQS queue include
ApproximateAgeOfOldestMessageandApproximateNumberOfMessagesVisible. - Metrics for the Lambda function include
ErrorsandInvocations.
-
Cross-Account Access:
- An IAM policy is created to allow a specified AWS account to access the CloudWatch dashboard. Replace
<account-id>with the actual account ID that needs access.
- An IAM policy is created to allow a specified AWS account to access the CloudWatch dashboard. Replace
Note:
- Ensure you replace placeholder values like
<region>,<account-id>, and<topic-name>with actual values. - The Lambda function code is a simple inline code for demonstration. Replace it with your actual processing logic.
- The cross-account IAM policy allows the specified account to view the dashboard. Adjust permissions as necessary.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.