You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.2 KiB

7 years ago
  1. package verteiltesysteme.aws;
  2. import java.nio.file.Files;
  3. import java.nio.file.Paths;
  4. import java.util.Base64;
  5. import com.amazonaws.AmazonClientException;
  6. import com.amazonaws.auth.profile.ProfileCredentialsProvider;
  7. import com.amazonaws.services.ec2.AmazonEC2;
  8. import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
  9. import com.amazonaws.services.ec2.model.RunInstancesRequest;
  10. import com.amazonaws.services.ec2.model.RunInstancesResult;
  11. /**
  12. *
  13. * In order to use the services in this sample, you need:
  14. *
  15. * - A valid Amazon Web Services account. You can register for AWS at:
  16. * https://aws-portal.amazon.com/gp/aws/developer/registration/index.html
  17. *
  18. * - Your account's Access Key ID and Secret Access Key:
  19. * http://aws.amazon.com/security-credentials
  20. *
  21. * - A subscription to Amazon EC2. You can sign up for EC2 at:
  22. * http://aws.amazon.com/ec2/
  23. *
  24. */
  25. public class StartVertSysServersTokyo {
  26. /*
  27. * Before running the code: Fill in your AWS access credentials in the provided
  28. * credentials file template, and be sure to move the file to the default
  29. * location (C:\\Users\\<username>\\.aws\\credentials) where the sample code will
  30. * load the credentials from.
  31. * https://console.aws.amazon.com/iam/home?#security_credential
  32. *
  33. * WARNING: To avoid accidental leakage of your credentials, DO NOT keep the
  34. * credentials file in your source directory.
  35. */
  36. public static void main(String[] args) throws Exception {
  37. System.out.println("===========================================");
  38. System.out.println("Verteilte Systeme AWS Demo (AWS Java SDK)");
  39. System.out.println("===========================================");
  40. /*
  41. * The ProfileCredentialsProvider will return your [default] credential profile
  42. * by reading from the credentials file located at
  43. * (C:\\Users\\<username>\\.aws\\credentials).
  44. */
  45. ProfileCredentialsProvider credentialsProvider = new ProfileCredentialsProvider();
  46. try {
  47. credentialsProvider.getCredentials();
  48. } catch (Exception e) {
  49. throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
  50. + "Please make sure that your credentials file is at the correct "
  51. + "location (C:\\Users\\<username>\\.aws\\credentials), and is in valid format.", e);
  52. }
  53. // Tokyo
  54. System.out.println("Instantiating EC2 Client for Tokyo...");
  55. AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(credentialsProvider)
  56. .withRegion("ap-northeast-1").build();
  57. System.out.println("Loading user-data from file...");
  58. byte[] encoded = Files.readAllBytes(Paths.get("src\\verteiltesysteme\\aws\\user-data.txt"));
  59. String userData = Base64.getEncoder().encodeToString(encoded);
  60. System.out.println("Starting instance in Tokyo...");
  61. RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
  62. // Amazon Linux 2 LTS Candidate AMI 2017.12.0 (HVM), SSD Volume Type - ami-6be57d0d
  63. runInstancesRequest.withImageId("ami-6be57d0d").withInstanceType("t2.nano").withMinCount(1).withMaxCount(1)
  64. .withKeyName("srieger-amazon-aws-keypair").withUserData(userData)
  65. .withSubnetId("subnet-1b8de752")
  66. .withSecurityGroupIds("sg-12e96e6b");
  67. RunInstancesResult result = ec2.runInstances(runInstancesRequest);
  68. System.out.println(result);
  69. }
  70. }