关闭/启动ec2实例基于标签lambda函数不起作用

时间:2015-12-06 10:25:03

标签: node.js aws-lambda aws-sdk

我想通过使用lambda函数基于单个标记和值来启动/停止ec2实例。

我正在尝试修改以下代码:https://github.com/SamVerschueren/aws-lambda-stop-server

到目前为止,我已经能够指定我想要运行lambda函数的区域,但是无法训练如何过滤实例。即过滤带有“schedule”标签的实例,然后对该值执行拆分字符串,并检查其中一个拆分值是否与“stop6pmdaily”匹配。例如schedule = start8amdaily | stop6pmdaily

if (instance.State.Code === 16) {
// 0: pending, 16: running, 32: shutting-down, 48: terminated, 64: stopping, 80: stopped
    values = instance.Tags["schedule"].Value.Split("|")
    for (v of values) {
        if (v == 'stop6pmdaily'){
            stopParams.InstanceIds.push(instance.InstanceId);
        }
    }
}

所以完整的功能代码如下:

'use strict';

/**
 * AWS Lambda function that stops servers.
 *
 * @author Sam Verschueren      <sam.verschueren@gmail.com>
 * @since  09 Oct. 2015
 */

// module dependencies
var AWS = require('aws-sdk');
	AWS.config.update({region: 'ap-southeast-2'});
var pify = require('pify');
var Promise = require('pinkie-promise');

var ec2 = new AWS.EC2();

/**
 * The handler function.
 *
 * @param {object}  event		The data regarding the event.
 * @param {object}  context		The AWS Lambda execution context.
 */
exports.handler = function (event, context) {

	// Describe the instances
	pify(ec2.describeInstances.bind(ec2), Promise)() //(describeParams)
		.then(function (data) {
			var stopParams = {
				InstanceIds: []
			};

			data.Reservations.forEach(function (reservation) {
				reservation.Instances.forEach(function (instance) {
					if (instance.State.Code === 16) {
						// 0: pending, 16: running, 32: shutting-down, 48: terminated, 64: stopping, 80: stopped
						values = instance.Tags["schedule"].Value.Split("|")
						for (v of values) {
							if (v == 'stop6pmdaily'){
								stopParams.InstanceIds.push(instance.InstanceId);
							}
						}
					}
				});
			});

			if (stopParams.InstanceIds.length > 0) {
				// Stop the instances
				return pify(ec2.stopInstances.bind(ec2), Promise)(stopParams);
			}
		})
		.then(context.succeed)
		.catch(context.fail);
};

1 个答案:

答案 0 :(得分:2)

所以事实证明我没有正确访问标签数组。

<!doctype html>
<html lang="en">

  <head>

    <title>HTML5 Skeleton</title>

    <meta charset="utf-8">
      <!--[if lt IE 9]>
      <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
      </script>
      <![endif]-->

    <style>

      body {
        font-family: Verdana, sans-serif; 
        font-size:0.8em;
      }
      header,nav,section,article,footer {
        border:1px solid grey; 
        margin:5px; 
        padding:8px;}
       nav ul {
        margin:0; 
        padding:0;
      }
       nav ul li {
        display:inline; 
        margin:5px;
      }
      .slidingDiv {
        height:300px;
        padding:20px;
        margin-top:10px;
      }

      .show_hide {
        display:none;
      }
    </style>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">

    $(document).ready(function(){

    $(".slidingDiv").hide();
    $(".show_hide").show();

    $('.show_hide').click(function(){
    $(".slidingDiv").slideToggle();
    });

    });

  </script>
  </head>
  <body>
    <header>
      <h1 position="centre">Blah</h1>
    </header>
    <nav>

      <ul>
        <li><a href="#" class="show_hide">Topic 1</a></li>
        <li><a href="#" class="show_hide">Topic 2</a></li>
        <li><a href="#" class="show_hide">Topic 3</a></li>
      </ul>

    </nav>
    <section class="slidingDiv">
      <h1>Main heading</h1>

      <article>
        <h2>Sub heading</h2>
        <p>Blahblahblahblahblah</p>
      </article>
      <a href="#" class="show_hide">hide</a></div>
    </section>

    <footer>
     <p>Footer</p>
    </footer>

  </body>
</html>