use of software.amazon.awssdk.services.ec2.model.Ec2Exception 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 "";
}
use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.
the class DescribeInstances method describeEC2Instances.
// snippet-start:[ec2.java2.describe_instances.main]
public static void describeEC2Instances(Ec2Client ec2) {
boolean done = false;
String nextToken = null;
try {
do {
DescribeInstancesRequest request = DescribeInstancesRequest.builder().maxResults(6).nextToken(nextToken).build();
DescribeInstancesResponse response = ec2.describeInstances(request);
for (Reservation reservation : response.reservations()) {
for (Instance instance : reservation.instances()) {
System.out.println("Instance Id is " + instance.instanceId());
System.out.println("Image id is " + instance.imageId());
System.out.println("Instance type is " + instance.instanceType());
System.out.println("Instance state name is " + instance.state().name());
System.out.println("monitoring information is " + instance.monitoring().state());
}
}
nextToken = response.nextToken();
} while (nextToken != null);
} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.
the class FindRunningInstances method findRunningEC2Instances.
// snippet-start:[ec2.java2.running_instances.main]
public static void findRunningEC2Instances(Ec2Client ec2) {
try {
String nextToken = null;
do {
Filter filter = Filter.builder().name("instance-state-name").values("running").build();
DescribeInstancesRequest request = DescribeInstancesRequest.builder().filters(filter).build();
DescribeInstancesResponse response = ec2.describeInstances(request);
for (Reservation reservation : response.reservations()) {
for (Instance instance : reservation.instances()) {
System.out.printf("Found Reservation with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.instanceId(), instance.imageId(), instance.instanceType(), instance.state().name(), instance.monitoring().state());
System.out.println("");
}
}
nextToken = response.nextToken();
} while (nextToken != null);
} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.
the class DescribeRegionsAndZones method describeEC2RegionsAndZones.
// snippet-start:[ec2.java2.describe_region_and_zones.main]
public static void describeEC2RegionsAndZones(Ec2Client ec2) {
// snippet-start:[ec2.java2.describe_region_and_zones.region]
try {
DescribeRegionsResponse regionsResponse = ec2.describeRegions();
for (Region region : regionsResponse.regions()) {
System.out.printf("Found Region %s " + "with endpoint %s", region.regionName(), region.endpoint());
System.out.println();
// snippet-end:[ec2.java2.describe_region_and_zones.region]
}
// snippet-start:[ec2.java2.describe_region_and_zones.avail_zone]
DescribeAvailabilityZonesResponse zonesResponse = ec2.describeAvailabilityZones();
for (AvailabilityZone zone : zonesResponse.availabilityZones()) {
System.out.printf("Found Availability Zone %s " + "with status %s " + "in region %s", zone.zoneName(), zone.state(), zone.regionName());
System.out.println();
// snippet-end:[ec2.java2.describe_region_and_zones.avail_zone]
}
} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.
the class DescribeSecurityGroups method describeEC2SecurityGroups.
// snippet-start:[ec2.java2.describe_security_groups.main]
public static void describeEC2SecurityGroups(Ec2Client ec2, String groupId) {
try {
DescribeSecurityGroupsRequest request = DescribeSecurityGroupsRequest.builder().groupIds(groupId).build();
DescribeSecurityGroupsResponse response = ec2.describeSecurityGroups(request);
for (SecurityGroup group : response.securityGroups()) {
System.out.printf("Found Security Group with id %s, " + "vpc id %s " + "and description %s", group.groupId(), group.vpcId(), group.description());
}
} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
Aggregations