I can see in Data Source: aws_ebs_snapshot
that it is possible to filter ebs snapshot using keys based on reference link describe-snapshots.
In the reference page I see description and start-time.
-
How can I select snapshot based on a “contained string” inside description field ?
-
How can I select snapshot based on a upper/lower/range of times using start-time field e.g. as sample in referenze (<=‘2020-03-31’ or >=‘2020-03-31T04:00:00’ or in a time range) ?
For (1.) I need to use each.key o each.value inside for_each, I tried below without success:
filter {
name = "Desciption"
values = ["*${each.key}*"]
}
For (2). I tried somethig like this:
filter {
name = "start-time"
values = ["<= 2021-07-25T04:00:00.000Z"]
}
I cannot figure out how to use filters with partial or time range selectors.
I managed to make substring to work:
filter {
name = "desciption"
values = ["*${each.key}*"]
}
no luck for time selector
I found a working start-time filter. It seems terraform data source --filter does not support ranges as aws ... --query
does.
most_recent seems like --query 'reverse(sort_by(..., ...))[0]. ...'
) but filters
can only filter using simple strings. If it can help, below a sample data source with conditional start-time filter:
### start-time is in UTC format (e.g. 2021-08-01T18:16:14.799000+00:00)
### you can check using aws cli ec2 describe-snapshots --profile ... --region ... --owner-ids self --filters 'Name="start-time",Values="2021-07-28T18:*"'
variable "dr_ami_cfg" {
type = any
default = {}
}
dr_ami_cfg = {
"bastion1" = {
...
"snap_start_time" = "2021-08-01T18:*"
}
"bastion2" = {
...
}
...
}
data "aws_ebs_snapshot" "dr_snap_rbd" {
for_each = {
for key, val in var.dr_ami_cfg:
key => val
}
most_recent = true
owners = ["self"]
filter {
name = "status"
values = ["completed"]
}
filter {
name = "tag:Name"
values = ["rbd_${each.key}*"]
}
filter {
name = "start-time"
values = [try(each.value.snap_start_time, "") != "" ? each.value.snap_start_time : "*"]
}
}