简体   繁体   English

以编程方式创建和部署按需EC2

[英]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: 参考:

AWS CloudFormation templates AWS CloudFormation模板

Terraform AWS PROVIDER Terraform AWS PROVIDER

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.

相关问题 AWS EC2 按需定价 - AWS EC2 On-Demand Pricing 詹金斯(Jenkins)错误,尝试按需生成Linux EC2 Slave - Jenkins error trying to raise on-demand linux ec2 slave 如何启动 EC2 实例并按需运行任务 - How to launch a EC2 instance and run a task on-demand 在Amazon EC2上,竞价型实例价格是否会高于按需价格? - On Amazon EC2, will the Spot Instance price ever be higher than the On-Demand Price? AWS EC2中的预留实例是否具有比按需实例或竞价型实例更好的性能? - Do Reserved Instances in AWS EC2 Have Better Performance Than On-Demand or Spot Instances? 如果 Spot 实例在 AWS 中不可用,我可以获取按需 EC2 实例吗 - Can I get the on-demand EC2 instances if spot instances are not available in AWS AWS数据管道-我们可以重用在“按需”管道激活期间创建的EC2实例吗? - AWS Data Pipeline - Can we re-use the EC2 instance created during 'on-demand' pipeline activation? *限制* 仪表板的“按需标准(A、C、D、H、I、M、R、T、Z)”描述中包含哪些 EC2 实例? - Which EC2 instances are included in the "On-Demand Standard (A, C, D, H, I, M, R, T, Z)" description of the *Limits* Dashboard? 我可以使用按需 EC2 实例来实现 FIPS 140-2 合规性,还是需要使用专用主机? - Can I use on-demand EC2 instances for FIPS 140-2 compliance, or do I need to use dedicated hosts? 按需缩放ec2,lambda样式 - scaling ec2 on demand , lambda style
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM