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