amazon-web-servicesterraformterraform-provider-awsaws-ebs

Copy EBS snapshot from one region to another region


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?


Solution

  • 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.