User guide

Tutorial: Creating Amazon EC2 Instances with
the AWS SDK for .NET
This topic demonstrates how to use the AWS SDK for .NET to create, start, and terminate Amazon Elastic
Compute Cloud (Amazon EC2) instances.
The sample code in this topic is written in C#, but you can use the AWS SDK for .NET with any language
that is compatible with the Microsoft .NET Framework.The AWS SDK for .NET installs a set of C# project
templates, so the simplest way to start this project is to click the Visual Studio File menu's New Project
command and create a new AWS Empty Project.
Your project must include an App.Config file, which serves as a container for your AWS credentials
and has the following content:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="AWSProfileName" value="profile_name"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
Note
If create your project by using an AWS SDK for .NET template, Visual Studio creates this file for
you.
To add your credentials to App.Config, replace profile_name with your AWS credentials' profile
name. For more information on credentials and profiles, see Configuring Your AWS SDK for .NET
Application (p. 8).The IAM policy that is attached to this profile must grant permissions for all of the
Amazon EC2 actions that you plan to use in your application. If you plan to launch an instance with an
IAM role, the policy must also grant permissions for IAM. For more information, see Permissions and
Policies and Roles.
The following sections walk you through the basic procedure for creating, launching, and terminating an
Amazon EC2 instance by using the AWS SDK for .NET.
Topics
Create an Amazon EC2 Client (p. 43)
Specify an Amazon EC2 Security Group (p. 44)
Authorize Security Group Ingress (p. 46)
Specify an Amazon EC2 Key Pair (p. 47)
Launch Amazon EC2 Instances (p. 48)
Terminate Amazon EC2 Instances (p. 51)
Additional Amazon EC2 Resources (p. 51)
Create an Amazon EC2 Client
The first step in setting up an Amazon EC2 instance is to create an Amazon EC2 client, which represents
a set of Amazon EC2 instances and is used to configure, start, and terminate them. The client is
represented by an AmazonEC2Client object, which you create as follows:
Version v2.0.0
43
AWS SDK for .NET Developer Guide
Tutorial: Creating Amazon EC2 Instances