I am using Terraform to obtain a specific AMI that’s eated weekly to update EC2 instances. The AMI ID is unknown, however I’m able to retrieve the AMI by name. I’m using Terraform AWS External Data Source, Data Source and a bash script to achieve this. However, I’m receiving the error below. I also provided the bash script and Terraform config file below;
Please advise how to covert to a string or if there is a better way to do this.
Thanks
Michael
Error
aws_ami" "get_ami_id":
│ 20: values = data.external.get_ami_name.result
data.external.get_ami_name.result is map of string with 1 element
Inappropriate value for attribute "values": list of string required.
–
#Terraform Config files
get_ami_name.sh
#!/bin/bash
AMI_NAME=$(aws ec2 describe-images --owners 5555566666 --query 'Images[?Name>=`em`][]'|grep Name|grep -v DeviceName|sort -n|tail -5|sort -t- -nrk5,5|awk '{print $2}'|sed 's/"//g')
jq -n --arg ami_name "$AMI_NAME" '{"ami_name":$ami_name}'
Terraform Main.tf
# Get lastest AMI NAME
#
data "external" "get_ami_name" {
program = ["bash", "${path.root}/get_ami_name.sh"]
}
output "ami_name_out" {
value = data.external.get_ami_name.results
}
#
# Get AMI ID based on name retrived
#
data "aws_ami" "get_ami_id" {
most_recent = true
owners = ["555556666"]
filter {
name = "name"
values = data.external.get_ami_name.results}"
}
}