Search code examples
terraformaws-parameter-store

Terraform fails to create a policy using data from Parameter Store


I have a comma separated list of IP addresses stored as a string in Parameter Store. Within Terraform, I'm trying to insert those into a statement of a S3 bucket policy.

value in Parameter Store: 10.7.60.210/32, 10.3.113.172/32, 10.9.128.86/32, 10.7.33.40/29, 10.1.168.30/32

data "aws_ssm_parameter" "office_ips" {
  name = "/ops/office/ips"
}

locals {
  ip_list = ["${split(",", data.aws_ssm_parameter.office_ips.value)}"]
}

resource "aws_s3_bucket_policy" "internal" {
  bucket = "${local.bucket_name}"

  policy = <<POLICY
{
  "Version": "2012-10-17",
  "Id": "allowInternalAccess",
  "Statement": [
    {
      "Sid": "IPAllow",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:*",
      "Resource": "arn:aws:s3:::${local.bucket_name}/app/*",
      "Condition": {
        "IpAddress": {
          "aws:SourceIp": [
            "${jsonencode(concat(formatlist("%s", local.ip_list)))}"
          ]
        }
      }
    }
  ]
}
POLICY
}

When I run terraform plan I receive the following:

aws_s3_bucket_policy.internal-tooling: "policy" contains an invalid JSON: invalid character '1' after array element


Solution

  • The problem was I was storing the data as a String in Parameter Store and I should have done so as a StringList.