AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-11-22 · Documentation low

File: code-library/latest/ug/java_2_auto-scaling_code_examples.md

Summary

Updated documentation to use 'Amazon EC2 Auto Scaling' branding, simplified code examples by removing CloudFormation dependencies, added explicit parameters for launch template and VPC zone ID, and modified region/configuration defaults.

Security assessment

The changes primarily involve branding updates and code simplification. The removal of CloudFormation stack handling and hardcoded values (like AMI IDs) does not indicate a security fix but rather example restructuring. No specific security vulnerabilities or mitigations are mentioned in the diff.

Diff

diff --git a/code-library/latest/ug/java_2_auto-scaling_code_examples.md b/code-library/latest/ug/java_2_auto-scaling_code_examples.md
index fc571bcbf..ac9aabeb9 100644
--- a//code-library/latest/ug/java_2_auto-scaling_code_examples.md
+++ b//code-library/latest/ug/java_2_auto-scaling_code_examples.md
@@ -9 +9 @@ There are more AWS SDK examples available in the [AWS Doc SDK Examples](https://
-# Auto Scaling examples using SDK for Java 2.x
+# Amazon EC2 Auto Scaling examples using SDK for Java 2.x
@@ -11 +11 @@ There are more AWS SDK examples available in the [AWS Doc SDK Examples](https://
-The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Java 2.x with Auto Scaling.
+The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Java 2.x with Amazon EC2 Auto Scaling.
@@ -23 +23 @@ Each example includes a link to the complete source code, where you can find ins
-The following code examples show how to get started using Auto Scaling.
+The following code examples show how to get started using Amazon EC2 Auto Scaling.
@@ -142 +142 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        private static final String ROLES_STACK = "MyCdkAutoScaleStack";
+    
@@ -147 +147 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                        <groupName>
+                        <groupName> <launchTemplateName> <vpcZoneId>
@@ -150,0 +151,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+                        launchTemplateName - The name of the launch template.\s
+                        vpcZoneId - A subnet Id for a virtual private cloud (VPC) where instances in the Auto Scaling group can be created.
@@ -153 +155,9 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            String groupName = "MyAutoScalingGroup2";
+            //if (args.length != 3) {
+            //    System.out.println(usage);
+            //    System.exit(1);
+           // }
+    
+            String groupName = "Scott250" ; //rgs[0];
+            String launchTemplateName = "MyTemplate5" ;//args[1];
+            String vpcZoneId = "subnet-0ddc451b8a8a1aa44" ; //args[2];
+    
@@ -155 +165 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                    .region(Region.US_WEST_2)
+                    .region(Region.US_EAST_1)
@@ -158,3 +168 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            Ec2Client ec2 = Ec2Client.builder()
-                    .region(Region.US_WEST_2)
-                    .build();
+            Ec2Client ec2 = Ec2Client.create();
@@ -167,8 +174,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            System.out.println("First, we will create a launch template using a CloudFormation script");
-            CloudFormationHelper.deployCloudFormationStack(ROLES_STACK);
-            Map<String, String> stackOutputs = CloudFormationHelper.getStackOutputsAsync(ROLES_STACK).join();
-            String launchTemplateName = stackOutputs.get("LaunchTemplateNameOutput");
-            String vpcZoneId = getVPC(ec2);
-            updateTemlate(ec2, launchTemplateName );
-            System.out.println("The VPC zone id created by the CloudFormation stack is"+vpcZoneId);
-    
@@ -176,2 +176 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            createAutoScalingGroup(autoScalingClient, ec2, groupName, launchTemplateName, vpcZoneId);
-    
+            createAutoScalingGroup(autoScalingClient, groupName, launchTemplateName, vpcZoneId);
@@ -248,2 +247 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            System.out.println("13. Delete the Auto Scaling group and cloud formation resources");
-            CloudFormationHelper.destroyCloudFormationStack(ROLES_STACK);
+            System.out.println("13. Delete the Auto Scaling group");
@@ -260,50 +257,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        public static String getVPC(Ec2Client ec2) {
-            try {
-                DescribeVpcsRequest request = DescribeVpcsRequest.builder()
-                        .filters(f -> f.name("isDefault").values("true"))
-                        .build();
-    
-                DescribeVpcsResponse response = ec2.describeVpcs(request);
-    
-                if (!response.vpcs().isEmpty()) {
-                    Vpc defaultVpc = response.vpcs().get(0);
-                    System.out.println("Default VPC ID: " + defaultVpc.vpcId());
-                    return defaultVpc.vpcId();
-                } else {
-                    System.out.println("No default VPC found.");
-                    return null; // Return null if no default VPC is found
-                }
-    
-            } catch (Ec2Exception e) {
-                System.err.println("EC2 error: " + e.awsErrorDetails().errorMessage());
-                return null; // Return null in case of an error
-            }
-        }
-    
-    
-        public static void updateTemlate(Ec2Client ec2, String launchTemplateName ) {
-            // Step 1: Create new launch template version
-            String newAmiId = "ami-0025f0db847eb6254";
-            RequestLaunchTemplateData launchTemplateData = RequestLaunchTemplateData.builder()
-                    .imageId(newAmiId)
-                    .build();
-    
-            CreateLaunchTemplateVersionRequest createVersionRequest = CreateLaunchTemplateVersionRequest.builder()
-                    .launchTemplateName(launchTemplateName)
-                    .versionDescription("Updated with valid AMI")
-                    .sourceVersion("1")
-                    .launchTemplateData(launchTemplateData)
-                    .build();
-    
-            CreateLaunchTemplateVersionResponse createResponse = ec2.createLaunchTemplateVersion(createVersionRequest);
-            int newVersionNumber = createResponse.launchTemplateVersion().versionNumber().intValue();
-    
-            // Step 2: Modify default version
-            ModifyLaunchTemplateRequest modifyRequest = ModifyLaunchTemplateRequest.builder()
-                    .launchTemplateName(launchTemplateName)
-                    .defaultVersion(String.valueOf(newVersionNumber))
-                    .build();
-    
-            ec2.modifyLaunchTemplate(modifyRequest);
-            System.out.println("Updated launch template to version " + newVersionNumber + " with AMI " + newAmiId);
-        }
@@ -350 +297,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                                                  Ec2Client ec2Client,
@@ -353 +300 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                                                  String vpcId) {
+                String vpcZoneId) {
@@ -355,15 +302 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                // Step 1: Get one subnet ID in the given VPC
-                DescribeSubnetsRequest subnetRequest = DescribeSubnetsRequest.builder()
-                        .filters(Filter.builder().name("vpc-id").values(vpcId).build())
-                        .build();
-    
-                DescribeSubnetsResponse subnetResponse = ec2Client.describeSubnets(subnetRequest);
-    
-                if (subnetResponse.subnets().isEmpty()) {
-                    throw new RuntimeException("No subnets found in VPC: " + vpcId);
-                }
-    
-                String subnetId = subnetResponse.subnets().get(0).subnetId(); // Use first subnet
-                System.out.println("Using subnet: " + subnetId);
-    
-                // Step 2: Create launch template reference
+                AutoScalingWaiter waiter = autoScalingClient.waiter();
@@ -374 +306,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                // Step 3: Create Auto Scaling group
@@ -376,0 +309 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+                        .availabilityZones("us-east-1a")
@@ -378 +310,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                        .minSize(1)
@@ -380 +312,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                        .vpcZoneIdentifier(subnetId)  // Correct: subnet ID, not VPC ID
+                        .minSize(1)
+                        .vpcZoneIdentifier(vpcZoneId)
@@ -384,3 +316,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    
-                // Step 4: Wait until group is created
-                AutoScalingWaiter waiter = autoScalingClient.waiter();
@@ -391,3 +321,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                WaiterResponse<DescribeAutoScalingGroupsResponse> waiterResponse =
-                        waiter.waitUntilGroupExists(groupsRequest);
-    
+                WaiterResponse<DescribeAutoScalingGroupsResponse> waiterResponse = waiter
+                        .waitUntilGroupExists(groupsRequest);
@@ -397,2 +326,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            } catch (Ec2Exception | AutoScalingException e) {
-                System.err.println("Error: " + e.awsErrorDetails().errorMessage());
+            } catch (AutoScalingException e) {
+                System.err.println(e.awsErrorDetails().errorMessage());
@@ -1587 +1516 @@ Run the interactive scenario at a command prompt.
-Create a class that wraps Auto Scaling and Amazon EC2 actions.
+Create a class that wraps Amazon EC2 Auto Scaling and Amazon EC2 actions.
@@ -2082 +2011 @@ Create a class that wraps Auto Scaling and Amazon EC2 actions.
-Create a class that wraps Elastic Load Balancing actions.
+Create a class that wraps ELB actions.