@cdklabs/cdk-amazonmq

中文文档
0.1.9发布于 2026/03/12Apache-2.0TypeScriptCommonJS零依赖构建来源已验证近 30 天下载 4944
中文使用文档
文档目录23 节
  1. 目录
  2. 导言
  3. 警卫
  4. 活动MQ 经纪人
  5. ActiveMQ 经纪人部署
  6. 活动MQ 中介端点
  7. 允许连接到活动MQ经纪商
  8. 进口活性MQ经纪人
  9. 活动MQ 经纪配置
  10. ActiveMQ 经纪用户管理
  11. 监测活动MQ 经纪人
  12. 与 AWS Lambda 合并
  13. 兔子MQ 经纪人
  14. 兔子MQ 中间商部署
  15. 兔子MQ 中介端点
  16. 允许连接到 RabbitMQ 经纪
  17. 进口快活兔MQ 经纪人
  18. 兔子MQ 经纪用户管理
  19. 兔子MQ 经纪人配置
  20. 监测兔子MQ 经纪人
  21. 与AWS Lambda公司合并
  22. 使用兔子MQ 管理 HTTP API
  23. 外部实例

AWS:: AmazonMQ L2+ 构造库


特征稳定
ActiveMQ经纪商的更高层次构造Experimental
RabbitMQ Bokers的更高级建筑Experimental

实验: 本单元中更高层次的构件是实验和 正在积极发展之中。 它们受到非后向相容性修改或任何 未来版本. 这些限制不受《公约》的约束 语义版本 模型和 断开更改将在发布注释中宣布。 这意味着当你可以使用它们, 您可能需要在升级到此软件包的更新版本时更新源代码.


目录

导言

Amazon MQ是一个管理服务,它使得Apache ActiveMQ和RabbitMQ消息代理商在规模上容易创建和运行. 这个库为 Amazon MQ 带来了 L2 AWS CDK 构造,并引入了一个概念: 经纪人部署 和区别 经纪人 财务报告和审定财务报表 经纪人部署。 。 。 .

  • 经纪人部署 代表定义经纪人(或特定配置中的一组经纪人)将如何部署的配置。 实际上,这是代表 AWS::AmazonMQ::Broker ,并将暴露资源类型的相关属性(如ARN, Id).
  • 经纪人 代表访问经纪人的手段,即其终点和(在ActiveMQ)IPv4地址.

这是因为,在创建这些网络时 AWS::AmazonMQ::Broker activeMQ 中的资源 ACTIVE_STANDBY_MULTI_AZ 部署模式,由此产生的AWS资源实际上将包含一组两个不同的经纪人.

分离可以以两种方式表示资源类型:

  • 实值时 经纪人部署 执行 经纪人 行为界面
  • 已经时 经纪人部署 包含(一组) 经纪人。 。 。 .

警卫

为了建立安全的解决办法,遵循《公约》的准则和建议 警卫 亚马逊MQ的 AWS 文件部分.

活动MQ 经纪人

Amazon MQ允许创建AWS管理的ActiveMQ经纪人. 经纪人可以交换信息 一些协议例如:AMQP 1.0,OpenWire,STOMP,MQTT.

ActiveMQ 经纪人部署

下面的例子创造了一个最低限度, 单声道活动MQ 中间商部署数字 :

import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { 
  ActiveMqBrokerEngineVersion, 
  ActiveMqBrokerInstance,
  ActiveMqBrokerUserManagement,
} from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const brokerUser: ISecret;

const broker = new ActiveMqBrokerInstance(stack, 'ActiveMqBroker', {
  publiclyAccessible: false,
  version: ActiveMqBrokerEngineVersion.V5_18,
  instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.MICRO),
  userManagement: ActiveMqBrokerUserManagement.simple({
    users: [{
      username: brokerUser.secretValueFromJson('username').unsafeUnwrap(),
      password: brokerUser.secretValueFromJson('password'),
    }],
  }),
});

