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;
}
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);
}
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);
}
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;
}
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);
}
Aggregations