There are more AWS SDK examples available in the AWS Doc SDK Examples
Amazon EC2 examples using SDK for .NET (v4)
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for .NET (v4) with Amazon EC2.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Get started
The following code examples show how to get started using Amazon EC2.
- SDK for .NET (v4)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. namespace EC2Actions; public class HelloEc2 { /// <summary> /// HelloEc2 lists the existing security groups for the default users. /// </summary> /// <param name="args">Command line arguments</param> /// <returns>Async task.</returns> static async Task Main(string[] args) { // Set up dependency injection for Amazon Elastic Compute Cloud (Amazon EC2). using var host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args) .ConfigureServices((_, services) => services.AddAWSService<IAmazonEC2>() .AddTransient<EC2Wrapper>() ) .Build(); // Now the client is available for injection. var ec2Client = host.Services.GetRequiredService<IAmazonEC2>(); try { // Retrieve information for up to 10 Amazon EC2 security groups. var request = new DescribeSecurityGroupsRequest { MaxResults = 10 }; var securityGroups = new List<SecurityGroup>(); var paginatorForSecurityGroups = ec2Client.Paginators.DescribeSecurityGroups(request); await foreach (var securityGroup in paginatorForSecurityGroups.SecurityGroups) { securityGroups.Add(securityGroup); } // Now print the security groups returned by the call to // DescribeSecurityGroupsAsync. Console.WriteLine("Welcome to the EC2 Hello Service example. " + "\nLet's list your Security Groups:"); securityGroups.ForEach(group => { Console.WriteLine( $"Security group: {group.GroupName} ID: {group.GroupId}"); }); } catch (AmazonEC2Exception ex) { Console.WriteLine($"An Amazon EC2 service error occurred while listing security groups. {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"An error occurred while listing security groups. {ex.Message}"); } } }
-
For API details, see DescribeSecurityGroups in AWS SDK for .NET API Reference.
-