Search in sources :

Example 31 with Ec2Client

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

the class StartStopInstance method startInstance.

// snippet-start:[ec2.java2.start_stop_instance.start]
public static void startInstance(Ec2Client ec2, String instanceId) {
    StartInstancesRequest request = StartInstancesRequest.builder().instanceIds(instanceId).build();
    ec2.startInstances(request);
    System.out.printf("Successfully started instance %s", instanceId);
}
Also used : StartInstancesRequest(software.amazon.awssdk.services.ec2.model.StartInstancesRequest)

Example 32 with Ec2Client

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

the class StartStopInstance method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "   <instanceId>\n\n" + "Where:\n" + "   instanceId - an instance id value that you can obtain from the AWS Console. \n\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String instanceId = args[0];
    boolean start;
    Region region = Region.US_WEST_2;
    Ec2Client ec2 = Ec2Client.builder().region(region).build();
    if (args[1].equals("start")) {
        start = true;
    } else {
        start = false;
    }
    if (start) {
        startInstance(ec2, instanceId);
    } else {
        stopInstance(ec2, instanceId);
    }
    ec2.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) Ec2Client(software.amazon.awssdk.services.ec2.Ec2Client)

Example 33 with Ec2Client

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

the class AllocateAddress method getAllocateAddress.

// snippet-start:[ec2.java2.allocate_address.main]
public static String getAllocateAddress(Ec2Client ec2, String instanceId) {
    try {
        AllocateAddressRequest allocateRequest = AllocateAddressRequest.builder().domain(DomainType.VPC).build();
        AllocateAddressResponse allocateResponse = ec2.allocateAddress(allocateRequest);
        String allocationId = allocateResponse.allocationId();
        AssociateAddressRequest associateRequest = AssociateAddressRequest.builder().instanceId(instanceId).allocationId(allocationId).build();
        AssociateAddressResponse associateResponse = ec2.associateAddress(associateRequest);
        return associateResponse.associationId();
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : AssociateAddressRequest(software.amazon.awssdk.services.ec2.model.AssociateAddressRequest) AssociateAddressResponse(software.amazon.awssdk.services.ec2.model.AssociateAddressResponse) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) AllocateAddressResponse(software.amazon.awssdk.services.ec2.model.AllocateAddressResponse) AllocateAddressRequest(software.amazon.awssdk.services.ec2.model.AllocateAddressRequest)

Example 34 with Ec2Client

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

the class CreateInstance method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "   <name> <amiId>\n\n" + "Where:\n" + "   name - an instance name value that you can obtain from the AWS Console (for example, ami-xxxxxx5c8b987b1a0). \n\n" + "   amiId - an Amazon Machine Image (AMI) value that you can obtain from the AWS Console (for example, i-xxxxxx2734106d0ab). \n\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String name = args[0];
    String amiId = args[1];
    Region region = Region.US_WEST_2;
    Ec2Client ec2 = Ec2Client.builder().region(region).build();
    String instanceId = createEC2Instance(ec2, name, amiId);
    System.out.println("The Amazon EC2 Instance ID is " + instanceId);
    ec2.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) Ec2Client(software.amazon.awssdk.services.ec2.Ec2Client)

Example 35 with Ec2Client

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

the class CreateSecurityGroup method createEC2SecurityGroup.

// snippet-start:[ec2.java2.create_security_group.main]
public static String createEC2SecurityGroup(Ec2Client ec2, String groupName, String groupDesc, String vpcId) {
    try {
        // snippet-start:[ec2.java2.create_security_group.create]
        CreateSecurityGroupRequest createRequest = CreateSecurityGroupRequest.builder().groupName(groupName).description(groupDesc).vpcId(vpcId).build();
        CreateSecurityGroupResponse resp = ec2.createSecurityGroup(createRequest);
        // snippet-end:[ec2.java2.create_security_group.create]
        // snippet-start:[ec2.java2.create_security_group.config]
        IpRange ipRange = IpRange.builder().cidrIp("0.0.0.0/0").build();
        IpPermission ipPerm = IpPermission.builder().ipProtocol("tcp").toPort(80).fromPort(80).ipRanges(ipRange).build();
        IpPermission ipPerm2 = IpPermission.builder().ipProtocol("tcp").toPort(22).fromPort(22).ipRanges(ipRange).build();
        AuthorizeSecurityGroupIngressRequest authRequest = AuthorizeSecurityGroupIngressRequest.builder().groupName(groupName).ipPermissions(ipPerm, ipPerm2).build();
        AuthorizeSecurityGroupIngressResponse authResponse = ec2.authorizeSecurityGroupIngress(authRequest);
        System.out.printf("Successfully added ingress policy to Security Group %s", groupName);
        return resp.groupId();
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : IpRange(software.amazon.awssdk.services.ec2.model.IpRange) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) AuthorizeSecurityGroupIngressRequest(software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressRequest) IpPermission(software.amazon.awssdk.services.ec2.model.IpPermission) AuthorizeSecurityGroupIngressResponse(software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressResponse) CreateSecurityGroupResponse(software.amazon.awssdk.services.ec2.model.CreateSecurityGroupResponse) CreateSecurityGroupRequest(software.amazon.awssdk.services.ec2.model.CreateSecurityGroupRequest)

Aggregations

Ec2Client (software.amazon.awssdk.services.ec2.Ec2Client)21 Region (software.amazon.awssdk.regions.Region)20 Ec2Exception (software.amazon.awssdk.services.ec2.model.Ec2Exception)16 Test (org.junit.Test)5 EC2Client (software.amazon.awssdk.services.ec2.EC2Client)5 Injector (com.google.inject.Injector)2 DescribeInstancesRequest (software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest)2 DescribeInstancesResponse (software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse)2 Instance (software.amazon.awssdk.services.ec2.model.Instance)2 Reservation (software.amazon.awssdk.services.ec2.model.Reservation)2 AbstractModule (com.google.inject.AbstractModule)1 Module (com.google.inject.Module)1 ListIterator (java.util.ListIterator)1 EC2Client (org.jclouds.aws.ec2.EC2Client)1 EC2AsyncClient (org.jclouds.ec2.EC2AsyncClient)1 EC2Client (org.jclouds.ec2.EC2Client)1 KeyPair (org.jclouds.ec2.domain.KeyPair)1 RunningInstance (org.jclouds.ec2.domain.RunningInstance)1 AccountAttribute (software.amazon.awssdk.services.ec2.model.AccountAttribute)1 AccountAttributeValue (software.amazon.awssdk.services.ec2.model.AccountAttributeValue)1