|
| 1 | +import { Duration, Stack, StackProps, CfnOutput, RemovalPolicy } from 'aws-cdk-lib'; |
| 2 | +import { Construct } from 'constructs'; |
| 3 | +import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager'; |
| 4 | +import * as logs from 'aws-cdk-lib/aws-logs'; |
| 5 | +import { Function, Runtime, Code } from 'aws-cdk-lib/aws-lambda'; |
| 6 | +import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2'; |
| 7 | +import { RabbitMqBrokerEngineVersion, RabbitMqBrokerInstance, RabbitMqEventSource } from '@cdklabs/cdk-amazonmq'; |
| 8 | + |
| 9 | +export class AmazonMqRabbitmqLambdaStack extends Stack { |
| 10 | + constructor(scope: Construct, id: string, props?: StackProps) { |
| 11 | + super(scope, id, props); |
| 12 | + |
| 13 | + // Define the admin secret in AWS Secrets Manager |
| 14 | + const adminSecret = new secretsmanager.Secret(this, 'AdminSecret', { |
| 15 | + secretName: 'AdminCredentials', |
| 16 | + generateSecretString: { |
| 17 | + secretStringTemplate: JSON.stringify({ |
| 18 | + username: 'admin' // Set a default username for RabbitMQ broker |
| 19 | + }), |
| 20 | + generateStringKey: 'password', // Auto-generate password |
| 21 | + excludePunctuation: true, // Avoid punctuation in password |
| 22 | + passwordLength: 12, // Set password length to 12 characters |
| 23 | + }, |
| 24 | + removalPolicy: RemovalPolicy.DESTROY, // Ensure the secret is deleted on stack destroy |
| 25 | + }); |
| 26 | + |
| 27 | + // Create a RabbitMQ broker instance with specified version and instance type |
| 28 | + const broker = new RabbitMqBrokerInstance(this, 'RabbitMqBroker', { |
| 29 | + publiclyAccessible: true, // Publicly accessible RabbitMQ broker |
| 30 | + version: RabbitMqBrokerEngineVersion.V3_13, // Use RabbitMQ version 3.13 |
| 31 | + instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.MICRO), // Instance type T3.micro |
| 32 | + admin: { |
| 33 | + username: adminSecret.secretValueFromJson('username').unsafeUnwrap(), // Use username from Secrets Manager |
| 34 | + password: adminSecret.secretValueFromJson('password'), // Use password from Secrets Manager |
| 35 | + }, |
| 36 | + autoMinorVersionUpgrade: true, // Enable auto minor version upgrades |
| 37 | + }); |
| 38 | + |
| 39 | + // Output the AMQP and Web Console endpoints and ports for the RabbitMQ broker |
| 40 | + new CfnOutput(this, 'AmqpEndpointUrl', { value: broker.endpoints.amqp.url }); |
| 41 | + new CfnOutput(this, 'AmqpEndpointPort', { value: broker.endpoints.amqp.port.toString() }); |
| 42 | + new CfnOutput(this, 'WebConsoleUrl', { value: broker.endpoints.console.url }); |
| 43 | + new CfnOutput(this, 'WebConsolePort', { value: broker.endpoints.console.port.toString() }); |
| 44 | + |
| 45 | + // Create a custom CloudWatch Log Group for the consumer Lambda function |
| 46 | + const consumerLambdaLogGroup = new logs.LogGroup(this, 'ConsumerLambdaLogGroup', { |
| 47 | + logGroupName: 'customLogGroup', // Custom log group name |
| 48 | + removalPolicy: RemovalPolicy.DESTROY, // Ensure it's deleted on stack destroy |
| 49 | + }); |
| 50 | + |
| 51 | + // Define the consumer Lambda function which will handle messages from RabbitMQ |
| 52 | + const consumer_lambda = new Function(this, 'consumer_lambdaFunction', { |
| 53 | + runtime: Runtime.NODEJS_20_X, // Use Node.js 20.x runtime for the Lambda function |
| 54 | + code: Code.fromAsset('lambda'), // Path to Lambda function code directory |
| 55 | + handler: 'consumer.handler', // The entry point (handler) for the Lambda function |
| 56 | + memorySize: 128, // Set memory size to 128 MB |
| 57 | + timeout: Duration.seconds(30), // Set timeout duration to 30 seconds |
| 58 | + logGroup: consumerLambdaLogGroup, // Attach the custom CloudWatch log group |
| 59 | + }); |
| 60 | + |
| 61 | + // Prevent the default log group creation |
| 62 | + consumer_lambda.logGroup.applyRemovalPolicy(RemovalPolicy.DESTROY); |
| 63 | + |
| 64 | + // Add RabbitMQ as an event source for the Lambda function to consume messages from 'testQueue' |
| 65 | + consumer_lambda.addEventSource(new RabbitMqEventSource({ |
| 66 | + broker, // Reference to the RabbitMQ broker instance |
| 67 | + credentials: adminSecret, // Use admin credentials from Secrets Manager |
| 68 | + queueName: 'testQueue', // Queue name in RabbitMQ from which Lambda will consume messages |
| 69 | + })); |
| 70 | + } |
| 71 | +} |
0 commit comments