I read that The Terraform language supports user-defined functions but in the mock exam you marked it as false. i know I can do this in Terraform.
function instance_size(cpus, memory) {
if cpus <= 2 && memory <= 4 {
return “t2.micro”
} else if cpus <= 4 && memory <= 16 {
return “t2.small”
} else if cpus <= 8 && memory <= 32 {
return “t2.medium”
} else {
return “t2.large”
}
}
resource “aws_instance” “example” {
ami = “ami-0c55b159cbfafe1f0”
instance_type = instance_size(2, 4)
}
In my example, the instance_size
function takes two arguments: cpus
and memory
. The function uses an if-else
statement to determine the appropriate EC2 instance type based on the inputs, and returns the appropriate instance type as a string. The instance_type
for the aws_instance
resource is then set to the value returned by the instance_size
function, using the arguments 2
and 4
.