0
votes

I am about to refactor a couple of code for a business project. Among other tings, converting from JSON to YAML templates is necessary. I use terraform for infrastructure deployment.

I have this JSON template cf_sns.json.tpl file:

{
 "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "SNSTopic": {
      "Type": "AWS::SNS::Topic",
      "Properties": {
        "TopicName": "${sns_topic_name}",
        "KmsMasterKeyId": "${kms_key_id}",
        "DisplayName": "${sns_topic_name}",
        "Subscription": [
          "${sns_subscription_list}"
        ]
      }
    }
  },
  "Outputs" : {
  "SNSTopicARN" : {
    "Description": "The SNS Topic Arn",  
    "Value" : { "Ref" : "SNSTopic" }
  }
}
}

This is a main.tf file using this template file:

data "template_file" "this" {
  template = "${file("${path.module}/templates/cf_sns.json.tpl")}"
  vars = {
    kms_key_id            = var.kms_key_id
    sns_topic_name        = var.sns_topic_name
    sns_subscription_list = join(",", formatlist("{\"Endpoint\": \"%s\",\"Protocol\": \"%s\"}", var.sns_subscription_email_address_list, "email"))

  }
}

I pass ["myemail", "myOtherEmail"] to var.sns_subscription_email_adress_list. I had to use this approach with a cloudformation resource since Terraform does not support the email protocol for a sns subspription.

How can I refactor the cf_sns.json.tpl to a YAML file together with the data resource mentioned above in the main.tf file? Particularly, I have no clue how to properly pass the sns_subscription_list as YAML array.

1
This is Terraform 0.11? - Matt Schuchard
It is terraform 0.12.25 - WorkoutBuddy

1 Answers

1
votes

That cf_sns.json.tpl is AWS CloudFormation code, if you are already using terraform just refactor that all the way, not just convert from JSON to YAML but completely get rid of that and use the proper terraform resources:

Here is some sample code:

resource "aws_sns_topic" "SNSTopic" {
  name              = var.sns_topic_name
  kms_master_key_id = var.kms_key_id
  display_name      = var.sns_topic_name 
}

output "SNSTopicARN" {
  value = aws_sns_topic.SNSTopic.arn
}