Reputation: 1846
I am writing my first ever code for AWS. I have downloaded the AWS .NET SDK.
I need to programmatically Create/Launch/Terminate an EC2 instance.
I was able to write the following lines of code, but have no idea what do from here:
public static Boolean LaunchInstance()
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
AmazonAutoScaling autoscaling = AWSClientFactory.CreateAmazonAutoScalingClient(
appConfig["AWSAccessKey"],
appConfig["AWSSecretKey"]
);
CreateLaunchConfigurationResponse ccResponse = autoscaling.CreateLaunchConfiguration(new CreateLaunchConfigurationRequest());
return true;
}
I am stuck because I can't understand how to use the CreateLaunchConfigurationResponse and can't find any example on the internet. Do you have any example how to use this?
Upvotes: 0
Views: 420
Reputation: 1846
After going crazy all day, I was able to create/launch an instance with this code:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;
using Amazon;
using Amazon.EC2;
using Amazon.EC2.Model;
using Amazon.AutoScaling;
using Amazon.AutoScaling.Model;
namespace HG.AWS
{
public class AutoScale
{
public static Boolean LaunchInstance()
{
AmazonEC2Config EC2Config = new AmazonEC2Config()
.WithServiceURL("https://ec2.eu-west-1.amazonaws.com");
NameValueCollection appConfig = ConfigurationManager.AppSettings;
AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client(
appConfig["AWSAccessKey"],
appConfig["AWSSecretKey"],
EC2Config);
try
{
RunInstancesRequest EC2R = new RunInstancesRequest();
EC2R.ImageId = "ami-885b6bfc";
EC2R.InstanceType = "m1.large";
EC2R.MaxCount = 1;
EC2R.MinCount = 1;
RunInstancesResponse r = ec2.RunInstances(EC2R);
Upvotes: 2