aws javascript如何列出所有可用的实例

时间:2018-02-19 16:53:20

标签: javascript amazon-ec2

我希望能够将实例(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>
&#13;
&#13;
&#13;

它给了我错误:

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

我认为这意味着我所有ami-id的帖子都不好。所以问题是如何使用有效的图像ID(ami-id)进行调用。在哪里获得这些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);
                                        }
                                    });
                                });
                        });
                    });
                });

1 个答案:

答案 0 :(得分:0)

分享您的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