Home > Mobile >  How to use kms key from one module inside other sqs module in terraform?
How to use kms key from one module inside other sqs module in terraform?

Time:07-13

I have created two modules in terraform. One for SQS and other for KMS key. This is my SQS module

resource "aws_sqs_queue" "terraform_queue" {
  name                      = "terraform-example-queue"
  delay_seconds             = 90
  max_message_size          = 2048
  message_retention_seconds = 86400
  kms_master_key_id         = ????
  receive_wait_time_seconds = 10
}

and this is my KMS module

resource "aws_kms_key" "a" {
  description             = "KMS key 1"
  deletion_window_in_days = 7
}

The contents of my parent module is this :-

module "kms" {
    source ="./modules/KMS"
}

module "sqs" {
    source = "./modules/SQS"
}

How can i use the kms key from this module inside my sqs module or in other words what would be the value for kms_master_key_id in sqs? Thank you.

CodePudding user response:

You have to output key id from your kms module, and the pass it to sqs:

module "sqs" {
    source = "./modules/SQS"
    key_id  = module.kms.id
}

In the sqs you will have variable:

variable "key_id" {}


resource "aws_sqs_queue" "terraform_queue" {
  name                      = "terraform-example-queue"
  delay_seconds             = 90
  max_message_size          = 2048
  message_retention_seconds = 86400
  kms_master_key_id         = var.key_id
  receive_wait_time_seconds = 10
}

The only other way would be through a data source, but using outputs in a module is a proper way.

  • Related