Skip to content

Commit

Permalink
Fix. Handle lack of snapshot ID for a volume. (#7995)
Browse files Browse the repository at this point in the history
This commit resolves the issue where lack of snapshot ID in the device mapping
section of the API response to DescribeImagesResponse would cause Terraform to
crash due to a nil pointer dereference. Usually, the snapshot ID is included,
but in some unique cases (e.g. ECS-enabled AMI from Amazon available on the
Market Place) a volume that is attached might not have it.

The API documentation does not clearly define whether the snapshot ID either
should be or must be included for any volume in the response.

Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
  • Loading branch information
kwilczynski authored and stack72 committed Aug 5, 2016
1 parent 7ae7533 commit bed5f62
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion resource_aws_ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,16 @@ func resourceAwsAmiRead(d *schema.ResourceData, meta interface{}) error {
"delete_on_termination": *blockDev.Ebs.DeleteOnTermination,
"encrypted": *blockDev.Ebs.Encrypted,
"iops": 0,
"snapshot_id": *blockDev.Ebs.SnapshotId,
"volume_size": int(*blockDev.Ebs.VolumeSize),
"volume_type": *blockDev.Ebs.VolumeType,
}
if blockDev.Ebs.Iops != nil {
ebsBlockDev["iops"] = int(*blockDev.Ebs.Iops)
}
// The snapshot ID might not be set.
if blockDev.Ebs.SnapshotId != nil {
ebsBlockDev["snapshot_id"] = *blockDev.Ebs.SnapshotId
}
ebsBlockDevs = append(ebsBlockDevs, ebsBlockDev)
} else {
ephemeralBlockDevs = append(ephemeralBlockDevs, map[string]interface{}{
Expand Down
3 changes: 3 additions & 0 deletions resource_aws_ami_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func TestAccAWSAMICopy(t *testing.T) {
}

for _, bdm := range image.BlockDeviceMappings {
// The snapshot ID might not be set,
// even for a block device that is an
// EBS volume.
if bdm.Ebs != nil && bdm.Ebs.SnapshotId != nil {
snapshots = append(snapshots, *bdm.Ebs.SnapshotId)
}
Expand Down

0 comments on commit bed5f62

Please sign in to comment.