Home > database >  How do I create an ebs volume in terraform if it could not find a snapshot to restore from
How do I create an ebs volume in terraform if it could not find a snapshot to restore from

Time:10-04

So I was reading the documentation on here: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ebs_volume

It says that we can either use the SnapshotId= or size= such that snapshot id will create the volume from a snapshot given the id, and size= will create a brand new one.

I have this data source to access my snapshots:

data "aws_ebs_snapshot" "p4_ebs_snapshot" {
  most_recent = true
  owners = ["self"]

  filter {
    name = "tag:Name"
    values = ["PerforceDepots"]
  }
}

I want to know if it's possible to do something like this:

if data.foundSnapshot?
    snapshotId=data.id;
else
    size=100
end

The above is obviously just pseudocode but I was wondering how I could do something like this in terraform. Is it possible?

CodePudding user response:

Sadly, its not possible without external data source. TF does not have a notion of checking if something exists. If any data source does not exist, it errors out. The only way around that is to develop your own data source, which would provide such a functionality.

  • Related