下面的例子说明如何对一对活动-备用的冗余配对进行即时处理. ActiveMqBrokerRedundantPair 不执行 IActiveMqBroker,但有两个属性: first,以及 second 就是这样 这是因为 ActiveMq 冗余派部署 曝光两个,分别的经纪人,在主动站立配置中工作. 他们的名字是 first (代替 active和 second (代替 standby由于无法保证哪家经纪人是 active 以及哪个 - 如何 standby。 。 。 .

import { 
  InstanceClass,
  InstanceSize,
  InstanceType,
  IVpc,
  SubnetSelection
} from 'aws-cdk-lib/aws-ec2';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { 
  ActiveMqBrokerEngineVersion,
  ActiveMqBrokerRedundantPair,
  ActiveMqBrokerUserManagement,
} from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const brokerUser: ISecret;
declare const vpc: IVpc;
declare const vpcSubnets: SubnetSelection;

const brokerPair = new ActiveMqBrokerRedundantPair(stack, 'ActiveMqBrokerPair', {
  publiclyAccessible: false,
  version: ActiveMqBrokerEngineVersion.V5_18,
  instanceType: InstanceType.of(InstanceClass.M5, InstanceSize.LARGE),
  userManagement: ActiveMqBrokerUserManagement.simple({
    users: [{
      username: brokerUser.secretValueFromJson('username').unsafeUnwrap(),
      password: brokerUser.secretValueFromJson('password'),
    }],
  }),
  vpc,
  vpcSubnets,
});

活动MQ 中介端点

每个创建的经纪人实例工具 IActiveMqBroker 并且已经 endpoints 以 url 和 port 代表每个属性.

可以使用以下例子中的终点

import { CfnOutput } from 'aws-cdk-lib';
import { IActiveMqBroker } from '@cdklabs/cdk-amazonmq';

declare const broker: IActiveMqBroker;

new CfnOutput(this, 'AmqpEndpointUrl', { value:  broker.endpoints.amqp.url });
new CfnOutput(this, 'AmqpEndpointPort', { value: broker.endpoints.amqp.port.toString() });

new CfnOutput(this, 'StompEndpointUrl', { value:  broker.endpoints.stomp.url });
new CfnOutput(this, 'StompEndpointPort', { value: broker.endpoints.stomp.port.toString() });

new CfnOutput(this, 'OpenWireEndpointUrl', { value:  broker.endpoints.openWire.url });
new CfnOutput(this, 'OpenWireEndpointPort', { value: broker.endpoints.openWire.port.toString() });

new CfnOutput(this, 'MqttEndpointUrl', { value:  broker.endpoints.mqtt.url });
new CfnOutput(this, 'MqttEndpointPort', { value: broker.endpoints.mqtt.port.toString() });

new CfnOutput(this, 'WssEndpointUrl', { value:  broker.endpoints.wss.url });
new CfnOutput(this, 'WssEndpointPort', { value: broker.endpoints.wss.port.toString() });

new CfnOutput(this, 'WebConsoleUrl', { value: broker.endpoints.console.url });
new CfnOutput(this, 'WebConsolePort', { value: broker.endpoints.console.port.toString() });

new CfnOutput(this, 'IpAddress', { value: broker.ipAddress });

对于冗余的配对部署,可以访问属性下的所有端点 first 财务报告和审定财务报表 second,作为每个工具 IActiveMqBroker。 。 。 .

允许连接到活动MQ经纪商

对于无法公开访问且带有特定 VPC 和子网的 ActiveMQ 经纪人部署, 您可以控制谁可以使用 Broker 访问 connections 属性。 默认情况下,不允许连接,必须明确允许连接.

import { Peer, Port } from 'aws-cdk-lib/aws-ec2';
import {
  IActiveMqBroker,
  IActiveMqBrokerDeployment,
} from '@cdklabs/cdk-amazonmq';

declare const deployment: IActiveMqBrokerDeployment;
declare const broker: IActiveMqBroker;

// for the applications to interact over the STOMP protocol
deployment.connections?.allowFrom(Peer.ipv4('1.2.3.4/8'), Port.tcp(broker.endpoints.stomp.port));

// for the applications to interact over the OpenWire protocol
deployment.connections?.allowFrom(Peer.ipv4('1.2.3.4/8'), Port.tcp(broker.endpoints.openWire.port));

// for the Web Console access
deployment.connections?.allowFrom(Peer.ipv4('1.2.3.4/8'), Port.tcp(broker.endpoints.console.port));

小心点 connections 将仅在指定 VPC 和子网时定义。 例如 ActiveMqBrokerRedundantPair 一个可以进入经纪人的终点 first 或者说 second 属性.

警卫: 这是安全最佳做法 封锁与VPC安全小组的不必要的协议。 。 。 .

进口活性MQ经纪人

导入已有的 ActiveMqBrokerInstance 使用情况 .fromActiveMqBrokerInstanceArn() 或者说 .fromActiveMqBrokerInstanceNameAndId() 方法。 。 .

import { ActiveMqBrokerInstance } from '@cdklabs/cdk-amazonmq';

const broker = ActiveMqBrokerInstance.fromActiveMqBrokerInstanceArn(
  this,
  "Imported",
  "arn:aws:mq:us-east-2:123456789012:broker:TestBroker:b-123456789012-123456789012"
);

如果你想使用的话 .connections 还可以通过 securityGroups数字 :

import { SecurityGroup } from 'aws-cdk-lib/aws-ec2';
import { ActiveMqBrokerInstance } from '@cdklabs/cdk-amazonmq';

const sgs = [
  SecurityGroup.fromSecurityGroupId(this, "ImportedSG", "sg-123123123123"),
];

const broker = ActiveMqBrokerInstance.fromActiveMqBrokerInstanceNameAndId(
  this,
  "Imported",
  "TestBroker",
  "b-123456789012-123456789012",
  sgs,
);

同样, ActiveMqBrokerRedundantPair 可用于导入 .fromActiveMqRedundantPairArn() 财务报告和审定财务报表 .fromActiveMqRedundantPairNameAndId() 方法。 。 .

活动MQ 经纪配置

默认情况下, Amazon MQ 会为您部署的经纪人创建默认配置 。 您可以通过在下面的例子中明确创建自定义配置来引入:

import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { 
  ActiveMqBrokerConfiguration, 
  ActiveMqBrokerConfigurationDefinition, 
  ActiveMqAuthenticationStrategy,
  ActiveMqBrokerEngineVersion,
  ActiveMqBrokerInstance,
  ActiveMqBrokerUserManagement,
} from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const brokerUser: ISecret;
declare const configurationData: string;

const customConfiguration = new ActiveMqBrokerConfiguration(stack, 'CustomConfiguration', {
  configurationName: 'ConfigurationName',
  description: 'ConfigurationDescription',
  engineVersion: ActiveMqBrokerEngineVersion.V5_18,
  authenticationStrategy: ActiveMqAuthenticationStrategy.SIMPLE,
  definition: ActiveMqBrokerConfigurationDefinition.data(configurationData),
});

const broker = new ActiveMqBrokerInstance(stack, 'Broker', {
  publiclyAccessible: false,
  version: ActiveMqBrokerEngineVersion.V5_18,
  instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.MICRO),
  userManagement: ActiveMqBrokerUserManagement.simple({
    users: [{
      username: brokerUser.secretValueFromJson('username').unsafeUnwrap(),
      password: brokerUser.secretValueFromJson('password'),
    }],
  }),
  configuration: customConfiguration
});

配置也可以在经纪人创建后与特定经纪人相联. 然后,要求它与经纪人明确联系.

import { 
  IActiveMqBrokerConfiguration,
  IActiveMqBrokerDeployment,
} from '@cdklabs/cdk-amazonmq';

declare const configuration: IActiveMqBrokerConfiguration;
declare const deployment: IActiveMqBrokerDeployment;

configuration.associateWith(deployment);

⚠️ 折旧: 这个 createRevision() 由于自定义资源的限制,方法被贬值。 相反,创建一个新的 ActiveMqBrokerConfiguration 把它和你的经纪人联系起来 AWS 现在支持 删除配置 API,使得无需修改即可管理配置更为简单.

遗留: 创建配置修改( 已过期)

此库还允许修改已有的配置 。 这种对特定配置的更新是: 创建新配置 订正 以便可以在 AWS 控制台中查看修改的历史。 新的修改可以和经纪人联系起来,这样它就把它作为工作配置.

import { 
  ActiveMqBrokerConfigurationDefinition,
  IActiveMqBrokerConfiguration,
  IActiveMqBrokerDeployment,
} from '@cdklabs/cdk-amazonmq';

declare const configuration: IActiveMqBrokerConfiguration;
declare const deployment: IActiveMqBrokerDeployment;
declare const newData: string;

const newRevision = configuration.createRevision({
  description: 'We need to modify an AuthorizationEntry',
  definition: ActiveMqBrokerConfigurationDefinition.data(newData)
});

newRevision.associateWith(deployment);

ActiveMQ 经纪用户管理

ActiveMQ 经纪人简单认证

使用 ActiveMQ 内置 简单认证 在经纪人部署定义中需要提供用户.

警卫: 在简单认证用户管理权限中,由配置管理. 这是安全方面的最佳做法 总是配置授权地图。 。 。 .

ActiveMQ 经纪人 LDAP 集成

Amazon MQ for ActiveMQ 启用了 LDAP 集成. 下面的一个例子显示了一个最小的设置,用于配置一个Amazon MQ用于ActiveMQ经纪人.

import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { 
  ActiveMqBrokerEngineVersion, 
  ActiveMqBrokerInstance,
  ActiveMqBrokerUserManagement,
} from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const serviceAccountSecret: ISecret;

const broker = new ActiveMqBrokerInstance(stack, 'ActiveMqBrokerInstance', {
  publiclyAccessible: false,
  version: ActiveMqBrokerEngineVersion.V5_18,
  instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.MICRO),
  userManagement: ActiveMqBrokerUserManagement.ldap({
    hosts: ['ldap.example.com'],
    userSearchMatching: 'uid={0}',
    userRoleName: 'amq',
    userBase: 'ou=users,dc=example,dc=com',
    roleBase: 'ou=roles,dc=example,dc=com',
    roleSearchMatching: 'cn={0}',
    roleName: 'amq',
    serviceAccountPassword: serviceAccountSecret.secretValueFromJson('password'),
    serviceAccountUsername: serviceAccountSecret.secretValueFromJson('username'),
  }),
});

监测活动MQ 经纪人

这个图书馆介绍 一套衡量标准 我们可以用来 IActiveMqBrokerDeployment 监测。 每一个都可以作为方法访问 IActiveMqBrokerDeployment 与《公约》有关的 metric[MetricName]。下面的一个例子说明人们如何使用:

import { IActiveMqBrokerDeployment } from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const deployment: IActiveMqBrokerDeployment;

const consumerCountMetric = deployment.metricConsumerCount();
consumerCountMetric.createAlarm(stack, 'ConsumerCountAlarm', {
  threshold: 100,
  evaluationPeriods: 3,
  datapointsToAlarm: 2,
});

与 AWS Lambda 合并

Amazon MQ用于 ActiveMQ 经纪人队列可以用作 AWS Lambda 函数的事件源. 对于认证,仅支持 ActiveMQ 简单认证程序。 Lambda使用OpenWire/Java Message Service(JMS)协议来消耗消息. 消费消息不支持其他协议 。 在JMS协议中,仅支持文本Message和字节Message. Lambda也支持JMS自定义属性. 将关于一体化要求的更详细资料改为: 文件。 。 。 .

以下的例子为创建这样的事件源映射的例子:

import { IFunction } from 'aws-cdk-lib/aws-lambda';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { 
  ActiveMqEventSource,
  IActiveMqBrokerDeployment,
} from '@cdklabs/cdk-amazonmq';

declare const target: IFunction;
declare const creds: ISecret; // with username and password fields
declare const broker: IActiveMqBrokerDeployment;
declare const queueName: string;

target.addEventSource(new ActiveMqEventSource({
  broker,
  credentials: creds,
  queueName,
}));

警卫: 当将 AWS Lambda 函数的事件源添加到 Amazon MQ for ActiveMQ 时, 库更新执行角色的权限以满足 提供事件源映射的 Amazon MQ 要求。 。 。 .

在私人部署的情况下,定义的事件源映射将在经纪人部署创建通信端点的子网中创建一套弹性网络接口。 因此,为了使事件源图能够与经纪人进行交流,需要额外允许从OpenWire端口的ENI进入。 由于 ENI 将使用同样的安全组来规范对经纪人端点的访问, 您可以简单地允许经纪人的安全组在 OpenWire 端口上进行通信 :

import { Port } from 'aws-cdk-lib/aws-ec2';
import { 
  IActiveMqBroker,
  IActiveMqBrokerDeployment,
} from '@cdklabs/cdk-amazonmq';

declare const deployment: IActiveMqBrokerDeployment;
declare const broker: IActiveMqBroker;

deployment.connections?.allowInternally(Port.tcp(broker.endpoints.openWire.port), 'Allowing for the ESM');

兔子MQ 经纪人

Amazon MQ允许创建由AWS管理的RabbitMQ经纪人. 这些经纪人通过AMQP 0-9-1协议交换信息.

兔子MQ 中间商部署

下面的例子创造了一个最低限度的单一事件RabbitMQ经纪人部署:

import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { 
  RabbitMqBrokerEngineVersion, 
  RabbitMqBrokerInstance,
  RabbitMqBrokerUserManagement,
} from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const adminSecret: ISecret;

const broker = new RabbitMqBrokerInstance(stack, 'RabbitMqBroker', {
  publiclyAccessible: false,
  version: RabbitMqBrokerEngineVersion.V3_13,
  instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.MICRO),
  userManagement: RabbitMqBrokerUserManagement.simple({
    admin: {
      username: adminSecret.secretValueFromJson('username').unsafeUnwrap(),
      password: adminSecret.secretValueFromJson('password'),
    },
  }),
});

下一例创建了最小的 RabbitMQ 经纪人集群:

import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import {
  RabbitMqBrokerCluster,
  RabbitMqBrokerEngineVersion,
  RabbitMqBrokerUserManagement,
} from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const adminSecret: ISecret;

