User guide
Step 1: Setting Up Your Credentials
To begin using this code sample, you need to populate the App.config file with your AWS credentials,
which identify you to Amazon Web Services.You specify your credentials in the files appSettings
element. The preferred way to handle credentials is to create a profile in the SDK Store, which encrypts
your credentials and stores them separately from any project.You can then specify the profile by name
in the App.config file, and the credentials are automatically incorporated into the application. For more
information, see Configuring Your AWS SDK for .NET Application (p. 8).
Now that you have configured your settings, you can get started using the code in the example.
Step 2: Setting Up a Security Group
A security group acts as a firewall that controls the traffic allowed in and out of a group of instances. By
default, an instance is started without any security group, which means that all incoming IP traffic, on any
TCP port will be denied. So, before submitting your Spot Request, you will set up a security group that
allows the necessary network traffic. For the purposes of this tutorial, we will create a new security group
called "GettingStarted" that allows connection using the Windows Remote Desktop Protocol (RDP) from
the IP address of the local computer, that is, the computer where you are running the application.
To set up a new security group, you need to include or run the following code sample that sets up the
security group programmatically.You only need to run this code once to create the new security group.
However, the code is designed so that it is safe to run even if the security group already exists. In this
case, the code catches and ignores the "InvalidGroup.Duplicate" exception.
In the code below, we first use AWSClientFactoryClass to create an AmazonEC2 client object. We
then create a CreateSecurityGroupRequest object with the name, "GettingStarted" and a description
for the security group. Finally, we call the ec2.createSecurityGroup API to create the group.
1 AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client();
try
{
5 CreateSecurityGroupRequest securityGroupRequest = new CreateSecurity
GroupRequest();
securityGroupRequest.GroupName = "GettingStartedGroup";
securityGroupRequest.GroupDescription = "Getting Started Security Group";
ec2.CreateSecurityGroup(securityGroupRequest);
10 }
catch (AmazonEC2Exception ae)
{
if (string.Equals(ae.ErrorCode, "InvalidGroup.Duplicate", StringCompar
ison.InvariantCulture))
{
15 Console.WriteLine(ae.Message);
}
else
{
throw;
20 }
}
To enable access to the group, we create an ipPermission object with the IP address set to the CIDR
representation of the IP address of the local computer. The "/32" suffix on the IP address indicates that
the security group should accept traffic only from the local computer.We also configure the ipPermission
object with the TCP protocol and port 3389 (RDP).You will need to fill in the IP address of the local
Version v2.0.0
81
AWS SDK for .NET Developer Guide
Tutorial: Amazon EC2 Spot Instances