/** * Update tags for one all more instance * @param instanceIds * @param tags * @throws Exception */ @Override public void setTagsForInstances(List<String> instanceIds, List<Tag> tags) throws Exception { Preconditions.checkNotNull(instanceIds); Preconditions.checkNotNull(tags); awsRateLimiter.acquire(); OperationStats op = new OperationStats("ec2InstanceStore", "setTagsForInstances"); try { if (tags.size() > 0) { CreateTagsRequest req = new CreateTagsRequest(instanceIds, tags); defaultClient.createTags(req); } op.succeed(); } catch (Exception ex) { op.failed(); throw ex; } }
void attachSnapshotTags(AmazonEC2Async client, String sourceSnapshotId, String snapshotId) { DescribeSnapshotsResult result = client .describeSnapshots(new DescribeSnapshotsRequest().withSnapshotIds(sourceSnapshotId)); List<Snapshot> snapshots = result.getSnapshots(); if (snapshots.size() != 1) { throw new RuntimeException("snapshot can not found. sourceSnapshotId[" + snapshotId + "]"); } List<Tag> sourceSnapshotTags = snapshots.get(0).getTags(); List<Tag> tags = new ArrayList<Tag>(); tags.addAll(sourceSnapshotTags); tags.add(new Tag("SourceSnapshotId", sourceSnapshotId)); tags.add(new Tag("BackupType", "copy-snapshot")); // overwrite CreateTagsRequest snapshotTagsRequest = new CreateTagsRequest().withResources(snapshotId); snapshotTagsRequest.setTags(tags); client.createTags(snapshotTagsRequest); }
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); }
/** * Tags an EC2 instance. Expects that the instance already exists or is in the process of * being created. This may also tag EBS volumes depending on template configurations. * * @param template the instance template * @param userDefinedTags the user-defined tags * @param virtualInstanceId the virtual instance id * @param ec2InstanceId the EC2 instance id * @return true if the instance was successfully tagged, false otherwise * @throws InterruptedException if the operation is interrupted */ private boolean tagSpotInstance(EC2InstanceTemplate template, List<Tag> userDefinedTags, String virtualInstanceId, String ec2InstanceId) throws InterruptedException { LOG.info(">> Tagging instance {} / {}", ec2InstanceId, virtualInstanceId); // We have to individually tag the spot instance and it's associated volumes // since AWS doesn't allow specifying tags as part of the launch request for // spot instances. // Wait for the instance to be started. If it is terminating, skip tagging. if (!waitUntilInstanceHasStarted(ec2InstanceId)) { return false; } List<Tag> tags = ec2TagHelper.getInstanceTags(template, virtualInstanceId, userDefinedTags); client.createTags(new CreateTagsRequest().withTags(tags).withResources(ec2InstanceId)); // Tag EBS volumes if they were part of instance launch request if (EBSAllocationStrategy.get(template) == EBSAllocationStrategy.AS_INSTANCE_REQUEST) { tagSpotEbsVolumes(ec2InstanceId, virtualInstanceId, tags); } return true; }
public List<Instance> runInstances(RunInstancesRequest request, Tag... tags) throws Exception { logger.info("create ec2 instance, request={}", request); RunInstancesResult result = new Runner<RunInstancesResult>() .maxAttempts(3) .retryInterval(Duration.ofSeconds(20)) .retryOn(this::retryOnRunInstance) .run(() -> ec2.runInstances(request)); Threads.sleepRoughly(Duration.ofSeconds(5)); // wait little bit to make sure instance is visible to tag service List<String> instanceIds = result.getReservation().getInstances().stream().map(Instance::getInstanceId).collect(Collectors.toList()); CreateTagsRequest tagsRequest = new CreateTagsRequest() .withResources(instanceIds) .withTags(tags); createTags(tagsRequest); waitUntilRunning(instanceIds); return describeInstances(instanceIds); }
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; }
private String createSG(Environment env) throws Exception { String sgName = env.name + ":" + resourceId; CreateSecurityGroupRequest request = new CreateSecurityGroupRequest(sgName, sgName); if (bakeSubnet != null) request.setVpcId(bakeSubnet.getVpcId()); String sgId = AWS.ec2.createSecurityGroup(request).getGroupId(); AWS.ec2.createSGIngressRules(sgId, Lists.newArrayList(new IpPermission() .withIpv4Ranges(new IpRange().withCidrIp("0.0.0.0/0")) .withFromPort(22) .withToPort(22) .withIpProtocol("tcp"))); AWS.ec2.createTags(new CreateTagsRequest() .withResources(sgId) .withTags(tagHelper.name(resourceId), tagHelper.env(), tagHelper.resourceId(resourceId))); return sgId; }
@Override public void execute(Context context) throws Exception { logger.info("create route table, routeTable={}", resource.id); resource.remoteRouteTable = AWS.vpc.ec2.createRouteTable(new CreateRouteTableRequest().withVpcId(resource.vpc.remoteVPC.getVpcId())).getRouteTable(); if (resource.internetGateway != null) { AWS.vpc.ec2.createRoute(new CreateRouteRequest() .withRouteTableId(resource.remoteRouteTable.getRouteTableId()) .withGatewayId(resource.internetGateway.remoteInternetGatewayId) .withDestinationCidrBlock("0.0.0.0/0")); } else { AWS.vpc.ec2.createRoute(new CreateRouteRequest() .withRouteTableId(resource.remoteRouteTable.getRouteTableId()) .withNatGatewayId(resource.nat.remoteNATGateway.getNatGatewayId()) .withDestinationCidrBlock("0.0.0.0/0")); } EC2TagHelper tagHelper = new EC2TagHelper(context.env); AWS.ec2.createTags(new CreateTagsRequest() .withResources(resource.remoteRouteTable.getRouteTableId()) .withTags(tagHelper.env(), tagHelper.name(resource.id), tagHelper.resourceId(resource.id))); }
@Override public boolean tagEnvironmentWithVersion(Region region, DeployJobVariables jobVariables) { String searchTag = jobVariables.getEnvironment(); String version = jobVariables.getVersion(); LOGGER.info("tagEnvironmentWithVersion " + region + " Tag " + searchTag + " version " + version); boolean environmentSuccessfulTagged = false; ec2.setRegion(region); DescribeInstancesResult instances = ec2.describeInstances(); for (Reservation reservation : instances.getReservations()) { for (Instance instance : reservation.getInstances()) { for (Tag tag : instance.getTags()) { if (tag.getValue().equalsIgnoreCase(searchTag)) { CreateTagsRequest createTagsRequest = new CreateTagsRequest(); createTagsRequest.withResources(instance.getInstanceId()).withTags(new Tag(VERSION_TAG, version)); LOGGER.info("Create Tag " + version + " for instance " + instance.getInstanceId()); ec2.createTags(createTagsRequest); environmentSuccessfulTagged = true; } } } } return environmentSuccessfulTagged; }
/** * Sets tags for the specified instance * @param instanceId * @return */ private void setTagsForInstance(String instanceId) { Set<Object> keys = awsProperties.keySet(); List<Tag> tags = new ArrayList<>(); for(Object o : keys) { if(o instanceof String && ((String)o).startsWith("tag")) { String values = (String)awsProperties.get(o); String[] splitValues = values.split(","); String key = splitValues[0]; String value = splitValues[1]; Tag tagToAdd = new Tag(key,value); log.info("Adding tag: " + tagToAdd); tags.add(tagToAdd); } } // Including a hard coded tag here so we can track which resources originate from this plugin Tag nodeTag = new Tag("LaunchSource","SeleniumGridScalerPlugin"); log.info("Adding hard-coded tag: " + nodeTag); tags.add(nodeTag); CreateTagsRequest ctr = new CreateTagsRequest(Arrays.asList(instanceId),tags); ec2Client.createTags(ctr); }
public void tagInstance(String instanceId, String tag, String value, AmazonEC2 ec2Client) { System.out.println(instanceId); //quick fix try { Thread.sleep(1000); } catch (InterruptedException e) { // swallow } CreateTagsRequest request = new CreateTagsRequest(); request = request.withResources(instanceId) .withTags(new Tag(tag, value)); ec2Client.createTags(request); }
private void createTags(PropertyHandler ph) throws APPlatformException { List<Tag> tags = new ArrayList<Tag>(); tags.add(new Tag(PropertyHandler.TAG_NAME, ph.getInstanceName())); tags.add(new Tag(PropertyHandler.TAG_SUBSCRIPTION_ID, ph.getSettings() .getSubscriptionId())); tags.add(new Tag(PropertyHandler.TAG_ORGANIZATION_ID, ph.getSettings() .getOrganizationId())); CreateTagsRequest ctr = new CreateTagsRequest(); LOGGER.debug("attach tags to resource " + ph.getAWSInstanceId()); ctr.withResources(ph.getAWSInstanceId()).setTags(tags); getEC2().createTags(ctr); }
@Test public void testCreateInstance() throws Exception { ec2mock.createRunInstancesResult("instance1"); ec2mock.createDescribeImagesResult("image1"); ec2mock.createDescribeSubnetsResult("subnet-a77430d0"); ec2mock.createDescribeSecurityGroupResult("subnet-a77430d0", "security_group1,security_group2"); ec2mock.createDescribeInstancesResult("instance1", "ok", "1.2.3.4"); Image image = ec2comm.resolveAMI("image1"); ec2comm.createInstance(image); String result = ph.getAWSInstanceId(); assertEquals("instance1", result); ArgumentCaptor<RunInstancesRequest> arg1 = ArgumentCaptor .forClass(RunInstancesRequest.class); verify(ec2).runInstances(arg1.capture()); RunInstancesRequest rir = arg1.getValue(); assertEquals("image1", rir.getImageId()); assertEquals("type1", rir.getInstanceType()); assertEquals("key_pair", rir.getKeyName()); assertEquals(1, rir.getMinCount().intValue()); assertEquals(1, rir.getMaxCount().intValue()); ArgumentCaptor<CreateTagsRequest> arg2 = ArgumentCaptor .forClass(CreateTagsRequest.class); verify(ec2).createTags(arg2.capture()); CreateTagsRequest ctr = arg2.getValue(); for (Tag t : ctr.getTags()) { if (t.getKey().equalsIgnoreCase("Name")) { assertEquals("name1", t.getValue()); } else if (t.getKey().equalsIgnoreCase("SubscriptionId")) { assertEquals("subId", t.getValue()); } else if (t.getKey().equalsIgnoreCase("OrganizationId")) { assertEquals("orgId", t.getValue()); } } parameters.put("USERDATA_URL", new Setting("USERDATA_URL", "userdata")); }
void attachSnapshotTags(AmazonEC2Async client, String volumeId, String snapshotId) { List<Tag> tags = new ArrayList<Tag>(); tags.add(new Tag("VolumeId", volumeId)); tags.add(new Tag("BackupType", "snapshot")); CreateTagsRequest snapshotTagsRequest = new CreateTagsRequest().withResources(snapshotId); snapshotTagsRequest.setTags(tags); client.createTags(snapshotTagsRequest); }
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 + "]"); } }
/** * Synchronous Tagging of one or many AWS resources with the provided tags. */ public static void tagResources(AmazonEC2AsyncClient client, Collection<Tag> tags, String... resourceIds) { if (isAwsClientMock()) { return; } CreateTagsRequest req = new CreateTagsRequest() .withResources(resourceIds).withTags(tags); client.createTags(req); }
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); }
/** * Tags an EC2 Spot instance request. * * @param userDefinedTags the user-defined tags * @param spotInstanceRequestId the Spot instance request ID * @param virtualInstanceId the virtual instance ID * @throws InterruptedException if the operation is interrupted */ @VisibleForTesting @SuppressWarnings("PMD.UselessParentheses") protected void tagSpotInstanceRequest(List<Tag> userDefinedTags, String spotInstanceRequestId, String virtualInstanceId) throws InterruptedException { LOG.info(">> Tagging Spot instance request {} / {}", spotInstanceRequestId, virtualInstanceId); List<Tag> tags = Lists.newArrayList( ec2TagHelper.createClouderaDirectorIdTag(virtualInstanceId), ec2TagHelper.createClouderaDirectorTemplateNameTag(template.getName())); tags.addAll(userDefinedTags); // Wait for the request to become visible while (!spotInstanceRequestExists(spotInstanceRequestId)) { TimeUnit.SECONDS.sleep(5); } // Test failures and google indicate that we can fail to find a request to tag even when we // have determined that it exists by describing it. I am adding a retry loop to attempt to // avoid this case. boolean tagged = false; while (!tagged && (System.currentTimeMillis() < requestExpirationTime.getTime())) { try { client.createTags( new CreateTagsRequest().withTags(tags).withResources(spotInstanceRequestId)); tagged = true; } catch (AmazonServiceException e) { String errorCode = e.getErrorCode(); if ("InvalidSpotInstanceRequestID.NotFound".equals(errorCode)) { LOG.info(">> Waiting, requestId {}, transient error {}...", spotInstanceRequestId, errorCode); TimeUnit.SECONDS.sleep(5); } else { throw AWSExceptions.propagate(e); } } } }
/** * * @{inheritDoc */ @Override public void tagInstance(final List<String> instanceIds, KeyValuePair... tag) { if (tag.length != 0) { final List<Tag> tags = new ArrayList<Tag>(); for (KeyValuePair pair : tag) { tags.add(new Tag(pair.getKey(), pair.getValue())); } new Thread(new Runnable() { @Override public void run() { int count = 0; try { while (++count <= 5 && !instanceIds.isEmpty()) { Thread.sleep(5000); CreateTagsRequest createTagsRequest = new CreateTagsRequest().withResources(instanceIds).withTags(tags); asynchEc2Client.createTagsAsync(createTagsRequest); Thread.sleep(1000); Future<DescribeInstancesResult> describeInstances = asynchEc2Client.describeInstancesAsync(new DescribeInstancesRequest().withInstanceIds(instanceIds)); for (Reservation r : describeInstances.get().getReservations()) { for (Instance i : r.getInstances()) { if (i.getTags() != null && !i.getTags().isEmpty()) { instanceIds.remove(i.getInstanceId()); } } } } } catch (Exception e) { LOG.error("Error tagging instances: " + e, e); } }; }).start(); } }
public List<String> runInstances(int numberOfInstances) { if (numberOfInstances <= 0) return null; if (numberOfInstances > 20) { numberOfInstances = 20; logger.info("Can't run more than 20 instances."); } RunInstancesRequest request = new RunInstancesRequest(Configuration.EC2_IMAGE_ID, numberOfInstances, numberOfInstances); request.setInstanceType(Configuration.EC2_INSTANCE_TYPE); IamInstanceProfileSpecification prof = new IamInstanceProfileSpecification(); prof.setName(Configuration.EC2_IAM_PROFILE); // security concerns request.setIamInstanceProfile(prof); request.setUserData(getStartupScript()); // base64 request.setKeyName(Configuration.EC2_KEYPAIR_NAME); List<Instance> instances = _ec2.runInstances(request).getReservation().getInstances(); List<Tag> tags = new ArrayList<Tag>(); tags.add(new Tag(_tagKey, _defaultTag)); tags.add(new Tag("Name", _ofType.toString().toLowerCase())); logger.info("Launch instances: " + instances); List<String> instancesID = new ArrayList<String>(); for (Instance instance : instances) { instancesID.add(instance.getInstanceId()); } // tag the resources CreateTagsRequest tagreq = new CreateTagsRequest(instancesID, tags); _ec2.createTags(tagreq); return instancesID; }
public void setName(String name) { com.amazonaws.services.ec2.AmazonEC2 client = AmazonEC2.connect(); CreateTagsRequest req = new CreateTagsRequest(); List<String> instanceIds = new ArrayList<String>(); instanceIds.add(id); req.withResources(instanceIds); List<Tag> tags = new ArrayList<Tag>(); tags.add(new Tag("Name", name)); req.setTags(tags); client.createTags(req); }
public void createTags(final CreateTagsRequest request) throws Exception { new Runner<>() .retryInterval(Duration.ofSeconds(5)) .maxAttempts(3) .retryOn(e -> e instanceof AmazonServiceException) .run(() -> { logger.info("create tags, request={}", request); ec2.createTags(request); return null; }); }
@Override public void execute(Context context) throws Exception { EC2TagHelper tags = new EC2TagHelper(context.env); String description = context.env.name + ":" + resource.id; CreateSecurityGroupRequest request = new CreateSecurityGroupRequest(resource.name, description); if (resource.vpc != null) request.withVpcId(resource.vpc.remoteVPC.getVpcId()); resource.remoteSecurityGroup = AWS.ec2.createSecurityGroup(request); Threads.sleepRoughly(Duration.ofSeconds(5)); // wait small period of time, for sg to be visible for creating tag AWS.ec2.createTags(new CreateTagsRequest() .withResources(resource.remoteSecurityGroup.getGroupId()) .withTags(tags.env(), tags.resourceId(resource.id), tags.name(resource.id))); }
@Override public void execute(Context context) throws Exception { resource.remoteInternetGatewayId = AWS.vpc.createInternetGateway(resource.vpc.remoteVPC.getVpcId()).getInternetGatewayId(); EC2TagHelper tagHelper = new EC2TagHelper(context.env); AWS.ec2.createTags(new CreateTagsRequest() .withResources(resource.remoteInternetGatewayId) .withTags(tagHelper.env(), tagHelper.resourceId(resource.id), tagHelper.name(resource.id))); }
private void createTag(Environment env) throws Exception { EC2TagHelper tagHelper = new EC2TagHelper(env); CreateTagsRequest createTagsRequest = new CreateTagsRequest() .withResources(resource.remoteVPC.getVpcId()) .withTags(tagHelper.env(), tagHelper.resourceId(resource.id), tagHelper.name(resource.id)); AWS.ec2.createTags(createTagsRequest); }
@Override public void setTags(Iterable<String> instanceIds, Tags tags) { List<Tag> awsTags = newArrayList(); for (org.excalibur.core.cloud.api.domain.Tag tag: tags) { awsTags.add(new Tag(tag.getName(), tag.getValue())); } this.ec2_.createTags(new CreateTagsRequest().withResources(toArray(instanceIds, String.class)).withTags(awsTags)); }
public void createTag(AwsProcessClient awsProcessClient, String resourceId, List<Tag> tags) { CreateTagsRequest request = new CreateTagsRequest(); request.withResources(resourceId); request.withTags(tags); awsProcessClient.getEc2Client().createTags(request); // ログ出力 if (log.isInfoEnabled()) { for (Tag tag : tags) { log.info(MessageUtils.getMessage("IPROCESS-100154", resourceId, tag.getKey(), tag.getValue())); } } }
public void tagResources(List<String> resources, List<Tag> tags) { CreateTagsRequest createTagsRequest = new CreateTagsRequest(); createTagsRequest.setResources(resources); createTagsRequest.setTags(tags); try { ec2.createTags(createTagsRequest); } catch (AmazonServiceException e) { System.out.println("Error terminating instances"); System.out.println("Caught Exception: " + e.getMessage()); System.out.println("Reponse Status Code: " + e.getStatusCode()); System.out.println("Error Code: " + e.getErrorCode()); System.out.println("Request ID: " + e.getRequestId()); } }
@Override public Void call() { LOG.debug("setting tags {} on resources {}", this.tags, this.resourceIds); CreateTagsRequest request = new CreateTagsRequest(this.resourceIds, this.tags); getClient().getApi().createTags(request); return null; }
/** * Tags the specified resource, eg. a VM or volume (basically, it gives it a name). * @param resourceId The ID of the resource to tag * @param tagName The resource's name * @return true if the tag was done, false otherwise */ private boolean tagResource(String resourceId, String tagName) { boolean result = false; if(! Utils.isEmptyOrWhitespaces(tagName)) { Tag tag = new Tag( "Name", tagName ); CreateTagsRequest ctr = new CreateTagsRequest(Collections.singletonList(resourceId), Arrays.asList( tag )); try { this.ec2Api.createTags( ctr ); } catch(Exception e) { this.logger.warning("Error tagging resource " + resourceId + " with name=" + tagName + ": " + e); } result = true; } return result; }
@Override public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest request, ResultCapture<Void> extractor) { ActionResult result = resource.performAction("CreateTags", request, extractor); if (result == null) return null; return CodecUtils.transform(result.getResources(), TagImpl.CODEC); }
@Override public List<com.amazonaws.resources.ec2.Tag> createTags(List<Tag> tags, ResultCapture<Void> extractor) { CreateTagsRequest request = new CreateTagsRequest() .withTags(tags); return createTags(request, extractor); }