Skip to content

Commit 6bf38bc

Browse files
Itxakasigmaris
andcommitted
Add snapshot import to amazon-import
This patch adds the possibility of importing an image to Amazon EC2 by using the ImportSnapshot/RegisterImage API, which has lower requirements than the ImportImage API and does not try to modify the imported image. It reuses the current post-process method but diverges once we need to import the image. The artifact upload to S3 is the same, but instead of calling ImportImage, we call ImportSnapshot to create an EBS snapshot from the S3 artifact, then call RegisterImage to register the snapshot as a new AMI. The steps after registering the AMI are identical to the previous image import process. Signed-off-by: Itxaka <igarcia@suse.com> Co-authored-by: Hugh Cole-Baker <sigmaris@gmail.com> Signed-off-by: Hugh Cole-Baker <sigmaris@gmail.com>
1 parent 199a997 commit 6bf38bc

File tree

4 files changed

+401
-123
lines changed

4 files changed

+401
-123
lines changed

builder/common/state.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ func (w *AWSPollingConfig) WaitUntilFastLaunchEnabled(ctx aws.Context, conn *ec2
227227
return err
228228
}
229229

230+
func (w *AWSPollingConfig) WaitUntilSnapshotImported(ctx aws.Context, conn *ec2.EC2, taskID string) error {
231+
importInput := ec2.DescribeImportSnapshotTasksInput{
232+
ImportTaskIds: []*string{&taskID},
233+
}
234+
235+
err := WaitForSnapshotToBeImported(conn,
236+
ctx,
237+
&importInput,
238+
w.getWaiterOptions()...)
239+
return err
240+
}
241+
230242
// Custom waiters using AWS's request.Waiter
231243

