buildKafka = (config: Config, vpc: ec2.IVpc, bastionSg: ec2.SecurityGroup,…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
buildKafka = (config: Config, vpc: ec2.IVpc, bastionSg: ec2.SecurityGroup, cfnClusterId?: string) => {
const mskConfigArn = ssm.StringParameter.valueFromLookup(this, '/msk/configurationArn');
const mskConfigRevisionParameter = new cr.AwsCustomResource(this, 'GetConfigRevision', {
onUpdate: {
service: 'SSM',
action: 'getParameter',
parameters: {
Name: '/msk/configurationRevision',
},
physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()), // Update physical id to always fetch the latest version
},
policy: cr.AwsCustomResourcePolicy.fromSdkCalls({ resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE }),
});
const mskConfigRevision = mskConfigRevisionParameter.getResponseField('Parameter.Value');
const subnets = vpc.selectSubnets({ subnetGroupName: 'private' });
const sg = this.buildSecurityGroup(config, vpc, bastionSg);
const ingressSg = this.buildIngressSecurityGroup(vpc, sg);
this.mskSecurityGroupId = ingressSg.securityGroupId;
const kafka = new msk.CfnCluster(this,
cfnClusterId ?? 'Kafka',
{
clusterName: `${config.Cluster.clusterName}-${config.Stage}`,
brokerNodeGroupInfo:
{
securityGroups: [sg.securityGroupId],
storageInfo: { ebsStorageInfo: { volumeSize: config.Cluster.volumeSize } },
clientSubnets: subnets.subnetIds,
instanceType: config.Cluster.instanceType,
},
numberOfBrokerNodes: config.Cluster.numberOfBrokerNodes,
kafkaVersion: config.Cluster.kafkaVersion,
configurationInfo: {
arn: mskConfigArn,
revision: cdk.Token.asNumber(mskConfigRevision),
},
encryptionInfo: {
//encryptionAtRest: {dataVolumeKmsKeyId: ''},
encryptionInTransit: { clientBroker: 'TLS', inCluster: true },
},
enhancedMonitoring: config.Cluster.enhancedMonitoring,
loggingInfo: {
brokerLogs: {
cloudWatchLogs: {
enabled: true,
logGroup: new logs.LogGroup(this, 'KafkaLogGrp', { retention: logs.RetentionDays.TWO_WEEKS, removalPolicy: cdk.RemovalPolicy.DESTROY }).logGroupName,
},
firehose: { enabled: false },
s3: { enabled: false },
},
},
});
kafka.addPropertyOverride('ClientAuthentication.Sasl', { Scram: { Enabled: true } });
return kafka;
}
can I add somewhere in the code to enable multi-VPC connectivity?
Yes, you can enable multi-VPC connectivity for the Kafka cluster by modifying the brokerNodeGroupInfo object to include the vpcId property and the subnetIds that belong to the other VPC.
Here's an example of how you can modify the brokerNodeGroupInfo object to enable multi-VPC connectivity:
const vpc2 = ec2.Vpc.fromId(this, 'VPC2');
const subnets2 = vpc2.selectSubnets({ subnetGroupName: 'private' });
const sg = this.buildSecurityGroup(config, vpc, bastionSg);
const ingressSg = this.buildIngressSecurityGroup(vpc, sg);
this.mskSecurityGroupId = ingressSg.securityGroupId;
const kafka = new msk.CfnCluster(this,
cfnClusterId ?? 'Kafka',
{
clusterName: `${config.Cluster.clusterName}-${config.Stage}`,
brokerNodeGroupInfo:
{
// ... other broker node group info properties ...
vpcId: vpc.ref,
subnetIds: subnets2.subnetIds,
},
numberOfBrokerNodes: config.Cluster.numberOfBrokerNodes,
// ... other cluster properties ...
});
This will create a Kafka cluster that spans both VPCs and can be accessed from either VPC. Note that you may need to modify the other properties in the brokerNodeGroupInfo object to match the configuration of your other VPC.