About validation problem

Hi ,

I hope you are doing great!

ı am tring below code but I am gettıng the following error. How should ı write the condition part here?

provider “aws” {
region = var.aws_region
}

resource “aws_instance” “my-instance” {
ami = lookup(var.ami, var.aws_region)
instance_type = lookup(var.instance_type, var.aws_region)
subnet_id = aws_subnet.subnet.id
tags = {
Name = lookup(var.tag, var.aws_region)
}
}

resource “aws_vpc” “vpc” {
cidr_block = lookup(var.vpc_cidr, var.aws_region)
}

resource “aws_subnet” “subnet” {
cidr_block = lookup(var.subnet_cidr, var.aws_region)
vpc_id = aws_vpc.vpc.id
tags = {
Name = “Main”
}
}

variable “instance_type” {
type = map
default = {
“us-east-1” = “t2.nano”
“us-east-2” = “t2.micro”
“us-west-2” = “t2.small”
}

validation {
condition = ( length(var.instance_type) > 3 && substr(var.instance_type,0,3)==“t2.”)
error_message = “The instance_type value must start with "t2.".”
}
}

variable “vpc_cidr” {
type = map
default = {
“us-east-1” = “10.0.0.0/16”
“us-east-2” = “10.30.0.0/16”
“us-west-2” = “10.20.0.0/16”
}
}

variable “subnet_cidr” {
type = map
default = {
“us-east-1” = “10.0.2.0/24”
“us-east-2” = “10.30.4.0/28”
“us-west-2” = “10.20.1.0/24”
}
}

variable “tag” {
type = map
default = {
“us-east-1” = “terraform_us_east_1”
“us-east-2” = “terraform_us_east_2”
“us-west-2” = “terraform_us_west_2”
}
}

variable “ami” {
type = map
default = {
“us-east-1” = “ami-053b0d53c279acc90”
“us-east-2” = “ami-024e6efaf93d85776”
“us-west-2” = “ami-0f3769c8d8429942f”
}
}

variable “aws_region” {
description = “AWS region”
}

output “vpcinfo” {
value = aws_vpc.vpc.cidr_block
}

output “subnetinfo” {
value = aws_subnet.subnet.cidr_block
}

PS C:\Users\alkes\OneDrive\Masaüstü\TERRAFORM\map> terraform apply
var.aws_region
AWS region

Enter a value: us-east-2


│ Error: Invalid function argument

│ on map_validation.tf line 35, in variable “instance_type”:
│ 35: condition = ( length(var.instance_type) > 3 && substr(var.instance_type,0,3)==“t2.”)
│ ├────────────────
│ │ while calling substr(str, offset, length)
│ │ var.instance_type is a map of dynamic

│ Invalid value for “str” parameter: string required.

Regards
Elif

I think this is what you want

variable "instance_type" {
  type = map
  default = {
    "us-east-1" = "t2.nano"
    "us-east-2" = "t2.micro"
    "us-west-2" = "t3.small"
  }

 validation {
  condition = alltrue([
     for k, v in var.instance_type : length(v) > 3 && substr(v,0,3)=="t2."
     ])
     error_message = "The instance_type value must start with t2."
   }
}

And you see I put a t3 in there, and you get this when you terraform apply

╷
│ Error: Invalid value for variable
│
│   on main.tf line 1:
│    1: variable "instance_type" {
│     ├────────────────
│     │ var.instance_type is map of string with 3 elements
│
│ The instance_type value must start with t2.
│
│ This was checked by the validation rule at main.tf:9,2-12.

In future, please post code as blocks like I have here. It is very hard to read when it is pasted as text - all the indentation is lost.

Also I noticed
C:\Users\alkes\OneDrive\Masaüstü\

Some software you may use, e.g. Vagrant has issues if there are unicode characters like ü in directory names. Just forewarning you!

Hi Alistair,

I run the code as your recommendatıon but although ı chosed us-east-2 region which has t2.micro ınstance type ıt gave an error again.

variable “instance_type” {
type = map(any)
default = {
“us-east-1” = “t2.nano”
“us-east-2” = “t2.micro”
“us-west-2” = “t3.small”
}

validation {
condition = alltrue([
for k, v in var.instance_type : length(v) > 3 && substr(v, 0, 3) == “t2.”
])
error_message = “The instance_type value must start with t2.”
}
}

PS C:\Users\alkes\OneDrive\Masaüstü\TERRAFORM\map> terraform apply
var.aws_region
AWS region

Enter a value: us-east-2

aws_vpc.vpc: Refreshing state… [id=vpc-0ee86b452a0fef78a]
aws_subnet.subnet: Refreshing state… [id=subnet-099fbb0a139c4aa15]

Note: Objects have changed outside of Terraform.



… Error: Invalid value for variable

│ on map_validation.tf line 26:
│ 26: variable “instance_type” {
│ ├────────────────
│ │ var.instance_type is map of string with 3 elements

│ The instance_type value must start with t2.

Hi Alistair,

ı lots of time tried to send you my code as blocks . For this ı installed prettier extention on visual studio. I think ıt did not happen now but ı will do it as soon as possıble.

Thank you for your recommendation. ıt will be usefull ındeed.

When you type in the reply box, do this

```

all text here between the upper and lower ```
is code formatted

```
^
type 3 back ticks

As for the validation, the rule checks that all entries in the map have t2 instances. The validation cannot reference other variables e.g. var.aws_region.

The validation on instance_type can only check the values in instance_type which must all be t2 or there will be an validation error.

1 Like