【问题标题】:aws javascript how to list all available instancesaws javascript如何列出所有可用实例
【发布时间】:2018-02-20 01:18:11
【问题描述】:

我希望能够将实例 (ec2) 放入 aws FROM javascript api。 我已经有了脚本,但问题是在发布这样的详细信息时:

    <p style="display: block;">
                                    <input type="text" style="display:block;" id="provisionEnvironmentName" placeholder="Name">
                                    <select class="ThemeInput" id="provisionEnvironmentInstanceType" style="width:100%;">
                                    <option>m3.medium</option><option>m3.large</option><option>m3.xlarge</option><option>m3.2xlarge</option>
    <option>c3.large</option><option>c3.xlarge</option><option>c3.2xlarge</option><option>c3.4xlarge</option><option>c3.8xlarge</option>
    r3.large<option>r3.xlarge</option><option>r3.2xlarge</option><option>r3.4xlarge</option><option>r3.8xlarge</option><option>x1.16xlarge</option><option>x1.32xlarge</option>
    <option>d2.xlarge</option><option>d2.2xlarge</option><option>d2.4xlarge</option><option>d2.8xlarge</option><option>i2.xlarge</option><option>i2.2xlarge</option><option>i2.4xlarge</option><option>i2.8xlarge</option><option>i3.large</option><option>i3.xlarge</option><option>i3.2xlarge</option><option>i3.4xlarge</option><option>i3.8xlarge</option><option>i3.16xlarge</option>
    <option>f1.2xlarge</option><option>f1.16xlarge</option>
                                    </select>
                                    <select class="ThemeInput" id="provisionEnvironmentImage" style="width:100%">
                                        <option value="ami-162c2575">Amazon Linux AMI 2017.03.0 (HVM), SSD Volume Type</option>
                                        <option value="ami-39ac915a">Red Hat Enterprise Linux 7.3 (HVM), SSD Volume Type</option>
                                        <option value="ami-527b4031">SUSE Linux Enterprise Server 12 SP2 (HVM), SSD Volume Type</option>
                                        <option value="ami-96666ff5">Ubuntu Server 16.04 LTS (HVM), SSD Volume Type</option>
                                        <option value="ami-bf5743dc">Microsoft Windows Server 2016 Base</option>
                                        <option value="ami-d78288b4">Microsoft Windows Server 2016 Base with Containers</option>
                                        <option value="ami-64ada707">Microsoft Windows Server 2016 Base Nano</option>
                                        <option value="ami-c4262ca7">Microsoft Windows Server 2012 R2 Base</option>
                                        <option value="ami-9b242ef8">Microsoft Windows Server 2012 Base</option>
                                        <option value="ami-0e23296d">Microsoft Windows Server 2008 R2 Base</option>
                                        <option value="ami-8ea3fbed">SUSE Linux Enterprise Server 11 SP4 (PV), SSD Volume Type</option>
                                        <option value="ami-807876e3">Ubuntu Server 14.04 LTS (HVM), SSD Volume Type</option>
                                        <option value="ami-af2128cc">Amazon Linux AMI 2017.03.0 (PV)</option>
                                        <option value="ami-ae5e4bcd">Microsoft Windows Server 2003 R2 Base (32-bit)</option>
                                        <option value="ami-b35a4fd0">Microsoft Windows Server 2003 R2 Base (64-bit)</option>
                                    </select>
                                  </p>

它给了我错误:

InvalidAMIID.NotFound: The image id '[ami-162c2575]' does not exist

我认为这意味着我的所有 ami-ids 的帖子都不好。所以问题是如何使用有效的图像 ID (ami-ids) 进行呼叫。从哪里获取这些 id,如何调用以使用 javascript 获取有效列表?

$('#Aws_Create_Button').click(function(){
                        $('#loader').html("<img src='./theme/admin/img/LoaderBamboo.gif' style='height:20px; width:auto; margin:0 5px 0 0;'>Loading...");
                        $('#loader').fadeIn();
                        name = $('#Aws_User').val();
                        pw = $('#Aws_Password').val();
                        region = $('#Aws_Region').val();

                        if(pw == '') {
                            $('#loader').html("<font color='#CC0000'>Please fill out all mandatory fields (AccessKeyId and SecretAccessKey).</font>");  
                            return;
                        }

                        AWS.config.update({
                            'accessKeyId': name,
                            'secretAccessKey': pw,
                            'region': region
                        });

                        var ec2 = new AWS.EC2();
                        swal({
                              title: 'Provision Environment',
                              text: `
                                <input type='text' style='display:block;' id='provisionEnvironmentName' placeholder='Name'>
                                <select class='ThemeInput' id='provisionEnvironmentInstanceType' style='width:100%;'>
                                <option>m3.medium</option><option>m3.large</option><option>m3.xlarge</option><option>m3.2xlarge</option>
<option>c3.large</option><option>c3.xlarge</option><option>c3.2xlarge</option><option>c3.4xlarge</option><option>c3.8xlarge</option>
</option>r3.large<option>r3.xlarge</option><option>r3.2xlarge</option><option>r3.4xlarge</option><option>r3.8xlarge</option><option>x1.16xlarge</option><option>x1.32xlarge</option>
<option>d2.xlarge</option><option>d2.2xlarge</option><option>d2.4xlarge</option><option>d2.8xlarge</option><option>i2.xlarge</option><option>i2.2xlarge</option><option>i2.4xlarge</option><option>i2.8xlarge</option><option>i3.large</option><option>i3.xlarge</option><option>i3.2xlarge</option><option>i3.4xlarge</option><option>i3.8xlarge</option><option>i3.16xlarge</option>
<option>f1.2xlarge</option><option>f1.16xlarge</option>
                                </select>
                                <select class='ThemeInput' id='provisionEnvironmentImage' style='width:100%'>
                                    <option value='ami-162c2575'>Amazon Linux AMI 2017.03.0 (HVM), SSD Volume Type</option>
                                    <option value='ami-39ac915a'>Red Hat Enterprise Linux 7.3 (HVM), SSD Volume Type</option>
                                    <option value='ami-527b4031'>SUSE Linux Enterprise Server 12 SP2 (HVM), SSD Volume Type</option>
                                    <option value='ami-96666ff5'>Ubuntu Server 16.04 LTS (HVM), SSD Volume Type</option>
                                    <option value='ami-bf5743dc'>Microsoft Windows Server 2016 Base</option>
                                    <option value='ami-d78288b4'>Microsoft Windows Server 2016 Base with Containers</option>
                                    <option value='ami-64ada707'>Microsoft Windows Server 2016 Base Nano</option>
                                    <option value='ami-c4262ca7'>Microsoft Windows Server 2012 R2 Base</option>
                                    <option value='ami-9b242ef8'>Microsoft Windows Server 2012 Base</option>
                                    <option value='ami-0e23296d'>Microsoft Windows Server 2008 R2 Base</option>
                                    <option value='ami-8ea3fbed'>SUSE Linux Enterprise Server 11 SP4 (PV), SSD Volume Type</option>
                                    <option value='ami-807876e3'>Ubuntu Server 14.04 LTS (HVM), SSD Volume Type</option>
                                    <option value='ami-af2128cc'>Amazon Linux AMI 2017.03.0 (PV)</option>
                                    <option value='ami-ae5e4bcd'>Microsoft Windows Server 2003 R2 Base (32-bit)</option>
                                    <option value='ami-b35a4fd0'>Microsoft Windows Server 2003 R2 Base (64-bit)</option>
                                </select>
                              `,
                              html: true,
                              showCancelButton: true,
                            },function(){
                                var params = {
                                   ImageId: $('#provisionEnvironmentImage').val(),
                                   InstanceType: $('#provisionEnvironmentInstanceType').val(),
                                   MinCount: 1,
                                   MaxCount: 1
                                };

                                ec2.runInstances(params, function(err, data) {
                                   if (err) {
                                      $('#loader').html("<font color='#CC0000'>"+err+"</font>");
                                      return;
                                   }
                                   //$.getJSON('index.php?Theme=default&Base=Environment&Script=Awsfunc&Username='+name+'&Password='+pw+'&Region='+region);
                                   var instanceId = data.Instances[0].InstanceId;
                                   params = {Resources: [instanceId], Tags: [
                                      {
                                         Key: 'Name',
                                         Value: $('#provisionEnvironmentName').val()
                                      }
                                   ]};
                                   ec2.createTags(params, function(err) {
                                      console.log('Tagging instance', err ? 'failure' : 'success');
                                   });
                                   data.Instances[0]['KeyName'] = $('#provisionEnvironmentName').val();
                                   $.getJSON('index.php?Theme=default&Base=Environment&Script=Awsfunc&Username='+name+'&Password='+pw+'&Region='+region+'&Action=insert&Data='+JSON.stringify(data.Instances[0]), function(data){
                                        console.log(data);
                                        if(JSON.stringify(data) == "null" || JSON.stringify(data) == "NaN" || JSON.stringify(data) == "error"){
                                            $('#loader').html("<font color='#CC0000'>Something went wrong!<br>Please check your entries and try again.</font>");    
                                        }else{
                                            var string = "<font color='#00CC00'>Connection successfull - Environments imported and deployed to AWS successfully</font><br><br>The following server and environment have been imported or updated and deployed to AWS:<br>";
                                            for(var i=0; i<data['hosts'].length; i++) {
                                                string += '<a target="new" href="index.php?Theme=admin&Base=Environment&Script=Insertupdate&EnvironmentID=' + data['envts'][i]['EnvironmentID'] + '&EnvironmentUUID=' + data['envts'][i]['EnvironmentUUID'] + '">' + data['envts'][i]['EnvironmentName'] + '</a> - <a href="index.php?Theme=admin&Base=HostServer&Script=Insertupdate&HostServerID=' + data['hosts'][i]['HostServerID'] + '&HostServerUUID=' + data['hosts'][i]['HostServerUUID'] + '" target="new">(Server)</a><br>';
                                            }
                                            $('#loader').html(string);
                                        }
                                    });
                                });
                        });
                    });
                });

