I am using terraform to create an SNS Queue. I am also creating my own SNS policy file
Terraform Code :-
variables.tf
variable "awsAccId" {
default = "111222433"
}
variable "region" {
default = "ca-central-1"
}
locals.tf
sns_topic_name = "fail-notification"
fail_noti_topic_arn = "arn:aws:sns:${var.region}:${var.awsAccId}:${local.sns_topic_name }"
resource "aws_sns_topic" "fail-noti-topic" {
name = "${local.sns_topic_name }"
}
resource "aws_sns_topic_policy" "fail-noti-topic-policy" {
arn = aws_sns_topic.fail-noti-topic.arn
policy = file("policy/sns-policy.json")
}
resource "aws_sns_topic_subscription" "fail-noti-topic" {
...
...
}
Inside policy(folder) -> sns-policy.json (file)
{
"Version": "2012-10-17",
"Id": "${local.sns_topic_name }_policy_ID",
"Statement": [
{
"Sid": "${local.sns_topic_name}_statement_ID",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": [
"SNS:Publish",
"SNS:RemovePermission",
"SNS:SetTopicAttributes",
"SNS:DeleteTopic",
"SNS:ListSubscriptionsByTopic",
"SNS:GetTopicAttributes",
"SNS:AddPermission",
"SNS:Subscribe"
],
"Resource": "${local.fail_noti_topic_arn}",
"Condition": {
"StringEquals": {
"AWS:SourceOwner": "{var.awsAccId}"
}
}
}
]
}
I was expecting that the variables in the json would resolve and I would get the exact values as described in the locals.tf
and variables.tf
file.
Instead, the access policy, as shown in the AWS console is
I tried using policy = jsonencode(file("policy/sns-policy.json"))
, but I am getting another error , while terraform apply
that (something like this)
policyerror Attribute is not valid
.
This error is not there during the planning phase.
I also tried something like
resource "aws_sns_topic_policy" "fail-noti-topic-policy" {
arn = aws_sns_topic.fail-noti-topic.arn
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "${local.sns_topic_name }_policy_ID",
"Statement": [
{
"Sid": "${local.sns_topic_name}_statement_ID",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
......
POLICY
But, this too does NOT resolve the variables with their corresponding values.
What should I do, to resolve the variables in the policy JSON files, so that I get their actual values, instead of the variables themselves ?