When I execute the following, I get the desire results in the Ami name.
2023-05-16-77777-xxx-aaa-wwww #The name of the customer’s AMI
However, when I try to use regex to get the xxx column of the customer’s AMI name (version number). after commenting out example one
and uncomment example Two.
I get the following error:
Error: Invalid function argument
on main.tf line 33, in output "customer_ami_name"
33: value = regex("^(?:[^-]+-){4}([^-]+)",data.aws_ami.customer_ami.name])
data.aws_ami.customer_ami.name will be know only after apply
Invalid value for "string" parameter: string requried
#Declare the aws providers
terraform {
required_version = "~> 1.2"
required_providers {
aws = {
source = "hashicorp/aws"
version = "2.2.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
locals {
cus_ami_id = "ami-xxxxxxxxx"
}
#
# Get AMI NAME
#
data "aws_ami" "customer_ami" {
owners = ["xxxxxxxxxx"]
filter {
name = "image-id"
values = ["${local.cus_ami_id}"]
}
}
output "customer_ami_name" {
#value = [data.aws_ami.customer_ami.name] #example One
value = regex("^(?:[^-]+-){4}([^-]+)",data.aws_ami.customer_ami.name]) #example Two
}