Search in sources :

Example 6 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method createKeyPair.

/**
   * Create a New Key Pair
   *
   * @param ec2
   * @param name
   * @return the keypair
   */
public static KeyPair createKeyPair(AmazonEC2 ec2, String name) {
    CreateKeyPairRequest newKeyRequest = new CreateKeyPairRequest();
    newKeyRequest.setKeyName(name);
    CreateKeyPairResult keyresult = ec2.createKeyPair(newKeyRequest);
    /**
     * **********************print the properties of this key****************
     */
    KeyPair keyPair = keyresult.getKeyPair();
    System.out.println("The key we created is = " + keyPair.toString());
    /**
     * ***************store the key in a .pem file ***************
     */
    try {
        String fileName = KEYHOME + FILESEPARATOR + name + PRIVATEKEYFILEEXTENSION;
        File distFile = new File(fileName);
        BufferedReader bufferedReader = new BufferedReader(new StringReader(keyPair.getKeyMaterial()));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(distFile));
        char[] buf = new char[1024];
        int len;
        while ((len = bufferedReader.read(buf)) != -1) {
            bufferedWriter.write(buf, 0, len);
        }
        bufferedWriter.flush();
        bufferedReader.close();
        bufferedWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return keyPair;
}
Also used : KeyPair(com.amazonaws.services.ec2.model.KeyPair) CreateKeyPairResult(com.amazonaws.services.ec2.model.CreateKeyPairResult) FileWriter(java.io.FileWriter) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException) File(java.io.File) CreateKeyPairRequest(com.amazonaws.services.ec2.model.CreateKeyPairRequest) BufferedWriter(java.io.BufferedWriter)

Example 7 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method stopInstance.

/**
   * Stop an instance
   *
   * @param ec2
   * @param createdInstanceId
   */
public static void stopInstance(AmazonEC2 ec2, String createdInstanceId) {
    System.out.println("Stopping Instance:" + createdInstanceId);
    List<String> instanceIds = new LinkedList<>();
    instanceIds.add(createdInstanceId);
    StopInstancesRequest stopIR = new StopInstancesRequest(instanceIds);
    ec2.stopInstances(stopIR);
}
Also used : StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) LinkedList(java.util.LinkedList)

Example 8 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method describeSecurityGroups.

/**
   * Describe Security Groups
   *
   * @param ec2
   */
public static void describeSecurityGroups(AmazonEC2 ec2) {
    StringBuilder output = new StringBuilder();
    String prefix = currentTab + "Security Groups: ";
    DescribeSecurityGroupsResult describeSecurityGroupsResult = ec2.describeSecurityGroups();
    for (SecurityGroup securityGroup : describeSecurityGroupsResult.getSecurityGroups()) {
        output.append(prefix);
        prefix = ", ";
        output.append(securityGroup.getGroupName());
    }
    System.out.println(output);
}
Also used : DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup)

Example 9 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method createSecurityGroup.

/**
   * Create a New Security Group with our standard permissions
   *
   * @param ec2
   * @param name
   * @return the name of the new group
   */
public static String createSecurityGroup(AmazonEC2 ec2, String name) {
    CreateSecurityGroupRequest securityGroupRequest = new CreateSecurityGroupRequest(name, name + " security group");
    ec2.createSecurityGroup(securityGroupRequest);
    AuthorizeSecurityGroupIngressRequest ingressRequest = new AuthorizeSecurityGroupIngressRequest();
    ingressRequest.setGroupName(name);
    List<IpPermission> permissions = new ArrayList<>();
    // open up ping (echo request)
    permissions.add(new IpPermission().withIpProtocol(ICMPPROTOCOL).withFromPort(ECHOTYPE).withToPort(WILDCARDCODE).withIpRanges(IPRANGESALL));
    permissions.add(new IpPermission().withIpProtocol(TCPPROTOCOL).withFromPort(SSHPORT).withToPort(SSHPORT).withIpRanges(IPRANGESALL));
    permissions.add(new IpPermission().withIpProtocol(TCPPROTOCOL).withFromPort(HTTPPORT).withToPort(HTTPPORT).withIpRanges(IPRANGESALL));
    permissions.add(new IpPermission().withIpProtocol(TCPPROTOCOL).withFromPort(HTTPNONROOTPORT).withToPort(HTTPNONROOTPORT).withIpRanges(IPRANGESALL));
    permissions.add(new IpPermission().withIpProtocol(TCPPROTOCOL).withFromPort(HTTPSPORT).withToPort(HTTPSPORT).withIpRanges(IPRANGESALL));
    permissions.add(new IpPermission().withIpProtocol(TCPPROTOCOL).withFromPort(MYSQLPORT).withToPort(MYSQLPORT).withIpRanges(IPRANGESALL));
    permissions.add(new IpPermission().withIpProtocol(TCPPROTOCOL).withFromPort(20000).withToPort(30000).withIpRanges(IPRANGESALL));
    permissions.add(new IpPermission().withIpProtocol(UDPPROTOCOL).withFromPort(20000).withToPort(30000).withIpRanges(IPRANGESALL));
    ingressRequest.setIpPermissions(permissions);
    ec2.authorizeSecurityGroupIngress(ingressRequest);
    return name;
}
Also used : ArrayList(java.util.ArrayList) AuthorizeSecurityGroupIngressRequest(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest) IpPermission(com.amazonaws.services.ec2.model.IpPermission) CreateSecurityGroupRequest(com.amazonaws.services.ec2.model.CreateSecurityGroupRequest)

Example 10 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method describeElasticIPs.

/**
   *
   * @param ec2
   */
public static void describeElasticIPs(AmazonEC2 ec2) {
    StringBuilder output = new StringBuilder();
    String prefix = currentTab + "Elastic IPs: ";
    DescribeAddressesResult describeAddressesResult = ec2.describeAddresses();
    for (Address address : describeAddressesResult.getAddresses()) {
        output.append(prefix);
        prefix = ", ";
        output.append(address.getPublicIp());
    }
    System.out.println(output);
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult)

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