@@ -27,6 +27,10 @@ variable "elasticache_subnets" {
2727 default = ["$CIDR_ELASTICACHE_A", "$CIDR_ELASTICACHE_B", "$CIDR_ELASTICACHE_C"]
2828}
2929
30+ variable "msk_subnets" {
31+ default = ["$CIDR_MSK_A", "$CIDR_MSK_B", "$CIDR_MSK_C"]
32+ }
33+
3034terraform {
3135 required_providers {
3236 aws = {
@@ -101,6 +105,20 @@ module "vpc" {
101105 }
102106}
103107
108+ # MSK Subnets =========================================
109+
110+ resource "aws_subnet" "msk_subnets" {
111+ count = length(var.msk_subnets)
112+
113+ vpc_id = module.vpc.vpc_id
114+ cidr_block = var.msk_subnets[count.index]
115+ availability_zone = var.zones[count.index]
116+
117+ tags = {
118+ Name = "${NAME}-msk-${count.index + 1}"
119+ "kubernetes.io/cluster/${NAME}" = "shared"
120+ }
121+ }
104122
105123# Kubernetes cluster EKS =========================================
106124
@@ -477,6 +495,74 @@ output "elasticache_cluster_name" {
477495}
478496
479497
498+ # MSK Kafka Cluster =========================================
499+
500+ locals {
501+ msk_cluster_id = "rasa-${NAME}-kafka"
502+ }
503+
504+ module "msk_security_group" {
505+ source = "terraform-aws-modules/security-group/aws"
506+ version = "~> 5.3"
507+
508+ name = "rasa-${NAME}-msk"
509+ description = "Security group for MSK ${NAME}"
510+ vpc_id = module.vpc.vpc_id
511+
512+ ingress_cidr_blocks = module.vpc.private_subnets_cidr_blocks
513+ ingress_rules = ["kafka-broker-tcp", "kafka-broker-tls-tcp"]
514+
515+ egress_cidr_blocks = [module.vpc.vpc_cidr_block]
516+ egress_rules = ["all-all"]
517+
518+ tags = {
519+ Name = "rasa-${NAME}-msk"
520+ }
521+ }
522+
523+ resource "aws_msk_cluster" "kafka_cluster" {
524+ cluster_name = local.msk_cluster_id
525+ kafka_version = "3.8.x"
526+ number_of_broker_nodes = 3
527+
528+ broker_node_group_info {
529+ instance_type = "kafka.m5.large"
530+ client_subnets = aws_subnet.msk_subnets[*].id
531+ security_groups = [module.msk_security_group.security_group_id]
532+
533+ storage_info {
534+ ebs_storage_info {
535+ volume_size = 100
536+ }
537+ }
538+ }
539+
540+ client_authentication {
541+ sasl {
542+ iam = true
543+ }
544+ }
545+
546+ encryption_info {
547+ encryption_in_transit {
548+ client_broker = "TLS"
549+ in_cluster = true
550+ }
551+ }
552+
553+ tags = {
554+ Name = local.msk_cluster_id
555+ }
556+ }
557+
558+ output "msk_cluster_arn" {
559+ value = aws_msk_cluster.kafka_cluster.arn
560+ }
561+
562+ output "msk_bootstrap_brokers" {
563+ value = aws_msk_cluster.kafka_cluster.bootstrap_brokers_sasl_iam
564+ }
565+
480566# DNS Identity =========================================
481567
482568module "iam_role_dns" {
@@ -505,7 +591,7 @@ output "service_account_dns" {
505591}
506592
507593
508- # Rasa Assistent Identity =========================================
594+ # Rasa Assistant Identity =========================================
509595
510596# S3 IAM Access Policy
511597
@@ -566,7 +652,8 @@ resource "aws_iam_policy" "rds_database_access" {
566652 "rds-db:connect"
567653 ]
568654 Resource = [
569- "arn:aws:rds-db:${REGION}:${data.aws_caller_identity.current.account_id}:dbuser:${module.rds_main.db_instance_resource_id}/${DB_ASSISTANT_USERNAME}"
655+ "arn:aws:rds-db:${REGION}:${data.aws_caller_identity.current.account_id}:dbuser:${module.rds_main.db_instance_resource_id}/${DB_ASSISTANT_USERNAME}",
656+ "arn:aws:rds-db:${REGION}:${data.aws_caller_identity.current.account_id}:dbuser:${module.rds_main.db_instance_resource_id}/${DB_ANALYTICS_USERNAME}"
570657 ]
571658 },
572659 ]
@@ -619,6 +706,44 @@ resource "aws_iam_policy" "bedrock_access" {
619706 })
620707}
621708
709+ # MSK IAM Access Policy
710+ resource "aws_iam_policy" "msk_access" {
711+ name = "${NAME}_msk_access"
712+ path = "/"
713+ description = "Rasa ${NAME} MSK Kafka access"
714+
715+ policy = jsonencode({
716+ Version = "2012-10-17"
717+ Statement = [
718+ {
719+ Effect = "Allow"
720+ Action = [
721+ "kafka-cluster:Connect",
722+ "kafka-cluster:DescribeCluster"
723+ ]
724+ Resource = "*"
725+ },
726+ {
727+ Effect = "Allow"
728+ Action = [
729+ "kafka-cluster:*Topic*",
730+ "kafka-cluster:WriteData",
731+ "kafka-cluster:ReadData"
732+ ]
733+ Resource = "*"
734+ },
735+ {
736+ "Effect": "Allow",
737+ "Action": [
738+ "kafka-cluster:AlterGroup",
739+ "kafka-cluster:DescribeGroup"
740+ ],
741+ "Resource": "*"
742+ }
743+ ]
744+ })
745+ }
746+
622747
623748module "iam_role_assistant" {
624749 source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
@@ -627,10 +752,11 @@ module "iam_role_assistant" {
627752 role_name = "${NAME}-assistant"
628753
629754 role_policy_arns = {
630- s3_policy = aws_iam_policy.assistant.arn
631- redis_policy = aws_iam_policy.redis_access.arn
632- rds_policy = aws_iam_policy.rds_database_access.arn
633- bedrock_policy = aws_iam_policy.bedrock_access.arn
755+ s3 = aws_iam_policy.s3_access.arn
756+ redis = aws_iam_policy.redis_access.arn
757+ rds = aws_iam_policy.rds_database_access.arn
758+ bedrock = aws_iam_policy.bedrock_access.arn
759+ msk = aws_iam_policy.msk_access.arn
634760 }
635761
636762 oidc_providers = {
@@ -649,6 +775,33 @@ output "service_account_assistant" {
649775 value = local.service_account_assistant
650776}
651777
778+ module "iam_role_analytics" {
779+ source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
780+ version = "~> 5.59"
781+
782+ role_name = "${NAME}-analytics"
783+
784+ role_policy_arns = {
785+ rds = aws_iam_policy.rds_database_access.arn
786+ msk = aws_iam_policy.msk_access.arn
787+ }
788+
789+ oidc_providers = {
790+ rasa = {
791+ provider_arn = module.eks.oidc_provider_arn
792+ namespace_service_accounts = ["${NAMESPACE}:rasa-analytics"]
793+ }
794+ }
795+ }
796+
797+ locals {
798+ service_account_analytics = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${module.iam_role_analytics.iam_role_name}"
799+ }
800+
801+ output "service_account_analytics" {
802+ value = local.service_account_analytics
803+ }
804+
652805
653806# Rasa Studio Identity =========================================
654807
0 commit comments