Search in sources :

Example 6 with CreateTagsRequest

use of software.amazon.awssdk.services.ec2.model.CreateTagsRequest in project aws-doc-sdk-examples by awsdocs.

the class CreateInstance method createEC2Instance.

// snippet-start:[ec2.java2.create_instance.main]
public static String createEC2Instance(Ec2Client ec2, String name, String amiId) {
    RunInstancesRequest runRequest = RunInstancesRequest.builder().imageId(amiId).instanceType(InstanceType.T1_MICRO).maxCount(1).minCount(1).build();
    RunInstancesResponse response = ec2.runInstances(runRequest);
    String instanceId = response.instances().get(0).instanceId();
    Tag tag = Tag.builder().key("Name").value(name).build();
    CreateTagsRequest tagRequest = CreateTagsRequest.builder().resources(instanceId).tags(tag).build();
    try {
        ec2.createTags(tagRequest);
        System.out.printf("Successfully started EC2 Instance %s based on AMI %s", instanceId, amiId);
        return instanceId;
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : RunInstancesResponse(software.amazon.awssdk.services.ec2.model.RunInstancesResponse) CreateTagsRequest(software.amazon.awssdk.services.ec2.model.CreateTagsRequest) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) RunInstancesRequest(software.amazon.awssdk.services.ec2.model.RunInstancesRequest) Tag(software.amazon.awssdk.services.ec2.model.Tag)

Example 7 with CreateTagsRequest

use of software.amazon.awssdk.services.ec2.model.CreateTagsRequest in project GNS by MobilityFirst.

the class AWSEC2 method addInstanceTag.

/**
   * Adds the key and value as a 'tag' for the instance.
   *
   * @param ec2
   * @param createdInstanceId
   * @param key
   * @param value
   */
public static void addInstanceTag(AmazonEC2 ec2, String createdInstanceId, String key, String value) {
    List<String> resources = new LinkedList<>();
    resources.add(createdInstanceId);
    List<Tag> tags = new LinkedList<>();
    Tag nameTag = new Tag(key, value);
    tags.add(nameTag);
    CreateTagsRequest ctr = new CreateTagsRequest(resources, tags);
    ec2.createTags(ctr);
}
Also used : CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 8 with CreateTagsRequest

use of software.amazon.awssdk.services.ec2.model.CreateTagsRequest in project photon-model by vmware.

the class AWSUtils method tagResources.

/**
 * 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);
}
Also used : CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest)

Example 9 with CreateTagsRequest

use of software.amazon.awssdk.services.ec2.model.CreateTagsRequest in project cloudbreak by hortonworks.

the class AwsMetadataCollector method addTag.

private void addTag(AmazonEC2 amazonEC2Client, CloudInstance cloudInstance, Instance instance) {
    String tagName = awsClient.getCbName(cloudInstance.getTemplate().getGroupName(), cloudInstance.getTemplate().getPrivateId());
    Tag t = new Tag();
    t.setKey(TAG_NAME);
    t.setValue(tagName);
    CreateTagsRequest ctr = new CreateTagsRequest();
    ctr.setTags(Collections.singletonList(t));
    ctr.withResources(instance.getInstanceId());
    amazonEC2Client.createTags(ctr);
}
Also used : CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag)

Aggregations

CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)8 Tag (com.amazonaws.services.ec2.model.Tag)6 CreateTagsResult (com.amazonaws.services.ec2.model.CreateTagsResult)3 LinkedList (java.util.LinkedList)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)1 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)1 Collection (java.util.Collection)1 Message (org.apache.camel.Message)1 CreateTagsRequest (software.amazon.awssdk.services.ec2.model.CreateTagsRequest)1 Ec2Exception (software.amazon.awssdk.services.ec2.model.Ec2Exception)1 RunInstancesRequest (software.amazon.awssdk.services.ec2.model.RunInstancesRequest)1 RunInstancesResponse (software.amazon.awssdk.services.ec2.model.RunInstancesResponse)1 Tag (software.amazon.awssdk.services.ec2.model.Tag)1