AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-08-01 · Documentation low

File: code-library/latest/ug/ec2_example_ec2_RunInstances_section.md

Summary

Added Swift SDK code example for EC2 RunInstances API with security group and key pair parameters

Security assessment

The change documents secure instance creation using security groups and key pairs, which are security features. However, it demonstrates normal API usage rather than addressing a specific security vulnerability.

Diff

diff --git a/code-library/latest/ug/ec2_example_ec2_RunInstances_section.md b/code-library/latest/ug/ec2_example_ec2_RunInstances_section.md
index bfb8047cd..fd80c2c6b 100644
--- a//code-library/latest/ug/ec2_example_ec2_RunInstances_section.md
+++ b//code-library/latest/ug/ec2_example_ec2_RunInstances_section.md
@@ -1091,0 +1092,56 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+Swift
+    
+
+**SDK for Swift**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/swift/example_code/ec2#code-examples). 
+    
+    
+    import AWSEC2
+    
+        /// Create and return a new EC2 instance.
+        /// 
+        /// - Parameters:
+        ///   - imageId: The image ID of the AMI to use when creating the instance.
+        ///   - instanceType: The type of instance to create.
+        ///   - keyPairName: The RSA key pair's name to use to secure the instance.
+        ///   - securityGroups: The security group or groups to add the instance
+        ///     to.
+        ///
+        /// - Returns: The EC2 instance as an `EC2ClientTypes.Instance` object.
+        func runInstance(imageId: String, instanceType: EC2ClientTypes.InstanceType,
+                            keyPairName: String, securityGroups: [String]?) async -> EC2ClientTypes.Instance? {
+            do {
+                let output = try await ec2Client.runInstances(
+                    input: RunInstancesInput(
+                        imageId: imageId,
+                        instanceType: instanceType,
+                        keyName: keyPairName,
+                        maxCount: 1,
+                        minCount: 1,
+                        securityGroupIds: securityGroups
+                    )
+                )
+    
+                guard let instances = output.instances else {
+                    print("*** Unable to create the instance.")
+                    return nil
+                }
+    
+                return instances[0]
+            } catch {
+                print("*** Error creating the instance: \(error.localizedDescription)")
+                return nil
+            }
+        }
+    
+    
+
+  * For API details, see [RunInstances](https://sdk.amazonaws.com/swift/api/awsec2/latest/documentation/awsec2/ec2client/runinstances\(input:\)) in _AWS SDK for Swift API reference_. 
+
+
+
+