const broker = new RabbitMqBrokerCluster(stack, 'RabbitMqBroker', {
  publiclyAccessible: false,
  version: RabbitMqBrokerEngineVersion.V3_13,
  instanceType: InstanceType.of(InstanceClass.M5, InstanceSize.LARGE),
  userManagement: RabbitMqBrokerUserManagement.simple({
    admin: {
      username: adminSecret.secretValueFromJson('username').unsafeUnwrap(),
      password: adminSecret.secretValueFromJson('password'),
    },
  }),
});

兔子MQ 中介端点

每个创建的经纪人 endpoints 带有 AMQP 端点url 和端口的属性.

import { CfnOutput } from 'aws-cdk-lib';
import { IRabbitMqBroker } from '@cdklabs/cdk-amazonmq';

declare const broker: IRabbitMqBroker;

new CfnOutput(this, 'AmqpEndpointUrl', { value:  broker.endpoints.amqp.url });
new CfnOutput(this, 'AmqpEndpointPort', { value: broker.endpoints.amqp.port.toString() });
new CfnOutput(this, 'WebConsoleUrl', { value: broker.endpoints.console.url });
new CfnOutput(this, 'WebConsolePort', { value: broker.endpoints.console.port.toString() });

允许连接到 RabbitMQ 经纪

对于无法公开访问的RabbitMQ经纪人部署以及特定的VPC和子网,您可以控制谁可以使用 connections 属性.

import { Peer, Port } from 'aws-cdk-lib/aws-ec2';
import { IRabbitMqBroker, IRabbitMqBrokerDeployment } from '@cdklabs/cdk-amazonmq';

declare const deployment: IRabbitMqBrokerDeployment;
declare const broker: IRabbitMqBroker;

// for the applications to interact over the AMQP protocol
deployment.connections?.allowFrom(Peer.ipv4('1.2.3.4/8'), Port.tcp(broker.endpoints.amqp.port));

// for the Web Console access
deployment.connections?.allowFrom(Peer.ipv4('1.2.3.4/8'), Port.tcp(broker.endpoints.console.port));

小心点 connections 将仅在指定 VPC 和子网时定义.

进口快活兔MQ 经纪人

导入已有的 RabbitMqBrokerInstance 使用情况 .fromRabbitMqBrokerInstanceArn() 或者说 .fromRabbitMqBrokerInstanceNameAndId() 方法。 。 .

import { RabbitMqBrokerInstance } from '@cdklabs/cdk-amazonmq';

const broker = RabbitMqBrokerInstance.fromRabbitMqBrokerInstanceArn(
  this,
  "Imported",
  "arn:aws:mq:us-east-2:123456789012:broker:TestBroker:b-123456789012-123456789012"
);

如果你想使用的话 .connections 还可以通过 securityGroups数字 :

import { SecurityGroup } from 'aws-cdk-lib/aws-ec2';
import { RabbitMqBrokerInstance } from '@cdklabs/cdk-amazonmq';

const sgs = [
  SecurityGroup.fromSecurityGroupId(this, "ImportedSG", "sg-123123123123"),
];

const broker = RabbitMqBrokerInstance.fromRabbitMqBrokerInstanceNameAndId(
  this,
  "Imported",
  "TestBroker",
  "b-123456789012-123456789012",
  sgs,
);

同样, RabbitMqBrokerCluster 可用于导入 .fromRabbitMqClusterArn() 财务报告和审定财务报表 .fromRabbitMqClusterNameAndId() 方法。 。 .

进口双层兔子MQ 经纪人

从2025年4月起 Amazon MQ 支持兔子MQ 对经纪人使用双层端点。通过此修改, URL域名后缀将更改, 并使用 AWS::URLSuffix 伪参数 只为IPv4经纪人正确工作. 这就是为什么 .fromXXX 方法包含额外的 urlSuffix 用于指定 URL 域后缀的方法参数。 如果没有提供参数 - 使用 endpoints.amqp.url 财务报告和审定财务报表 endpoints.console.url 将投出一个错误.

下面的例子说明了访问AMQPS URL会出错的情况.

import { CfnOutput } from 'aws-cdk-lib';
import { SecurityGroup } from 'aws-cdk-lib/aws-ec2';
import { RabbitMqBrokerInstance } from '@cdklabs/cdk-amazonmq';

const sgs = [
  SecurityGroup.fromSecurityGroupId(this, "ImportedSG", "sg-123123123123"),
];

const broker = RabbitMqBrokerInstance.fromRabbitMqBrokerInstanceNameAndId(
  this,
  "Imported",
  "TestBroker",
  "b-123456789012-123456789012",
  sgs,
);

...

new CfnOutput(this, 'AmqpUrl', {
  value: broker.endpoints.amqp.url // <- throws 
});

以下例子如下: urlSuffix 参数通过 - url 属性可以访问.

