Skip to content

Commit

Permalink
move start instance
Browse files Browse the repository at this point in the history
  • Loading branch information
lor6 committed Mar 5, 2018
1 parent b346899 commit 572509f
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions aws/src/main/java/com/baeldung/ec2/EC2Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ public class EC2Application {

public static void main(String[] args) {

// 0) - Set up the client
// Set up the client
AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.US_EAST_1)
.build();

// 1) - Create a security group
// Create a security group
CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest().withGroupName("BaeldungSecurityGroup")
.withDescription("Baeldung Security Group");
ec2Client.createSecurityGroup(createSecurityGroupRequest);

// 2) - Allow HTTP and SSH traffic
// Allow HTTP and SSH traffic
IpRange ipRange1 = new IpRange().withCidrIp("0.0.0.0/0");

IpPermission ipPermission1 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 }))
Expand All @@ -69,19 +69,19 @@ public static void main(String[] args) {

ec2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);

// 3) - Create KeyPair
// Create KeyPair
CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest()
.withKeyName("baeldung-key-pair");
CreateKeyPairResult createKeyPairResult = ec2Client.createKeyPair(createKeyPairRequest);
String privateKey = createKeyPairResult
.getKeyPair()
.getKeyMaterial(); // make sure you keep it, the private key, Amazon doesn't store the private key

// 4) - See what key-pairs you've got
// See what key-pairs you've got
DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
DescribeKeyPairsResult describeKeyPairsResult = ec2Client.describeKeyPairs(describeKeyPairsRequest);

// 5) - Launch an Amazon Instance
// Launch an Amazon Instance
RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId("ami-97785bed") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html | https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/usingsharedamis-finding.html
.withInstanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
.withMinCount(1)
Expand All @@ -91,7 +91,13 @@ public static void main(String[] args) {

String yourInstanceId = ec2Client.runInstances(runInstancesRequest).getReservation().getInstances().get(0).getInstanceId();

// 6) Monitor Instances
// Start an Instance
StartInstancesRequest startInstancesRequest = new StartInstancesRequest()
.withInstanceIds(yourInstanceId);

ec2Client.startInstances(startInstancesRequest);

// Monitor Instances
MonitorInstancesRequest monitorInstancesRequest = new MonitorInstancesRequest()
.withInstanceIds(yourInstanceId);

Expand All @@ -102,14 +108,14 @@ public static void main(String[] args) {

ec2Client.unmonitorInstances(unmonitorInstancesRequest);

// 7) - Reboot an Instance
// Reboot an Instance

RebootInstancesRequest rebootInstancesRequest = new RebootInstancesRequest()
.withInstanceIds(yourInstanceId);

ec2Client.rebootInstances(rebootInstancesRequest);

// 8) - Stop an Instance
// Stop an Instance
StopInstancesRequest stopInstancesRequest = new StopInstancesRequest()
.withInstanceIds(yourInstanceId);

Expand All @@ -119,13 +125,7 @@ public static void main(String[] args) {
.getPreviousState()
.getName();

// 9) - Start an Instance
StartInstancesRequest startInstancesRequest = new StartInstancesRequest()
.withInstanceIds(yourInstanceId);

ec2Client.startInstances(startInstancesRequest);

// 10) - Describe an Instance
// Describe an Instance
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
DescribeInstancesResult response = ec2Client.describeInstances(describeInstancesRequest);
System.out.println(response.getReservations()
Expand Down

0 comments on commit 572509f

Please sign in to comment.