Skip to content

Commit

Permalink
Merge pull request #1138 from scmacdon/master
Browse files Browse the repository at this point in the history
Update EC2 Java V2 Code examples
  • Loading branch information
Laren-AWS committed Apr 28, 2020
2 parents 0e77ebb + 3f01a4c commit b8c3f22
Show file tree
Hide file tree
Showing 22 changed files with 529 additions and 571 deletions.
5 changes: 5 additions & 0 deletions javav2/example_code/ec2/metadata.yaml
Expand Up @@ -57,4 +57,9 @@ files:
- path: src/main/java/com/example/ec2/StartStopInstance.java
services:
- ec2
- path: src/main/java/com/example/ec2/TerminateInstance.java
services:
- ec2
- path: src/test/java/AWSEC2ServiceIntegrationTest.java
services:
- ec2
@@ -1,12 +1,12 @@
//snippet-sourcedescription:[AllocateAddress.java demonstrates how to allocate an elastic IP address for an EC2 instance.]
//snippet-sourcedescription:[AllocateAddress.java demonstrates how to allocate an elastic IP address for an Amazon EC2 instance.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[ec2]
//snippet-service:[Amazon EC2]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[2020/02/11]
//snippet-sourcedate:[2/11/2020]
//snippet-sourceauthor:[scmacdon]
/*
* Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand All @@ -20,9 +20,9 @@
* permissions and limitations under the License.
*/
package com.example.ec2;
// snippet-start:[ec2.java2.allocate_address.complete]

// snippet-start:[ec2.java2.allocate_address.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.AllocateAddressRequest;
import software.amazon.awssdk.services.ec2.model.DomainType;
Expand All @@ -48,8 +48,17 @@ public static void main(String[] args) {
}

String instanceId = args[0];
// snippet-start:[ec2.java2.allocate_address.main]
Ec2Client ec2 = Ec2Client.create();

Region region = Region.US_EAST_1;
Ec2Client ec2 = Ec2Client.builder()
.region(region)
.build();

System.out.println(getAllocateAddress(ec2, instanceId));
}