import { CfnOutput } from 'aws-cdk-lib';
import { SecurityGroup } from 'aws-cdk-lib/aws-ec2';
import { RabbitMqBrokerInstance } from '@cdklabs/cdk-amazonmq';

const sgs = [
  SecurityGroup.fromSecurityGroupId(this, "ImportedSG", "sg-123123123123"),
];

declare const urlSuffix: string;

const broker = RabbitMqBrokerInstance.fromRabbitMqBrokerInstanceNameAndId(
  this,
  "Imported",
  "TestBroker",
  "b-123456789012-123456789012",
  sgs,
  urlSuffix
);

...

new CfnOutput(this, 'AmqpUrl', {
  value: broker.endpoints.amqp.url // <- does not throw
});

兔子MQ 经纪用户管理

兔子MQ 中间商简单认证

RabbitMQ经纪人支持使用RabbitMQ管理插件进行简单的认证. 采用这种方法,用户直接通过RabbitMQ管理插件进行管理,并在经纪人部署时指定身份证明.

说明: 虽然 admin 属性仍然被支持为后向相容性,被贬值并抑制。 使用 userManagement 属性为 RabbitMqBrokerUserManagement.simple() 换句话说.

import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { 
  RabbitMqBrokerEngineVersion,
  RabbitMqBrokerInstance,
  RabbitMqBrokerUserManagement,
} from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const adminSecret: ISecret;

const broker = new RabbitMqBrokerInstance(stack, 'RabbitMqBroker', {
  publiclyAccessible: false,
  version: RabbitMqBrokerEngineVersion.V3_13,
  instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.MICRO),
  userManagement: RabbitMqBrokerUserManagement.simple({
    admin: {
      username: adminSecret.secretValueFromJson('username').unsafeUnwrap(),
      password: adminSecret.secretValueFromJson('password'),
    },
  }),
});

兔子MQ 经纪人配置管理认证

Amazon MQ for RabbitMQ也支持由配置管理认证,这使得外部用户管理系统可以处理认证,而不是使用RabitMQ的管理插件. 这一办法为与现有认证基础设施相融合提供了更大的灵活性.

import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2';
import { 
  RabbitMqBrokerEngineVersion,
  RabbitMqBrokerInstance,
  RabbitMqBrokerUserManagement,
} from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;

const broker = new RabbitMqBrokerInstance(stack, 'RabbitMqBroker', {
  publiclyAccessible: false,
  version: RabbitMqBrokerEngineVersion.V3_13,
  instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.MICRO),
  userManagement: RabbitMqBrokerUserManagement.configManaged(),
});

有配置管理的认证 :

  • 经纪人管理插件数据库中没有创建用户
  • 通过外部配置机制处理认证
  • 目前支持 OAuth 认证和授权
  • 在经纪人部署之外需要增加配置

兔子MQ 经纪人配置

如果您不指定自定义的 RabbitMQ 经纪配置, Amazon MQ for RabbitMQ 将代表您为经纪人创建默认配置 。 您可以通过在下面的例子中明确创建自定义配置来引入:

import { Duration } from 'aws-cdk-lib';
import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { 
  RabbitMqBrokerConfiguration, 
  RabbitMqBrokerConfigurationDefinition,
  RabbitMqBrokerEngineVersion,
  RabbitMqBrokerInstance,
  RabbitMqBrokerUserManagement,
} from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const adminSecret: ISecret;

const customConfiguration = new RabbitMqBrokerConfiguration(stack, 'CustomConfiguration', {
  configurationName: 'ConfigurationName',
  description: 'ConfigurationDescription',
  engineVersion: RabbitMqBrokerEngineVersion.V3_13,
  definition: RabbitMqBrokerConfigurationDefinition.parameters({
    consumerTimeout: Duration.minutes(20),
  }),
});

const broker = new RabbitMqBrokerInstance(stack, 'Broker', {
  publiclyAccessible: false,
  version: RabbitMqBrokerEngineVersion.V3_13,
  instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.MICRO),
  userManagement: RabbitMqBrokerUserManagement.simple({
    admin: {
      username: adminSecret.secretValueFromJson('username').unsafeUnwrap(),
      password: adminSecret.secretValueFromJson('password'),
    },
  }),
  configuration: customConfiguration
});

配置也可以在部署后与特定经纪人相联. 然后,要求它与经纪人明确联系.

import { 
  IRabbitMqBrokerConfiguration, 
  IRabbitMqBrokerDeployment,
} from '@cdklabs/cdk-amazonmq';

declare const configuration: IRabbitMqBrokerConfiguration;
declare const deployment: IRabbitMqBrokerDeployment;

configuration.associateWith(deployment);

