1

I wanted to copy my EBS snapshot from one region to another region. But while filtering the snapshot-id, it will return id named 1411205605 but i expected it to return something like: snap-..... .

Here is my code:

data "aws_ebs_snapshot_ids" "ebs_volumes" {

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

  filter {
    name   = "volume-size"
    values = ["2"]
  }
}

output "ebs_snapshot_ids"{
    value = ["${data.aws_ebs_snapshot_ids.ebs_volumes.ids}"]
}


resource "aws_ebs_snapshot_copy" "example_copy" {
  source_snapshot_id = "${data.aws_ebs_snapshot_ids.ebs_volumes.id}"
  source_region      = "ap-southeast-1"

  tags {
    Name = "aaa_copy_snap"
  }

}

The output while running terraform apply is :

aws_ebs_snapshot_copy.example_copy: InvalidParameterValue: Value (1411205605) for parameter snapshotId is invalid. Expected: 'snap-...'. status code: 400, request id: bd577049-8b4e-45bc-8415-59e22b4d26d5

I don't know where i made mistake. How can i resolve this issue?

Deependra Dangal
  • 1,145
  • 1
  • 13
  • 36
  • The error is in this statement - "${data.aws_ebs_snapshot_ids.ebs_volumes.id}". You can't retrieve the id like this. Actually it returns a list of ids. I'll update once I figure out something. – Shiv Rajawat Nov 04 '18 at 09:42

1 Answers1

1

It is because "Data Source: aws_ebs_snapshot_ids" returns an attribute "ids" which is set to the list of EBS snapshot IDs, sorted by creation time in descending order.

Now in your case it is safe to assume that "ids" contains a single snapshot id since you are using name as a filter. Hence change the code as shown below to retrieve that id.

source_snapshot_id = "${data.aws_ebs_snapshot_ids.ebs_volumes.ids.0}"

The "0" used here is to retrieve the 1st element from the list of ids. In your case it's the only element.

Shiv Rajawat
  • 898
  • 9
  • 21
  • it worked!!! thank you. Now if i want to copy the snapshot to another region than ap-southeast-1, then what should i do? if i put a different region name in "provider "aws" section" , it shows me the error – Deependra Dangal Nov 05 '18 at 02:44
  • Try using alias inside provider block. – Shiv Rajawat Nov 05 '18 at 03:10
  • I don't want to edit the answer since I'm not sure if 'alias' will work on not. And copy pasting code inside comment isn't looking good. Get an idea of using 'alias' from here https://github.com/hashicorp/terraform/issues/4789 – Shiv Rajawat Nov 05 '18 at 03:13
  • Let me know if it worked and if it didn't then ask another question for the same so that someone else can answer it. – Shiv Rajawat Nov 05 '18 at 03:20
  • I have the same requirement. How to use Terraform to copy EBS snapshots from 1 region to the other? – Biju Jun 29 '21 at 11:06