public DeferredResult<Void> createNameTagAsync(String resourceId, String name) { Tag nameTag = new Tag().withKey(AWS_TAG_NAME).withValue(name); CreateTagsRequest request = new CreateTagsRequest() .withResources(resourceId) .withTags(nameTag); String message = "Name tag AWS resource with id [" + resourceId + "] with name [" + name + "]."; AWSDeferredResultAsyncHandler<CreateTagsRequest, CreateTagsResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message); this.client.createTagsAsync(request, handler); return handler.toDeferredResult() .thenApply(result -> (Void) null); }
void createImageTags(AmazonEC2Async client, ImageCreateRequest imageCreateRequest, Context context) { try { // LambdaLogger logger = context.getLogger(); String instanceId = imageCreateRequest.getInstanceId(); String imageId = imageCreateRequest.getImageId(); List<Tag> tags = new ArrayList<Tag>(); tags.add(new Tag("InstanceId", instanceId)); tags.add(new Tag("BackupType", "image")); String requestDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")); tags.add(new Tag("RequestDate", requestDate)); // Tag to AMI CreateTagsRequest createTagsRequest = new CreateTagsRequest().withResources(imageId); createTagsRequest.setTags(tags); Future<CreateTagsResult> amiTagsResult = client.createTagsAsync(createTagsRequest); while (!amiTagsResult.isDone()) { Thread.sleep(100); } // Tag to EBS Snapshot tags.add(new Tag("ImageId", imageId)); // snapshotにはimageIdを付けておく。 List<String> snapshotIds = getSnapshotIdsFromImageId(client, imageCreateRequest, context); CreateTagsRequest snapshotTagsRequest = new CreateTagsRequest().withResources(snapshotIds); snapshotTagsRequest.setTags(tags); Future<CreateTagsResult> snapshotTagsResult = client.createTagsAsync(snapshotTagsRequest); while (!snapshotTagsResult.isDone()) { Thread.sleep(100); } } catch (Exception e) { context.getLogger().log("[ERROR][ImageStateCheckAndParge] message[" + e.getMessage() + "] stackTrace[" + getStackTrace(e) + "] [" + imageCreateRequest + "]"); } }
public static void main(String[] args) { final String USAGE = "To run this example, supply an instance name and AMI image id\n" + "Ex: CreateInstance <instance-name> <ami-image-id>\n"; if (args.length != 2) { System.out.println(USAGE); System.exit(1); } String name = args[0]; String ami_id = args[1]; final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); RunInstancesRequest run_request = new RunInstancesRequest() .withImageId(ami_id) .withInstanceType(InstanceType.T1Micro) .withMaxCount(1) .withMinCount(1); RunInstancesResult run_response = ec2.runInstances(run_request); String instance_id = run_response.getReservation().getReservationId(); Tag tag = new Tag() .withKey("Name") .withValue(name); CreateTagsRequest tag_request = new CreateTagsRequest() .withTags(tag); CreateTagsResult tag_response = ec2.createTags(tag_request); System.out.printf( "Successfully started EC2 instance %s based on AMI %s", instance_id, ami_id); }
/** * Create Tags. * * @param availabilityZone the availability zone * @param iops the iops * @param size the size * @param snapshotId the snapshot id * @param volumeType the volume type * @return Volume */ protected final boolean createTags(final Collection<String> resources, final Collection<Tag> tags) { CreateTagsRequest req = new CreateTagsRequest(); req.setResources(resources); req.setTags(tags); CreateTagsResult result = amazonEC2Client.createTags(req); if (result != null) { return true; } return false; }