Search code examples
amazon-web-servicesterraformaws-security-group

Error: Incorrect attribute value type - Terraform datasource(aws_ip_ranges)


I am getting error when i tried to use the terraform datasource (aws_ip_ranges) to get the avaliable ip address ranges for service "ec2".

provider "aws" {
   region = "${var.AWS_REGION}"
}
variable "AWS_REGION" {
   default = "eu-west-1"
}

data "aws_ip_ranges" "european_ec2" {
   regions = [ "eu-west-1" ]
   services = [ "ec2" ]
}
resource "aws_security_group" "from_europe" {
  name = "from_europe"
  ingress {
    from_port = "443"
    to_port = "443"
    protocol = "tcp"
    cidr_blocks = [ "${data.aws_ip_ranges.european_ec2.cidr_blocks}" ]
}
tags = {
  CreateDate = "${data.aws_ip_ranges.european_ec2.create_date}"
  SyncToken = "${data.aws_ip_ranges.european_ec2.sync_token}"
}
}

Getting this below error when executing "terraform apply"

  Error: Incorrect attribute value type

   on securitygroups.tf line 13, in resource "aws_security_group" 
 "from_europe":
  13:     cidr_blocks      = 
  ["${data.aws_ip_ranges.european_ec2.cidr_blocks}"]

  Inappropriate value for attribute "cidr_blocks": element 0: string 
  required.

version: Terraform v0.12.6 + provider.aws v2.23.0

Kindly help to resolve this.


Solution

  • In Terraform 0.12, redundant array brace syntax for arguments changes from being required to being an error. You can update your code and utilize first class variable expressions accordingly to fix the issue:

    resource "aws_security_group" "from_europe" {
      name = "from_europe"
    
      ingress {
        from_port   = "443"
        to_port     = "443"
        protocol    = "tcp"
        cidr_blocks = data.aws_ip_ranges.european_ec2.cidr_blocks
      }
    }