Declarative & Automatic using ops.json:
For your service, if you need one or more MSK clusters, you can add ops.json in the root directory of Github repo.
And add MSK clusters as a dependency, like below.
Provisioned cluster
Serverless cluster
-
id- Alphanumeric string. Must be unique amongst the clusters you’ve declared above. Changing this string will replace the original cluster with new one. -
type-provisionedorserverless. Default:provisioned. -
version- Provisioned only, required. A Kafka version MSK supports for new clusters, eg.,3.8.xor3.9.x. Recent MSK versions use thexform - write it exactly like that. Runaws kafka list-kafka-versionsor refer to AWS docs for the list. Older versions may be rejected for new clusters, eg., MSK no longer accepts3.6.0for standard brokers such askafka.t3.small. -
instance_type- Provisioned only. Broker instance type, must start withkafka., eg.,kafka.t3.small,kafka.m5.large. Refer to AWS docs for available sizes. Default:kafka.t3.small. -
broker_count- Provisioned only. Total number of brokers. Brokers are spread evenly over 2 or 3 availability zones, so this must be 2 or more and a multiple of 2 or 3 (2, 3, 4, 6, 8, 9, …). Default:2. Once the cluster exists, its brokers stay in the same availability zones: a newbroker_countmust still spread evenly over them, or the deployment fails with an error. -
storage_gb- Provisioned only. EBS storage per broker, in GB. Between1and16384. Default:20. MSK can only increase storage on an existing cluster, never reduce it. -
parameters- Provisioned only. Optional list of Kafka broker settings (server.properties). Each entry is an object with the following keys:name- Name of the setting (eg.,auto.create.topics.enable). Refer to AWS docs for the settings MSK allows.value- Value to set. Always specified as a string.
parametersis provided, LocalOps creates an MSK configuration for the cluster and attaches it. -
skip_preview- Set totrueto not create this cluster for ephemeral preview services. Default:false. -
preview_only- Set totrueto create this cluster only for ephemeral preview services. Default:false. -
exports- Set of key value pairs. Keys are the ENVIRONMENT VARS we will pass to your code / containers. Values are the properties of the MSK cluster provisioned. See below for available properties.
version, instance_type, broker_count, storage_gb or
parameters on a serverless cluster fails the deployment with an error, instead of being ignored.
Available properties of the MSK cluster to use in exports:
$name- Name of the MSK cluster$arn- Amazon resource name of the MSK cluster$bootstrapBrokers- Comma separated list of bootstrap brokers inhost:portformat. Pass this to your Kafka client as its brokers list.$port- Port the brokers listen on:9096for provisioned,9098for serverless.$region- AWS region of the cluster$saslMechanism-SCRAM-SHA-512for provisioned,OAUTHBEARERfor serverless.$securityProtocol- AlwaysSASL_SSL.$username- Provisioned only. SASL/SCRAM username, derived fromid.$password- Provisioned only. SASL/SCRAM password.$passwordArn- Provisioned only. ARN of the AWS Secrets Manager secret holding the username and password.
Lifecycle:
If you have providedops.json at the root of the git repository, it will be processed if a corresponding service in
any of your active environment points at the same repository as source and when a deployment is triggered.
- When the service is spinned up first time or when a new deployment is triggered,
ops.jsonis parsed for processing. Resources declared in thedependenciesobject will be provisioned before your code starts to run. - Resources with same
idare provisioned only once for the life of the service. And updated when there is a change in one of the properties above, within the limits MSK allows for an existing cluster (seebroker_countandstorage_gbabove). - Keys in
exportsobject will be passed as enviroment variables to your service. - When the service is deleted, the provisioned MSK clusters are deleted from the cloud account.
Authentication:
Both cluster types accept only authenticated, TLS encrypted connections (SASL_SSL). How your code authenticates
depends on the type.
Provisioned: username & password (SASL/SCRAM)
LocalOps generates a username and password for the cluster, stores them in an AWS Secrets Manager secret encrypted with a dedicated KMS key, and associates the secret with the cluster. Export$username and $password and pass them to
your Kafka client with the SCRAM-SHA-512 mechanism.
Serverless: IAM (no keys)
Serverless clusters authenticate with IAM only. You don’t pass any username, password or AWS secret key - your containers authenticate with the IAM role LocalOps attaches to all the services in your environment. We provision a policy like below on that role for every serverless cluster, covering all topics, consumer groups and transactional ids on that cluster.OAUTHBEARER mechanism. Use AWS’s signer library
for your language:
- Node.js -
aws-msk-iam-sasl-signer-js - Python -
aws-msk-iam-sasl-signer-python - Go -
aws-msk-iam-sasl-signer-go - Java -
aws-msk-iam-auth
Some examples online use the mechanism name
AWS_MSK_IAM. Only the Java aws-msk-iam-auth plugin registers that
name, and other clients don’t recognise it. That’s why $saslMechanism exports OAUTHBEARER.Connecting from your code:
Below are Node.js and Java examples, with the exports declared in theops.json examples above. Each works for both
cluster types. Topics are not created automatically on serverless clusters, nor on the provisioned example above
(auto.create.topics.enable is false), so each creates the topic first if it’s missing.
Node.js
Usingkafkajs:
kafka client with a consumer group:
Java
Using the officialkafka-clients library, with AWS’s
aws-msk-iam-auth plugin for serverless clusters. Add both to your
pom.xml:
kafkaProperties() from above and add a consumer group and deserializers:
Private only access:
All MSK clusters are created only in the private subnets of the same VPC where your environment is running. And they are attached with following security group. Ingress:- From source: Your environment’s VPC CIDR IP range
- At port:
9096(provisioned) or9098(serverless) - Protocol:
TCP
broker_count.
Serverless clusters need private subnets in at least 2 availability zones.
Encryption:
- Traffic between your code and the brokers, and between brokers, is encrypted with TLS.
- MSK encrypts data at rest by default.
Ephemeral preview environments:
Every preview service created for pull request previews gets its own cluster, which bills for as long as the preview exists. If that isn’t worth it, setskip_preview: true and point previews at a shared
cluster using regular environment variables.