232244
func WaitForVolumeToBeAttached(c *ec2.EC2, ctx aws.Context, input *ec2.DescribeVolumesInput, opts ...request.WaiterOption) error {
@@ -371,6 +383,43 @@ func WaitUntilFastLaunchEnabled(c *ec2.EC2, ctx aws.Context, input *ec2.Describe
371383
return w.WaitWithContext(ctx)
372384
}
373385

386+
func WaitForSnapshotToBeImported(c *ec2.EC2, ctx aws.Context, input *ec2.DescribeImportSnapshotTasksInput, opts ...request.WaiterOption) error {
387+
w := request.Waiter{
388+
Name: "DescribeSnapshot",
389+
MaxAttempts: 720,
390+
Delay: request.ConstantWaiterDelay(5 * time.Second),
391+
Acceptors: []request.WaiterAcceptor{
392+
{
393+
State: request.SuccessWaiterState,
394+
Matcher: request.PathAllWaiterMatch,
395+
Argument: "ImportSnapshotTasks[].SnapshotTaskDetail.Status",
396+
Expected: "completed",
397+
},
398+
{
399+
State: request.FailureWaiterState,
400+
Matcher: request.PathAnyWaiterMatch,
401+
Argument: "ImportSnapshotTasks[].SnapshotTaskDetail.Status",
402+
Expected: "deleted",
403+
},
404+
},
405+
Logger: c.Config.Logger,
406+
NewRequest: func(opts []request.Option) (*request.Request, error) {
407+
var inCpy *ec2.DescribeImportSnapshotTasksInput
408+
if input != nil {
409+
tmp := *input
410+
inCpy = &tmp
411+
}
412+
req, _ := c.DescribeImportSnapshotTasksRequest(inCpy)
413+
req.SetContext(ctx)
414+
req.ApplyOptions(opts...)
415+
return req, nil
416+
},
417+
}
418+
w.ApplyOptions(opts...)
419+
420+
return w.WaitWithContext(ctx)
421+
}
422+
374423
// This helper function uses the environment variables AWS_TIMEOUT_SECONDS and
375424
// AWS_POLL_DELAY_SECONDS to generate waiter options that can be passed into any
376425
// request.Waiter function. These options will control how many times the waiter

docs/post-processors/import.mdx

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,39 @@ Optional:
104104
must be set to `uefi`.
105105

106106
- `platform` (string) - The operating system of the virtual machine. One of:
107-
`linux` or `windows`. If `boot_mode` is set to `uefi` then this value must be
108-
set to either `windows` or `linux` depending on the operating system of the
109-
virtual machine.
107+
`linux` or `windows`. If `boot_mode` is set to `uefi` then this value must be
108+
set to either `windows` or `linux` depending on the operating system of the
109+
virtual machine. `windows` can only be used here when `import_type` is `image`.
110110

111111
- `custom_endpoint_ec2` (string) - This option is useful if you use a cloud
112112
provider whose API is compatible with aws EC2. Specify another endpoint
113113
like this `https://ec2.custom.endpoint.com`.
114114

115+
- `ena_support` (boolean) - Only applicable if `import_type` is set to
116+
`snapshot`. This sets a flag on the AMI indicating that the image includes
117+
support for the
118+
[Elastic Network Adapter](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enhanced-networking-ena.html).
119+
Defaults to `false`.
120+
115121
- `format` (string) - One of: `ova`, `raw`, `vhd`, `vhdx`, or `vmdk`. This
116122
specifies the format of the source virtual machine image. The resulting
117123
artifact from the builder is assumed to have a file extension matching the
118-
format. This defaults to `ova`.
124+
format. This defaults to `ova` if `import_type` is `image`, and `raw` if
125+
`import_type` is `snapshot`.
126+
127+
- `import_type` (string) - The method to use to import the image.
128+
One of: `image` or `snapshot`. If set to `image`, the
129+
[ImportImage](https://docs.aws.amazon.com/vm-import/latest/userguide/vmimport-image-import.html)
130+
API is used to perform the import, which only supports a limited number of
131+
[operating systems](https://docs.aws.amazon.com/vm-import/latest/userguide/prerequisites.html#vmimport-operating-systems)
132+
and performs
133+
[programmatic modifications](https://docs.aws.amazon.com/vm-import/latest/userguide/import-modify-vm.html)
134+
to the image during the import process. If set to `snapshot`, the
135+
[ImportSnapshot](https://docs.aws.amazon.com/vm-import/latest/userguide/vmimport-import-snapshot.html)
136+
API is used and then the resulting snapshot is registered as an AMI, which
137+
does not perform any modifications to the image, supports a wider range of
138+
Linux distributions, but does not support importing Windows images.
139+
The default is `image`.
119140

120141
- `insecure_skip_tls_verify` (boolean) - This allows skipping TLS
121142
verification of the AWS EC2 endpoint. The default is `false`.
@@ -127,7 +148,8 @@ Optional:
127148
Machine Image (AMI) after importing. Valid values: `AWS` or `BYOL`
128149
(default). For more details regarding licensing, see
129150
[Prerequisites](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/VMImportPrerequisites.html)
130-
in the VM Import/Export User Guide.
151+
in the VM Import/Export User Guide. If `import_type` is set to `snapshot`, this
152+
is ignored.
131153

132154
- `mfa_code` (string) - The MFA
133155
[TOTP](https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm)
@@ -169,6 +191,10 @@ Optional:
169191
- `skip_region_validation` (boolean) - Set to true if you want to skip
170192
validation of the region configuration option. Default `false`.
171193

194+
- `snapshot_device_name` (string) - The root device name to use in the block
195+
device mapping when registering a snapshot import as an AMI. Only applicable
196+
if `import_type` is `snapshot`. Defaults to `/dev/sda`.
197+
172198
- `tags` (object of key/value strings) - Tags applied to the created AMI and
173199
relevant snapshots.
174200

@@ -177,6 +203,11 @@ Optional:
177203
probably don't need it. This will also be read from the `AWS_SESSION_TOKEN`
178204
environmental variable.
179205

206+
- `virtualization_type` (string) - The virtualization type to be used for
207+
the imported AMI. One of: `hvm` or `paravirtual`. Defaults to `hvm`,
208+
`paravirtual` is only supported on previous-generation EC2 instance types.
209+
This option can only be set when `import_type` is set to `snapshot`.
210+
180211
## Basic Example
181212

182213
Here is a basic example. This assumes that the builder has produced an OVA

0 commit comments

Comments
 (0)