⚠️ 折旧: 这个 createRevision() 由于自定义资源的限制,方法被贬值。 相反,创建一个新的 RabbitMqBrokerConfiguration 把它和你的经纪人联系起来 AWS 现在支持 删除配置 API,使得无需修改即可管理配置更为简单.

遗留: 创建配置修改( 已过期)

此库还允许修改已有的配置 。 这种对特定配置的更新是: 创建新配置 订正 以便可以在 AWS 控制台中查看修改的历史。 新的修改可以和经纪人联系起来,这样它就把它作为工作配置.

import { Duration } from 'aws-cdk-lib';
import { 
  IRabbitMqBrokerConfiguration, 
  IRabbitMqBrokerDeployment,
  RabbitMqBrokerConfigurationDefinition,
} from '@cdklabs/cdk-amazonmq';

declare const configuration: IRabbitMqBrokerConfiguration;
declare const deployment: IRabbitMqBrokerDeployment;
declare const newConsumerTimeout: Duration;

const newRevision = configuration.createRevision({
  description: 'We need to modify the consumer timeout',
  definition: RabbitMqBrokerConfigurationDefinition.parameters({
    consumerTimeout: newConsumerTimeout
  })
});

newRevision.associateWith(deployment);

监测兔子MQ 经纪人

这个图书馆介绍 一套衡量标准 我们可以用来 IRabbitMqBrokerDeployment 监测。 每一个都可以作为方法访问 IRabbitMqBrokerDeployment 与《公约》有关的 metric[MetricName]。下面的一个例子说明人们如何使用:

import { IRabbitMqBrokerDeployment } from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const deployment: IRabbitMqBrokerDeployment;

const consumerCountMetric = deployment.metricConsumerCount();
consumerCountMetric.createAlarm(stack, 'ConsumerCountAlarm', {
  threshold: 100,
  evaluationPeriods: 3,
  datapointsToAlarm: 2,
});

与AWS Lambda公司合并

Amazon MQ for RabbitMQ 经纪队列可以用作AWS Lambda函数的事件源. 只支持PLAIN认证机制。 Lambda使用AMQP 0-9-1协议来消耗消息. 消费消息不支持其他协议 。 将关于一体化要求的更详细资料改为: 文件。 。 。 .

以下的例子为创建这样的事件源映射的例子:

import { IFunction } from 'aws-cdk-lib/aws-lambda';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { IRabbitMqBrokerDeployment, RabbitMqEventSource } from '@cdklabs/cdk-amazonmq';

declare const target: IFunction;
declare const creds: ISecret; // with username and password fields
declare const broker: IRabbitMqBrokerDeployment;
declare const queueName: string;

target.addEventSource(new RabbitMqEventSource({
  broker,
  credentials: creds,
  queueName,
}));

警卫: 当为 RabbitMQ 添加一个 Amazon MQ 作为 AWS Lambda 函数的事件源时, 库更新执行角色的权限以满足 提供事件源映射的 Amazon MQ 要求。 。 。 .

在私人部署的情况下,定义的事件源映射将在经纪人部署创建通信VPC端点的子网中创建一套弹性网络接口(ENIs). 因此,为了让事件源映射能够与broekr通信,需要额外允许来自ENI的入境流量. 由于 ENI 将使用同样的安全组来规范对 VPC 端点的访问, 您可以简单地允许经纪人的安全组在 AMQP 端口上进行通信 :

import { IRabbitMqBrokerDeployment } from '@cdklabs/cdk-amazonmq';

declare const deployment: IRabbitMqBrokerDeployment;

deployment.connections?.allowDefaultPortInternally();

使用兔子MQ 管理 HTTP API

这个库允许通过使用 RabbitMQ 管理 HTTP API 与 RabbitMQ 经纪人的 Amazon MQ 交互 RabbitMqCustomResource。这一资源遵循用户经验 AwsCustomResource 并得到了一个 SingletonFunction。自定义资源每个组合创建这样的单通函数 broker, (中文(简体) ), credentials, (中文(简体) ), vpc, (中文(简体) ), vpcSubnets,以及 securityGroups允许限制资源数量,但限制每个许可的范围(考虑到 broker 财务报告和审定财务报表 credentials)和连通性(通过 vpc, (中文(简体) ), vpcSubnets,以及 securityGroups) (中文(简体) ).

实例 RabbitMqCustomResource 现说明如下:

import { PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { LogGroup, RetentionDays } from 'aws-cdk-lib/aws-logs';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { PhysicalResourceId } from 'aws-cdk-lib/custom-resources';
import { HttpMethods, IRabbitMqBroker, RabbitMqCustomResource, RabbitMqCustomResourcePolicy } from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const username: string;
declare const userCreds: ISecret; // with username/password fields
declare const broker: IRabbitMqBroker;
declare const brokerAdminCreds: ISecret; // with username/password fields of the broker admin

const user = new RabbitMqCustomResource(stack, 'CreateUser', {
  broker,
  credentials: brokerAdminCreds,
  logGroup: new LogGroup(stack, 'RmqCustomResourceLogGroup', {
    retention: RetentionDays.ONE_DAY,
  }),
  onUpdate: {
    path: `/api/users/${userCreds.secretValueFromJson('username')}`,
    method: HttpMethods.PUT,
    payload: {
      password: userCreds.secretValueFromJson('password'),
      tags: '',
    },
    physicalResourceId: PhysicalResourceId.of(`${username}-create`),
  },
  onDelete: {
    path: `/api/users/${userCreds.secretValueFromJson('username')}`,
    method: HttpMethods.DELETE,
  },
  policy: RabbitMqCustomResourcePolicy.fromStatements([
    new PolicyStatement({
      actions: ['secretsmanager:GetSecretValue'],
      resources: [userCreds.secretArn],
    }),
  ]),
});

上述示例约束了兔子MQ用户的创建,更新和删除. 行为 onCreate 财务报告和审定财务报表 onUpdate 会 议 日 程 和 议 程 RabbitMqCustomResource 遵循 AwsCustomResource 如果不存在 onCreate,且仅限 onUpdate - 用于: onCreate 财务报告和审定财务报表 onUpdate。 。 。 .

此外, RabbitMqCustomResource 可以读取来自SecretManager Secrets的信息,它允许设置用户的密码而不暴露它. 由于这需要在秘密本身上读取权限 RabbitMqCustomResourcePolicy。 。 。 .

RabbitMqCustomResource 也复制命令中输出的格式,复制行为 AwsCustomResource。这意味着输出被平整,并回收任何字段 RabbitMqCustomResource 实例,需要应用平整路径。 下面的例子说明如何检索经纪人节点的名称 RabbitMqBrokerInstance数字 :


import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { RabbitMqBrokerInstance, RabbitMqCustomResource } from '@cdklabs/cdk-amazonmq';

declare const stack: Stack;
declare const broker: RabbitMqBrokerInstance;
declare const credentials: ISecret;

const getNodesName = new RabbitMqCustomResource(this, "GetNodes", {
  broker,
  credentials,
  onCreate: {
    path: '/api/nodes',
  },
});

// accessing the field returned by the call
getNodesName.getResponseField('0.name')

举例来说,该呼吁是针对 /api/nodes 端点是JSON对象阵列. 给 RabbitMqBrokerInstance 将有一个物体,而 RabbitMqBrokerCluster 每个节点将有三个对象显示信息。 通过使用对象位置的索引来平分阵列,这就是为什么通过指定响应字段名称来取出第一个(且仅在示例中)节点的名称 0.name。 。 。 .

外部实例

本节包含更多实例并使用与RabbitMQ经纪公司合作的案例,使用AWS:AmazonMQ L2+ Construction Library.

  • 兔MQ Lambda 集成的 AWS CDK 示例数字 : 一个实例: aws-samples/aws-cdk-样本 ,演示如何使用此库将RabbitMQ Brokers与AWS Lambda整合. 这个例子也与AWS保密管理器整合,用于安全的证书管理,并建立了一个云表日志组来进行记录.

版本与发布记录

@cdklabs/cdk-amazonmq 共发布 12 个版本,首次发布于 2024/08/07;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.9latest2026/03/127 个月前
0.1.82025/10/151.1 年前
0.1.72025/09/301.1 年前
0.1.62025/05/271.5 年前
0.1.52025/05/211.5 年前
0.1.42025/03/111.7 年前

关于 @cdklabs/cdk-amazonmq 的常见问题

如何安装 @cdklabs/cdk-amazonmq?

在项目目录运行 npm install @cdklabs/cdk-amazonmq,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:aws-cdk-lib(^2.240.0)、constructs(^10.5.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cdklabs/cdk-amazonmq 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cdklabs/cdk-amazonmq 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cdklabs/cdk-amazonmq 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 18.12.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cdklabs/cdk-amazonmq 可以商用吗?

@cdklabs/cdk-amazonmq 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@cdklabs/cdk-amazonmq 的下载量有多少?

根据 npm 官方下载统计,@cdklabs/cdk-amazonmq 在 2026/10/10 – 2026/11/08 这 30 天内被下载 4,944 次,日均约 165 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cdklabs/cdk-amazonmq 还在维护吗?

最新版本 0.1.9 发布于 2026/03/12(7 个月前);至今共发布 12 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/08/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。