Skip to content

Commit c94047e

Browse files
authored
feat(typescript/amazon-mq-rabbitmq-lambda) Add an example of Amazon MQ RabbitMQ (#1083)
* feat(typescript/amazon-mq-rabbitmq-lambda) Add an example of Amazon MQ RabbitMQ Integration with Lambda, Secrets Manager, and CloudWatch Logs * docs(typescript/amazon-mq-rabbitmq-lambda): Update README.md and fix code comments * docs(typescript/amazon-mq-rabbitmq-lambda): Update README.md * fix(typescript/amazon-mq-rabbitmq-lambda): Add missing lambda folder with code
1 parent 39f4dc3 commit c94047e

15 files changed

+490
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.js
2+
!jest.config.js
3+
*.d.ts
4+
node_modules
5+
6+
!lambda/consumer.js
7+
8+
# CDK asset staging directory
9+
.cdk.staging
10+
cdk.out
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.ts
2+
!*.d.ts
3+
4+
# CDK asset staging directory
5+
.cdk.staging
6+
cdk.out
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Amazon MQ RabbitMQ Integration with Lambda, Secrets Manager, and CloudWatch Logs
2+
<!--BEGIN STABILITY BANNER-->
3+
---
4+
5+
![Stability: Developer Preview](https://img.shields.io/badge/stability-Developer--Preview-important.svg?style=for-the-badge)
6+
7+
> **This is an experimental example. It may not build out of the box**
8+
>
9+
> This example is built on Construct Libraries marked "Developer Preview" and may not be updated for latest breaking changes.
10+
>
11+
> It may additionally requires infrastructure prerequisites that must be created before successful build.
12+
>
13+
> If build is unsuccessful, please create an [issue](https://github.com/aws-samples/aws-cdk-examples/issues/new) so that we may debug the problem
14+
---
15+
<!--END STABILITY BANNER-->
16+
This example demonstrates how to set up an Amazon MQ RabbitMQ cluster, integrate it with AWS Lambda for message processing,
17+
use AWS Secrets Manager for storing sensitive information, and configure CloudWatch Logs for monitoring.
18+
19+
## Deploy
20+
21+
To deploy this app, you need to be in this example's root folder.
22+
23+
Run `cdk deploy`. This will deploy / redeploy your Stack to your AWS Account.
24+
25+
After the deployment you will see the RabbitMQ Broker's endpoints, which will be similar to the following:
26+
27+
```
28+
AmazonMqRabbitmqLambdaStack.AmqpEndpointPort = 5671
29+
AmazonMqRabbitmqLambdaStack.AmqpEndpointUrl = amqps://<broker-id>.mq.us-west-2.amazonaws.com:5671
30+
AmazonMqRabbitmqLambdaStack.WebConsolePort = 443
31+
AmazonMqRabbitmqLambdaStack.WebConsoleUrl = https://<broker-id>.mq.us-west-2.amazonaws.com
32+
```
33+
34+
This will install the necessary CDK, then this example's dependencies, and then build your TypeScript files and your CloudFormation template.
35+
36+
## Testing with [producer.py](producer.py)
37+
> **Important:**
38+
> Update the `username`, `password`, and `broker_endpoint` fields in the [producer.py](producer.py) script as indicated by the comments in the code.
39+
>
40+
> - The `username` and `password` can be retrieved from the secret stored in **AWS Secrets Manager**. You can find the secret created during deployment by searching for the secret name in the AWS Management Console.
41+
> - The `broker_endpoint` should be set to the broker's endpoint shown in the terminal after the cdk deploy command finishes. Specifically, use the URL format: <broker-id>.mq.us-west-2.amazonaws.com
42+
43+
You can test the example by running the [producer.py](producer.py) script provided in the repository.
44+
This script connects to the RabbitMQ broker using the provided credentials and publishes three test messages to the `testQueue`.
45+
46+
After running the producer.py script, you can check the CloudWatch Log Group for your Lambda function to see the output from [consumer.js](lambda/consumer.js).
47+
The [consumer.js](lambda/consumer.js) file includes an example output that shows the details of the received messages, including their Base64-encoded data.
48+
49+
Additionally, you can check the RabbitMQ management console to see the three messages that were published by producer.py
50+
and consumed by the Lambda function, similar to the picture below.
51+
52+
![Queued Messages](images/queued-messages.png)
53+
54+
## Stack Components
55+
56+
![Component Diagram](images/amazon-mq-rabbitmq-lambda-diagram.png)
57+
58+
This stack includes:
59+
60+
- An Amazon MQ RabbitMQ single instance broker, provided by the [AWS::AmazonMQ L2 Construct Library](https://constructs.dev/packages/@cdklabs/cdk-amazonmq/v/0.0.1?lang=go#rabbitmq-brokers).
61+
- An AWS Lambda function for message processing, located in [consumer.js](lambda/consumer.js).
62+
- AWS Secrets Manager for storing RabbitMQ credentials.
63+
- A CloudWatch Log group for Lambda function logs.
64+
65+
## Testing
66+
```bash
67+
npm run test
68+
```
69+
70+
## Cleanup
71+
72+
To avoid incurring future charges, remember to destroy the resources:
73+
74+
```bash
75+
cdk destroy
76+
```
77+
78+
## Future Enhancements and Exploration Opportunities
79+
![Future Enhancements and Exploration Opportunities](images/Future-Enhancements-and-Exploration-Opportunities.png)
80+
81+
## Further Reading
82+
83+
- [Amazon MQ Documentation](https://docs.aws.amazon.com/amazon-mq/)
84+
- [AWS Lambda Developer Guide](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html)
85+
- [AWS Secrets Manager User Guide](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html)
86+
- [Amazon CloudWatch Logs User Guide](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html)
87+
88+
## Reference
89+
90+
- [AWS::AmazonMQ L2 Construct Library](https://constructs.dev/packages/@cdklabs/cdk-amazonmq/v/0.0.1?lang=go#rabbitmq-brokers)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
import * as cdk from 'aws-cdk-lib';
3+
import { AmazonMqRabbitmqLambdaStack } from '../lib/amazon-mq-rabbitmq-lambda-stack';
4+
5+
const app = new cdk.App();
6+
new AmazonMqRabbitmqLambdaStack(app, 'AmazonMqRabbitmqLambdaStack');
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"app": "npx ts-node --prefer-ts-exts bin/amazon-mq-rabbitmq-lambda.ts",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"**/*.d.ts",
11+
"**/*.js",
12+
"tsconfig.json",
13+
"package*.json",
14+
"yarn.lock",
15+
"node_modules",
16+
"test"
17+
]
18+
},
19+
"context": {
20+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
21+
"@aws-cdk/core:checkSecretUsage": true,
22+
"@aws-cdk/core:target-partitions": [
23+
"aws",
24+
"aws-cn"
25+
],
26+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
27+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
28+
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
29+
"@aws-cdk/aws-iam:minimizePolicies": true,
30+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
31+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
32+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
33+
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
34+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
35+
"@aws-cdk/core:enablePartitionLiterals": true,
36+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
37+
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
38+
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
39+
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
40+
"@aws-cdk/aws-route53-patters:useCertificate": true,
41+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
42+
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
43+
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
44+
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
45+
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
46+
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
47+
"@aws-cdk/aws-redshift:columnId": true,
48+
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
49+
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
50+
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
51+
"@aws-cdk/aws-kms:aliasNameRef": true,
52+
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
53+
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
54+
"@aws-cdk/aws-efs:denyAnonymousAccess": true,
55+
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true,
56+
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true,
57+
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true,
58+
"@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true,
59+
"@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true,
60+
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true,
61+
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true,
62+
"@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true,
63+
"@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true,
64+
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true,
65+
"@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true,
66+
"@aws-cdk/aws-eks:nodegroupNameAttribute": true,
67+
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true,
68+
"@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true,
69+
"@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault": false,
70+
"@aws-cdk/aws-s3:keepNotificationInImportedBucket": false,
71+
"@aws-cdk/aws-ecs:reduceEc2FargateCloudWatchPermissions": true,
72+
"@aws-cdk/aws-ec2:ec2SumTImeoutEnabled": true
73+
}
74+
}
109 KB
Loading
92.2 KB
Loading
63.4 KB
Loading
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
roots: ['<rootDir>/test'],
4+
testMatch: ['**/*.test.ts'],
5+
transform: {
6+
'^.+\\.tsx?$': 'ts-jest'
7+
}
8+
};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
exports.handler = async function(event) {
3+
console.log('Received event:', JSON.stringify(event, undefined, 2));
4+
};
5+
6+
/*
7+
8+
You will see the following output in CloudWatch Log Group after running producer.py
9+
{
10+
"eventSourceArn": "arn:aws:mq:us-west-2:<account-id>:broker:<broker-name>:<broker-id>",
11+
"rmqMessagesByQueue": {
12+
"testQueue::/": [
13+
{
14+
"basicProperties": {
15+
"contentType": null,
16+
"contentEncoding": null,
17+
"headers": null,
18+
"deliveryMode": 2,
19+
"priority": null,
20+
"correlationId": null,
21+
"replyTo": null,
22+
"expiration": null,
23+
"messageId": null,
24+
"timestamp": null,
25+
"type": null,
26+
"userId": null,
27+
"appId": null,
28+
"clusterId": null,
29+
"bodySize": 13
30+
},
31+
"redelivered": true,
32+
"data": "SGVsbG8gV29ybGQgMQ==" <-- Base64 Encoding of "Hello World 1"
33+
},
34+
{
35+
"basicProperties": {
36+
"contentType": null,
37+
"contentEncoding": null,
38+
"headers": null,
39+
"deliveryMode": 2,
40+
"priority": null,
41+
"correlationId": null,
42+
"replyTo": null,
43+
"expiration": null,
44+
"messageId": null,
45+
"timestamp": null,
46+
"type": null,
47+
"userId": null,
48+
"appId": null,
49+
"clusterId": null,
50+
"bodySize": 13
51+
},
52+
"redelivered": true,
53+
"data": "SGVsbG8gV29ybGQgMg==" <-- Base64 Encoding of "Hello World 2"
54+
},
55+
{
56+
"basicProperties": {
57+
"contentType": null,
58+
"contentEncoding": null,
59+
"headers": null,
60+
"deliveryMode": 2,
61+
"priority": null,
62+
"correlationId": null,
63+
"replyTo": null,
64+
"expiration": null,
65+
"messageId": null,
66+
"timestamp": null,
67+
"type": null,
68+
"userId": null,
69+
"appId": null,
70+
"clusterId": null,
71+
"bodySize": 13
72+
},
73+
"redelivered": true,
74+
"data": "SGVsbG8gV29ybGQgMw==" <-- Base64 Encoding of "Hello World 3"
75+
}
76+
]
77+
},
78+
"eventSource": "aws:rmq"
79+
}
80+
81+
*/

0 commit comments

Comments
 (0)