[英]Programmatically create and deploy On-Demand EC2
Is it possible to programmatically get/deploy and start an EC2 instance? 是否可以以编程方式获取/部署和启动EC2实例? Essentially pick your instance type, AMI and start it up? 基本上选择你的实例类型,AMI并启动它?
I see the StartInstance
method but this only applies to instances already create and stopped in your account. 我看到了StartInstance
方法,但这仅适用于已在您的帐户中创建和停止的实例。
http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_StartInstances.html http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_StartInstances.html
Essentially, what is going on is that I have an automated service that needs multiple EC2 instances for computation. 基本上,正在发生的事情是我有一个需要多个EC2实例进行计算的自动化服务。 I need to programmatically create a new instance, pick the instance type, pick the AMI, start it up and run some deployment scripts to get things rolling. 我需要以编程方式创建一个新实例,选择实例类型,选择AMI,启动它并运行一些部署脚本以使事情滚动。
I would think there is a way to do this with the AWS SDK but I'm just not seeing it. 我认为有一种方法可以使用AWS SDK做到这一点,但我只是没有看到它。
On a related note, also need to be able to programmatically destroy a shutdown instance. 在相关的说明中,还需要能够以编程方式销毁关闭实例。
Yes, it's possible. 是的,这是可能的。
You use the RunInstances API method. 您使用RunInstances API方法。
Launches the specified number of instances using an AMI for which you have permissions. 使用您具有权限的AMI启动指定数量的实例。
To completely get rid of an instance, use TerminateInstance . 要完全删除实例,请使用TerminateInstance 。
Shuts down one or more instances. 关闭一个或多个实例。 This operation is idempotent; 这种操作是幂等的; if you terminate an instance more than once, each call succeeds. 如果多次终止实例,则每次调用都会成功。
The language is a bit confusing because it says "Shuts down one or more instances", but in fact it totally removes them. 语言有点令人困惑,因为它说“关闭一个或多个实例”,但实际上它完全删除了它们。
You can write SDK scripts to do the job (create, change and destroy ec2 instances), depend on which language you mastered, such as javascript, java, ruby, python, etc. 您可以编写SDK脚本来完成工作(创建,更改和销毁ec2实例),具体取决于您掌握的语言,例如javascript,java,ruby,python等。
But there are easier way to follow up, we call it infrastructure as code , try aws cloudformation
or Hashicopy's terraform
但是有更简单的跟进方式,我们称之为基础设施代码 ,尝试aws cloudformation
或Hashicopy的terraform
With their templates, you can deploy the whole infrastructure (include ec2, rds, vpc, security groups, subnet, etc) as minutes job. 使用他们的模板,您可以将整个基础架构(包括ec2,rds,vpc,安全组,子网等)部署为分钟工作。
refer: 参考:
you can try this.. 你可以试试这个..
AmazonEC2Client amazonEc2client = GetAmazonClient(ConfigurationManager.AppSettings["AwsRegionEndPint"]);
var launchRequest = new RunInstancesRequest()
{
ImageId = YOUR IMAGE ID,
InstanceType = YOUR INSTANCE TYPE,
MinCount = 1,
MaxCount = 1,
KeyName = your keyPairName,
SecurityGroupIds =your groups,
SubnetId = your subnet Id,
};
RunInstancesResponse runInstancesResponse = amazonEc2client.RunInstances(launchRequest);
//code For assign Tag name //代码用于分配标签名称
var InstanceId = runInstancesResponse.Reservation.Instances[0].InstanceId;
var trequest = new CreateTagsRequest();
trequest.Resources=new List<string>(){InstanceId};
List<Tag> tags=new List<Tag>();
Tag tag=new Tag("Name","TestCodeFinal");
tags.Add(tag);
trequest.Tags = tags;
amazonEc2client.CreateTags(trequest);
Reservation reservation = runInstancesResponse.Reservation;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.