Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 :-

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading