Snippet Java for EC2

With snippet Java for EC2, you can free your mind

  1. Create Instance by code from AMI exist

    AmazonEC2 ec2 = new AmazonEC2Client(new ClasspathPropertiesFileCredentialsProvider());
    ec2.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1));
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
    runInstancesRequest.setInstanceType("m1.small");
    runInstancesRequest.setImageId("ami-115aaf15");
    runInstancesRequest.setMinCount(Integer.valueOf(1));
    runInstancesRequest.setMaxCount(Integer.valueOf(1));
    runInstancesRequest.setKeyName("keypair");
    ArrayList securityGroup = new ArrayList();
    securityGroup.add("sgrsslave");
    runInstancesRequest.setSecurityGroups(securityGroup);
    ec2.runInstances(runInstancesRequest);
    
  2. Get instance-id

    String ec2Id = "";
    String inputLine;
    URL url = new URL("http://169.254.169.254/latest/meta-data/instance-id");
    URLConnection con = url.openConnection();
    try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
      while ((inputLine = in.readLine()) != null) { ec2Id = inputLine; }
    }
    System.out.println(ec2Id);
    

Comments

Popular posts from this blog

Reduce TIME_WAIT Socket Connections