String createAMI(ImageCreateRequest request, Context context) { LambdaLogger logger = context.getLogger(); AmazonEC2Async client = createEC2Client(); String imageId = null; try { Future<CreateImageResult> result = client .createImageAsync(new CreateImageRequest(request.getInstanceId(), request.getAmiName()) .withNoReboot(request.isNoReboot())); while (!result.isDone()) { Thread.sleep(1000); } imageId = result.get().getImageId(); logger.log("AMI Create Request End. instanceId[" + request.getInstanceId() + "] noReboot[" + request.isNoReboot() + "] imageId[" + imageId + "]"); } catch (Exception e) { throw new RuntimeException("An unexpected error at the time of AMI creation has occurred", e); } finally { client.shutdown(); } return imageId; }
/** * Build an AMI of a running EC2Instance. Creates a snap which is * disassociated and tracked via a the description. * * @param sourceInstance * the EC2 instance to create an AMI from * @param description * to shove in the console so you know what your looking at * @return id of the ami * @throws TimeoutException * if the ami isn't available in time. * * @see #deleteStorageArtifacts(String) */ public String createAMI(String sourceInstance, String description) throws TimeoutException { CreateImageResult imageResult = ec2Client .createImage(new CreateImageRequest() .withInstanceId(sourceInstance) .withDescription(description) .withName( sourceInstance + "-" + System.currentTimeMillis()) .withNoReboot(true)); String amiId = imageResult.getImageId(); if (!waitForImage(amiId)) { throw new TimeoutException( "Timed out waiting for amazon to create AMI " + amiId); } log.info("Created new AMI with ID: " + amiId); return amiId; }
private String createAMI(Context context, String instanceId) throws Exception { AWS.ec2.stopInstances(Lists.newArrayList(instanceId)); logger.info("create AMI, instanceId={}, imageName={}", instanceId, resource.name()); CreateImageResult result = AWS.ec2.ec2.createImage(new CreateImageRequest(instanceId, resource.name())); String imageId = result.getImageId(); AWS.ec2.createTags(new CreateTagsRequest() .withResources(imageId) .withTags(tagHelper.env(), tagHelper.resourceId(resource.id), tagHelper.version(resource.nextVersion()), tagHelper.name(resource.id + ":" + resource.nextVersion()))); String key = "ami/" + resource.id; context.output(key, String.format("imageId=%s", imageId)); logger.info("result imageId => {}", imageId); return imageId; }
@Override public Image createImage(CreateImageRequest request, ResultCapture<CreateImageResult> extractor) { ActionResult result = resource.performAction("CreateImage", request, extractor); if (result == null) return null; return new ImageImpl(result.getResource()); }
@Override public Image createImage(String name, ResultCapture<CreateImageResult> extractor) { CreateImageRequest request = new CreateImageRequest() .withName(name); return createImage(request, extractor); }
@Override public CreateImageResult createImage(CreateImageRequest createImageRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
@Override public Image createImage(CreateImageRequest request) { return createImage(request, null); }
/** * stop instance and take image snapshot */ public Image imageCreate(final String instanceId, final String name, final String description) throws Exception { logger.info("ensure instance state : instanceId=" + instanceId); final InstanceStateName state = stateFrom(instanceId); final boolean wasRunning; switch (state) { case Pending: waitForIstanceState(instanceId, InstanceStateName.Running); case Running: wasRunning = true; break; case Stopping: waitForIstanceState(instanceId, InstanceStateName.Stopped); case Stopped: wasRunning = false; break; default: throw new Exception("image create : invalid instance state=" + state); } if (wasRunning) { instanceStop(instanceId); } final CreateImageRequest request = new CreateImageRequest(); request.setInstanceId(instanceId); request.setName(name); request.setDescription(description); final CreateImageResult result = amazonClient.createImage(request); final String imageId = result.getImageId(); logger.info("ensure image state: imageId=" + imageId); final Image image = waitForImageCreate(imageId); if (wasRunning) { instanceStart(instanceId); } return image; }
/** * Performs the <code>CreateImage</code> action. * * <p> * The following request parameters will be populated from the data of this * <code>Instance</code> resource, and any conflicting parameter value set * in the request will be overridden: * <ul> * <li> * <b><code>InstanceId</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return The <code>Image</code> resource object associated with the result * of this action. * @see CreateImageRequest */ com.amazonaws.resources.ec2.Image createImage(CreateImageRequest request);
/** * Performs the <code>CreateImage</code> action and use a ResultCapture to * retrieve the low-level client response. * * <p> * The following request parameters will be populated from the data of this * <code>Instance</code> resource, and any conflicting parameter value set * in the request will be overridden: * <ul> * <li> * <b><code>InstanceId</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return The <code>Image</code> resource object associated with the result * of this action. * @see CreateImageRequest */ com.amazonaws.resources.ec2.Image createImage(CreateImageRequest request, ResultCapture<CreateImageResult> extractor);