【问题讨论】:

    标签: javascript amazon-ec2


    【解决方案1】:

    分享您的 javascript 代码。您应该使用以下代码创建新的 ec2 实例。

        var AWS = require('aws-sdk');
        // Load credentials and set region from JSON file
        AWS.config.loadFromPath('./config.json');
    
        // Create EC2 service object
        var ec2 = new AWS.EC2({apiVersion: '2016-11-15'});
    
        // AMI is amzn-ami-2011.09.1.x86_64-ebs
        var instanceParams = {
           ImageId: 'ami-10fd7020', 
           InstanceType: 't1.micro',
           KeyName: 'KEY_PAIR_NAME',
           MinCount: 1,
           MaxCount: 1
        };
    
        // Create a promise on an EC2 service object
        var instancePromise = new AWS.EC2({apiVersion: '2016-11-15'}).runInstances(instanceParams).promise();
    
    // Handle promise's fulfilled/rejected states
    instancePromise.then(
      function(data) {
        console.log(data);
        var instanceId = data.Instances[0].InstanceId;
        console.log("Created instance", instanceId);
        // Add tags to the instance
        tagParams = {Resources: [instanceId], Tags: [
           {
              Key: 'Name',
              Value: 'SDK Sample'
           }
        ]};
        // Create a promise on an EC2 service object
        var tagPromise = new AWS.EC2({apiVersion: '2016-11-15'}).createTags(tagParams).promise();
        // Handle promise's fulfilled/rejected states
        tagPromise.then(
          function(data) {
            console.log("Instance tagged");
          }).catch(
            function(err) {
            console.error(err, err.stack);
          });
      }).catch(
        function(err) {
        console.error(err, err.stack);
      });
    

    有关 javascript SDK 的更多信息,请阅读以下文档: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/sdk-code-samples.html

    【讨论】:

    • 我用 javascript 更新了我的帖子,见上文。您的代码中的内容是您如何找到 ImageId:'ami-10fd7020'。 Thaat 是我的问题,如何知道所有这些 ami id?假设:我在哪里可以获得工作 ami-id?
    • 您可以使用通用 AMI,例如 Amazon Linux,也可以使用您自定义的镜像 id。请阅读以下文档了解 Amazon Linux id 和相关信息(通用 AMI 之一)。 docs.aws.amazon.com/AWSEC2/latest/UserGuide/…
    • 感谢您的链接,但我仍然找不到 api 实例,例如从 populair ami 的 aws 列表下载
    • 要查找公共 AMI 列表,请使用您的 ec2 控制台并尝试创建新实例,您可以看到所有这些实例。
    • 是的,可以从命令行完成,但也许稍后它们会改变并且这个脚本可以被破坏:(这就是为什么我想从亚马逊获得它的实时选项
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    相关资源
    最近更新 更多