dovregubben
dovregubben

Reputation: 454

Terraform: how to ignore "Error: Your query returned no results" for data source "aws_instances"

I'm using Terraform 0.14.4 to maintain different AWS accounts. I have one .tf file and multiple state files, one for each account.

I use a data source to find all EC2 instances that have a certain tag attached to it:

data "aws_instances" "all_instances" {
  instance_tags = {
    Monitoring = "MONITOR"
  }

  instance_state_names = ["running", "pending", "stopped", "stopping"]
}

This data source is used to generate a few CloudWatch metrics for certain instances.

I have instances with that tag in all accounts but one. Running terraform plan on that account gives me this error:

Error: Your query returned no results. Please change your search criteria and try again.
  on main.tf line 6, in data "aws_instances" "all_instances":
   6: data "aws_instances" "all_instances" {

I'd like to ignore that the data source doesn't find any instances in this particular account, and go ahead with all the other resources maintained in my script.

How do I achieve this?

Upvotes: 4

Views: 4801

Answers (1)

Marcin
Marcin

Reputation: 238329

Apart from what you mentioned in the comment, one other possibility would be to use External Data Source. So instead of using data source given by aws provider (aws_instances) you could implement your own. This way you can program in any logic you want, including handling missing resources.

Upvotes: 2

Related Questions