wardviaene / terraform-course

Course files for my Udemy course about Terraform
https://www.udemy.com/learn-devops-infrastructure-automation-with-terraform/?couponCode=TERRAFORM_GIT
1.59k stars 4.03k forks source link

terraform demo-5 : Datasource #17

Closed narendran07 closed 5 years ago

narendran07 commented 5 years ago

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 = "ap-south-1"
}

data "aws_ip_ranges" "european_ec2" {
   regions = [ "eu-west-1", "eu-central-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.

narendran07 commented 5 years ago

In Terraform 0.12, redundant array brace syntax for arguments changes from being required to being an error. updated code as below..

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
  }
}
wardviaene commented 5 years ago

There is a separate branch for terraform 0.12 code. The brace syntax should be removed there.