0

I am using Terraform to obtain a specific AMI that's created 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

Blockquote│ Error: Incorrect attribute value type │ │ on main.tf line 20, in data "aws_ami" "get_ami_id": │ 20: values = "{$data.external.get_ami_name.results}" │ │ Inappropriate value for attribute "values": list of string required.

` 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}"
   }
}
0

1 Answer 1

1

The following just creates a string of literal value "{$data.external.get_ami_name.results}":

values = "{$data.external.get_ami_name.results}"

It probably should be:

values = data.external.get_ami_name.results

Similarly, for

output "ami_name_out" {
   value = ["data.external.get_ami_name.results"]
}

which should be:

output "ami_name_out" {
   value = data.external.get_ami_name.results
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply, receving this error Error: Incorrect attribute value type │ │ on main.tf line 20, in data "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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.