Search in sources :

Example 41 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class CreateInstance method main.

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 reservation_id = run_response.getReservation().getInstances().get(0).getInstanceId();
    Tag tag = new Tag().withKey("Name").withValue(name);
    CreateTagsRequest tag_request = new CreateTagsRequest().withResources(reservation_id).withTags(tag);
    CreateTagsResult tag_response = ec2.createTags(tag_request);
    System.out.printf("Successfully started EC2 instance %s based on AMI %s", reservation_id, ami_id);
}
Also used : CreateTagsResult(com.amazonaws.services.ec2.model.CreateTagsResult) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) Tag(com.amazonaws.services.ec2.model.Tag)

Example 42 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class CreateSecurityGroup method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a group name, group description and vpc id\n" + "Ex: CreateSecurityGroup <group-name> <group-description> <vpc-id>\n";
    if (args.length != 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String group_name = args[0];
    String group_desc = args[1];
    String vpc_id = args[2];
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    CreateSecurityGroupRequest create_request = new CreateSecurityGroupRequest().withGroupName(group_name).withDescription(group_desc).withVpcId(vpc_id);
    CreateSecurityGroupResult create_response = ec2.createSecurityGroup(create_request);
    System.out.printf("Successfully created security group named %s", group_name);
    IpRange ip_range = new IpRange().withCidrIp("0.0.0.0/0");
    IpPermission ip_perm = new IpPermission().withIpProtocol("tcp").withToPort(80).withFromPort(80).withIpv4Ranges(ip_range);
    IpPermission ip_perm2 = new IpPermission().withIpProtocol("tcp").withToPort(22).withFromPort(22).withIpv4Ranges(ip_range);
    AuthorizeSecurityGroupIngressRequest auth_request = new AuthorizeSecurityGroupIngressRequest().withGroupName(group_name).withIpPermissions(ip_perm, ip_perm2);
    AuthorizeSecurityGroupIngressResult auth_response = ec2.authorizeSecurityGroupIngress(auth_request);
    System.out.printf("Successfully added ingress policy to security group %s", group_name);
}
Also used : IpRange(com.amazonaws.services.ec2.model.IpRange) AuthorizeSecurityGroupIngressResult(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressResult) CreateSecurityGroupResult(com.amazonaws.services.ec2.model.CreateSecurityGroupResult) AuthorizeSecurityGroupIngressRequest(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) IpPermission(com.amazonaws.services.ec2.model.IpPermission) CreateSecurityGroupRequest(com.amazonaws.services.ec2.model.CreateSecurityGroupRequest)

Example 43 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class DeleteSecurityGroup method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a security group id\n" + "Ex: DeleteSecurityGroup <security-group-id>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String group_id = args[0];
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    DeleteSecurityGroupRequest request = new DeleteSecurityGroupRequest().withGroupId(group_id);
    DeleteSecurityGroupResult response = ec2.deleteSecurityGroup(request);
    System.out.printf("Successfully deleted security group with id %s", group_id);
}
Also used : DeleteSecurityGroupRequest(com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest) DeleteSecurityGroupResult(com.amazonaws.services.ec2.model.DeleteSecurityGroupResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Example 44 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class DescribeAddresses method main.

public static void main(String[] args) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    DescribeAddressesResult response = ec2.describeAddresses();
    for (Address address : response.getAddresses()) {
        System.out.printf("Found address with public IP %s, " + "domain %s, " + "allocation id %s " + "and NIC id %s", address.getPublicIp(), address.getDomain(), address.getAllocationId(), address.getNetworkInterfaceId());
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Example 45 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class DescribeKeyPairs method main.

public static void main(String[] args) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    DescribeKeyPairsResult response = ec2.describeKeyPairs();
    for (KeyPairInfo key_pair : response.getKeyPairs()) {
        System.out.printf("Found key pair with name %s " + "and fingerprint %s", key_pair.getKeyName(), key_pair.getKeyFingerprint());
    }
}
Also used : KeyPairInfo(com.amazonaws.services.ec2.model.KeyPairInfo) DescribeKeyPairsResult(com.amazonaws.services.ec2.model.DescribeKeyPairsResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)65 Filter (com.amazonaws.services.ec2.model.Filter)25 Instance (com.amazonaws.services.ec2.model.Instance)20 DescribeImagesRequest (com.amazonaws.services.ec2.model.DescribeImagesRequest)17 DescribeImagesResult (com.amazonaws.services.ec2.model.DescribeImagesResult)15 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)12 Reservation (com.amazonaws.services.ec2.model.Reservation)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)9 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)8 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)8 Tag (com.amazonaws.services.ec2.model.Tag)8 AWSCredentials (com.amazonaws.auth.AWSCredentials)6 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)6 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)5 DisassociateAddressRequest (com.amazonaws.services.ec2.model.DisassociateAddressRequest)5 ReleaseAddressRequest (com.amazonaws.services.ec2.model.ReleaseAddressRequest)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4