Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable setting a description for EBS Volume Snapshots #471

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .web-docs/components/builder/ebsvolume/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concept

- `snapshot_volume` (bool) - Create a Snapshot of this Volume.

- `snapshot_description` (string) - The description for the snapshot.

<!-- End of code generated from the comments of the BlockDevice struct in builder/ebsvolume/block_device.go; -->


Expand Down
3 changes: 3 additions & 0 deletions builder/ebsvolume/block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type BlockDevice struct {
// Create a Snapshot of this Volume.
SnapshotVolume bool `mapstructure:"snapshot_volume" required:"false"`

// The description for the snapshot.
SnapshotDescription string `mapstructure:"snapshot_description" required:"false"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to delve a bit into the code for understanding it better, but I believe we should add a check that if snapshot_description is set, snapshot_volume must be true, and error if that's not the case.
Probably the kind of thing to do in the Prepare function for the builder.

Alternatively if you don't want to error, maybe at least warn that it'll be ignored as no snapshot will be created for the block device?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've implemented something and the output looks like this:

$ packer build example.pkr.hcl 
Warning: snapshot_description is ignored when snapshot_volume is not set to true.

  on example.pkr.hcl line 12:
  (source code not available)

This only shows up if snapshot_description is set but snapshot_volume is not (or is set to false).

Do I need to do something to cause the code line to be printed here? The line number is also not quite correct, it's the line number of the source definition.


Comment on lines +30 to +32
Copy link
Contributor Author

@saxonww saxonww Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to add this here instead of builder/common/snapshot_config.go because it doesn't look like SnapshotDescription can be applied to the underlying volume snapshot when building an AMI with the CreateImage() call. You also already support setting an AMI Description using the ami_description field. I can move this there if that would be better, but it seemed like it would be confusing, to me.

awscommon.SnapshotConfig `mapstructure:",squash"`
}

Expand Down
7 changes: 7 additions & 0 deletions builder/ebsvolume/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
"Packer, inclusion of enable_t2_unlimited will error your builds.")
}

for _, configVolumeMapping := range b.config.VolumeMappings {
if configVolumeMapping.SnapshotDescription != "" && !configVolumeMapping.SnapshotVolume {
errs = packersdk.MultiErrorAppend(errs,
fmt.Errorf("All `ebs_volumes` blocks setting `snapshot_description` must also set `snapshot_volume`."))
}
}

if errs != nil && len(errs.Errors) > 0 {
return nil, warns, errs
}
Expand Down
2 changes: 2 additions & 0 deletions builder/ebsvolume/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions builder/ebsvolume/step_snapshot_ebs_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (s *stepSnapshotEBSVolumes) Run(ctx context.Context, state multistep.StateB
input := &ec2.CreateSnapshotInput{
VolumeId: aws.String(*instanceBlockDevice.Ebs.VolumeId),
TagSpecifications: []*ec2.TagSpecification{tagSpec},
Description: aws.String(configVolumeMapping.SnapshotDescription),
}

//Dont try to set an empty tag spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@

- `snapshot_volume` (bool) - Create a Snapshot of this Volume.

- `snapshot_description` (string) - The description for the snapshot.

<!-- End of code generated from the comments of the BlockDevice struct in builder/ebsvolume/block_device.go; -->
Loading