I know that we can use null resource and use triggers to set key,value of variables there and then refer them in provisioner local_exec with self.triggers.key but in our case we are not using null resource . we want to fix it when we use destroy provisioner “local_exec” within the resource “aws_instance” . In this case we have to pass variables value like self.tags[“Region”] as below but we don’t want to set Region and NamingPrefix tags to our instances and just because destroy provisioner is deprecated to use external references as var.naming_prefix , we have to unnecessarily add it into tags .
Is there any other solution for this ? Please do share.
locals {
ec2_tags = merge(var.std_tags, {
Naming_Prefix = var.naming_prefix
Region = var.region
})
}
resource "aws_instance" "xyz" {
ami = data.aws_ami.centos_7.id
count = var.num
instance_type = var.instance_type
key_name = var.key_name
subnet_id = element(split(",", var.subnet_ids), count.index)
tags = local.ec2_tags
provisioner "local-exec" {
command = "aws sns publish --topic-arn xxxxxxxxxxxxxxxxxxxx --message '{\"operation\": \"delete\", \"hostname\": \"${self.tags["NamingPrefix"]}-${self.id}.${self.tags["Region"]}\", \"param_name\": \"pod\", \"param_value\": \"${self.tags["Pod"]}\"}' --region us-east-1"
when = destroy
}
provisioner "local-exec" {
command = "aws sns publish --topic-arn xxxxxxxxxxxxx --message '{\"hostname\": \"${var.naming_prefix}-${self.id}.${var.region}\", \"param_name\": \"pod\", \"param_value\": \"${var.pod}\"}' --region us-east-1"
}
}