我正在编写我的第一个AWS代码。 我已经下载了AWS .NET SDK。
我需要以编程方式创建/启动/终止EC2实例。
我能够编写以下代码行,但不知道从这里做了什么:
public static Boolean LaunchInstance()
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
AmazonAutoScaling autoscaling = AWSClientFactory.CreateAmazonAutoScalingClient(
appConfig["AWSAccessKey"],
appConfig["AWSSecretKey"]
);
CreateLaunchConfigurationResponse ccResponse = autoscaling.CreateLaunchConfiguration(new CreateLaunchConfigurationRequest());
return true;
}
我被卡住了,因为我无法理解如何使用CreateLaunchConfigurationResponse并且无法在互联网上找到任何示例。 你有任何例子如何使用它吗?
答案 0 :(得分:2)
在整天疯狂之后,我能够使用以下代码创建/启动实例:
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);