// snippet-start:[ec2.java2.allocate_address.main]
public static String getAllocateAddress( Ec2Client ec2, String instanceId) {

try {
AllocateAddressRequest allocateRequest = AllocateAddressRequest.builder()
Expand All @@ -67,19 +76,14 @@ public static void main(String[] args) {
.allocationId(allocationId)
.build();

AssociateAddressResponse associateResponse =
ec2.associateAddress(associateRequest);
AssociateAddressResponse associateResponse = ec2.associateAddress(associateRequest);
return associateResponse.associationId();

// snippet-end:[ec2.java2.allocate_address.main]
System.out.printf(
"Successfully associated Elastic IP address %s " +
"with instance %s",
associateResponse.associationId(),
instanceId);
} catch (Ec2Exception e) {
e.getStackTrace();
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
// snippet-end:[ec2.java2.allocate_address.main]
}
}

// snippet-end:[ec2.java2.allocate_address.complete]
@@ -1,12 +1,13 @@
//snippet-sourcedescription:[CreateInstance.java demonstrates how to create an EC2 instance.]
//snippet-sourcedescription:[CreateInstance.java demonstrates how to create an Amazon EC2 instance.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[ec2]
//snippet-service:[Amazon EC2]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[11/02/2020]
//snippet-sourcedate:[2/15/2020]
//snippet-sourceauthor:[scmacdon]

/*
* Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand All @@ -20,9 +21,9 @@
* permissions and limitations under the License.
*/
package com.example.ec2;
// snippet-start:[ec2.java2.create_instance.complete]

// snippet-start:[ec2.java2.create_instance.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.InstanceType;
import software.amazon.awssdk.services.ec2.model.RunInstancesRequest;
Expand Down Expand Up @@ -50,8 +51,17 @@ public static void main(String[] args) {
String name = args[0];
String amiId = args[1];

// snippet-start:[ec2.java2.create_instance.main]
Ec2Client ec2 = Ec2Client.create();
Region region = Region.US_WEST_2;
Ec2Client ec2 = Ec2Client.builder()
.region(region)
.build();

String instanceId = createEC2Instance(ec2,name, amiId) ;
System.out.println("The instance ID is "+instanceId);
}

// snippet-start:[ec2.java2.create_instance.main]
public static String createEC2Instance(Ec2Client ec2,String name, String amiId ) {

RunInstancesRequest runRequest = RunInstancesRequest.builder()
.imageId(amiId)
Expand All @@ -61,7 +71,6 @@ public static void main(String[] args) {
.build();

RunInstancesResponse response = ec2.runInstances(runRequest);

String instanceId = response.instances().get(0).instanceId();

Tag tag = Tag.builder()
Expand All @@ -79,13 +88,14 @@ public static void main(String[] args) {
System.out.printf(
"Successfully started EC2 instance %s based on AMI %s",
instanceId, amiId);


return instanceId;

} catch (Ec2Exception e) {
System.err.println(e.getMessage());
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
// snippet-end:[ec2.java2.create_instance.main]
System.out.println("Done!");
return "";
}
}
// snippet-end:[ec2.java2.create_instance.complete]
@@ -1,12 +1,12 @@
//snippet-sourcedescription:[CreateKeyPair.java demonstrates how to create an EC2 key pair.]
//snippet-sourcedescription:[CreateKeyPair.java demonstrates how to create an Amazon EC2 key pair.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[ec2]
//snippet-service:[Amazon EC2]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[11/02/2020]
//snippet-sourcedate:[2/11/2020]
//snippet-sourceauthor:[scmacdon]
/*
* Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand All @@ -20,8 +20,9 @@
* permissions and limitations under the License.
*/
package com.example.ec2;
// snippet-start:[ec2.java2.create_key_pair.complete]

// snippet-start:[ec2.java2.create_key_pair.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.CreateKeyPairRequest;
import software.amazon.awssdk.services.ec2.model.CreateKeyPairResponse;
Expand All @@ -44,25 +45,32 @@ public static void main(String[] args) {
}

String keyName = args[0];
// snippet-start:[ec2.java2.create_key_pair.main]

Ec2Client ec2 = Ec2Client.create();
//Create an Ec2Client object
Region region = Region.US_WEST_2;
Ec2Client ec2 = Ec2Client.builder()
.region(region)
.build();

try {
createEC2KeyPair(ec2, keyName) ;
}

// snippet-start:[ec2.java2.create_key_pair.main]
public static void createEC2KeyPair(Ec2Client ec2,String keyName ) {

try {
CreateKeyPairRequest request = CreateKeyPairRequest.builder()
.keyName(keyName).build();

CreateKeyPairResponse response = ec2.createKeyPair(request);

System.out.printf(
"Successfully created key pair named %s",
keyName);

} catch (Ec2Exception e) {
e.getStackTrace();
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
// snippet-end:[ec2.java2.create_key_pair.main]
}
}
// snippet-end:[ec2.java2.create_key_pair.complete]
@@ -1,12 +1,13 @@
//snippet-sourcedescription:[CreateSecurityGroup.java demonstrates how to create an EC2 security group.]
//snippet-sourcedescription:[CreateSecurityGroup.java demonstrates how to create an Amazon EC2 security group.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[ec2]
//snippet-service:[Amazon EC2]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[11/02/2020]
//snippet-sourcedate:[2/11/2020]
//snippet-sourceauthor:[scmacdon]

/*
* Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand All @@ -20,15 +21,16 @@
* permissions and limitations under the License.
*/
package com.example.ec2;
// snippet-start:[ec2.java2.create_security_group.complete]

// snippet-start:[ec2.java2.create_security_group.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.CreateSecurityGroupRequest;
import software.amazon.awssdk.services.ec2.model.CreateSecurityGroupResponse;
import software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressRequest;
import software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressResponse;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
import software.amazon.awssdk.services.ec2.model.IpPermission;
import software.amazon.awssdk.services.ec2.model.CreateSecurityGroupResponse;
import software.amazon.awssdk.services.ec2.model.IpRange;
// snippet-end:[ec2.java2.create_security_group.import]

Expand All @@ -51,29 +53,33 @@ public static void main(String[] args) {
String groupDesc = args[1];
String vpcId = args[2];

// snippet-start:[ec2.java2.create_security_group.main]
//Create an Ec2Client object
// snippet-start:[ec2.java2.create_security_group.client]
Ec2Client ec2 = Ec2Client.create();
Region region = Region.US_WEST_2;
Ec2Client ec2 = Ec2Client.builder()
.region(region)
.build();
// snippet-end:[ec2.java2.create_security_group.client]


String id = createEC2SecurityGroup(ec2, groupName, groupDesc, vpcId);
System.out.printf(
"Successfully created security group with this ID %s",
id);
}

// snippet-start:[ec2.java2.create_security_group.main]
public static String createEC2SecurityGroup( Ec2Client ec2,String groupName, String groupDesc, String vpcId) {
try {

// snippet-start:[ec2.java2.create_security_group.create]
CreateSecurityGroupRequest createRequest = CreateSecurityGroupRequest.builder()
.groupName(groupName)
.description(groupDesc)
.vpcId(vpcId)
.build();

CreateSecurityGroupResponse createResponse =
ec2.createSecurityGroup(createRequest);
// snippet-end:[ec2.java2.create_security_group.create]

System.out.printf(
"Successfully created security group named %s",
groupName);
CreateSecurityGroupResponse resp= ec2.createSecurityGroup(createRequest);

// snippet-start:[ec2.java2.create_security_group.config]
IpRange ipRange = IpRange.builder()
.cidrIp("0.0.0.0/0").build();

Expand All @@ -82,7 +88,6 @@ public static void main(String[] args) {
.toPort(80)
.fromPort(80)
.ipRanges(ipRange)
// .ipv4Ranges(ip_range)
.build();

IpPermission ipPerm2 = IpPermission.builder()
Expand All @@ -99,18 +104,20 @@ public static void main(String[] args) {
.build();

AuthorizeSecurityGroupIngressResponse authResponse =
ec2.authorizeSecurityGroupIngress(authRequest);
ec2.authorizeSecurityGroupIngress(authRequest);

// snippet-end:[ec2.java2.create_security_group.config]
// snippet-end:[ec2.java2.create_security_group.main]
System.out.printf(
"Successfully added ingress policy to security group %s",
groupName);

return resp.groupId();

} catch (Ec2Exception e) {
e.getStackTrace();
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
}

// snippet-end:[ec2.java2.create_security_group.complete]

0 comments on commit b8c3f22

Please sign in to comment.