Giter Site home page Giter Site logo

Comments (8)

milvain avatar milvain commented on June 2, 2024 1

Many thanks @geekmuse it works now!!!!!

from iot-button-ec2-controller.

geekmuse avatar geekmuse commented on June 2, 2024

I can think of a few possible issues that might be going on here:

  • Are the EC2 instances you want to start/stop actually located in eu-west-3? AWS.config.region should be where the instances are, not necessarily the region the Lambda is deployed to.
  • Are you sure there's not a typo on tag key?
  • If you're sure the first two aren't an issue, how many EC2 instances do you have running in eu-west-3? It may be that there are too many running instances for the set returned in the API call to contain references to the instances you want to act on. I'll have a look at refactoring the function to recursively query all the EC2 instances in accounts where the EC2 footprint is substantial.

from iot-button-ec2-controller.

geekmuse avatar geekmuse commented on June 2, 2024

Actually, the third point above would likely be moot. The Filter expression is going to limit the API response to only instances containing the tag-key, since you're showing an empty set, that's certainly not the case here.

from iot-button-ec2-controller.

milvain avatar milvain commented on June 2, 2024

Hi,

  1. Yes my Lambda function is located to eu-west-1 and my EC2 instance is located to eu-west-3 so I configure AWS.config.region = 'eu-west-3'
  2. I double check and I try to switch to another tag-key and I have the same issue
  3. only one EC2 instance

from iot-button-ec2-controller.

milvain avatar milvain commented on June 2, 2024

For your information during the npm install

C:\Users\xxxxxx>>npm install
npm WARN deprecated [email protected]: CoffeeScript on NPM has moved to "coffeescript" (no hyphen)
npm WARN deprecated [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated [email protected]: please upgrade to graceful-fs 4 for compatibility with current and future versions of Node.js
npm WARN deprecated [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated [email protected]: Use uuid module instead
npm WARN deprecated [email protected]: This version is no longer maintained. Please upgrade to the latest version.
npm WARN deprecated [email protected]: This version is no longer maintained. Please upgrade to the latest version.
npm WARN deprecated [email protected]: This version is no longer maintained. Please upgrade to the latest version.
npm notice created a lockfile as package-lock.json. You should commit this file.
added 295 packages from 592 contributors and audited 1028 packages in 41.628s
found 69 vulnerabilities (9 low, 49 moderate, 11 high)
run npm audit fix to fix them, or npm audit for details

C:\Users\xxxxxx>grunt deploy
Running "lambda_package:default" (lambda_package) task
[email protected] ..........\xxxxx\AppData\Local\Temp\xxxxxxx\node_modules\iot-button-ec2-controller
└── [email protected]
Created package at ./dist/iot-button-ec2-controller_1-0-0_2019-2-1-10-32-47.zip

Running "lambda_deploy:default" (lambda_deploy) task
Uploading...
Package deployed.
No config updates to make.

Done, without errors.

from iot-button-ec2-controller.

milvain avatar milvain commented on June 2, 2024

Lambda function:

var AWS = require('aws-sdk');
var _ = require('underscore');
var ec2 = new AWS.EC2({ apiVersion: '2016-09-15' });
AWS.config.region = 'eu-west-3';

exports.handler = (event, context, callback) => {
console.log('Received event:', event);
var r;
var i;
var instanceIds = [];

var params = {
    Filters: [{
            Name: 'tag-key',
            Values: [
                'IOT'
            ]
        },
    ]
};

ec2.describeInstances(params, function(err, data) {
    if (err) {
        console.log(err, err.stack); // an error occurred
        context.fail('Error occurred.  Aborting.');
        return;
    }

    if (data.Reservations.length) {
        for (r in data.Reservations) {
            if (data.Reservations[r].Instances.length) {
                for (i in data.Reservations[r].Instances) {
                    if (event.clickType === 'SINGLE') {
                        if (!_.contains(['running', 'pending'], data.Reservations[r].Instances[i].State.Name)) {
                            instanceIds.push(data.Reservations[r].Instances[i].InstanceId);
                        }
                    } else if (event.clickType === 'DOUBLE') {
                        if (!_.contains(['shutting-down', 'terminated', 'stopped', 'stopping'], data.Reservations[r].Instances[i].State.Name)) {
                            instanceIds.push(data.Reservations[r].Instances[i].InstanceId);
                        }
                    }
                }
            }
        }
    }

    i = '';
    if (instanceIds.length) {
        if (event.clickType === 'SINGLE') {
            ec2.startInstances({ InstanceIds: instanceIds }, function(err, data) {
                if (!err) {
                    context.succeed({'msg': 'All affected instances started successfully.', 'instanceIds': instanceIds});
                } else {
                    console.log(err, err.stack);
                    context.fail({'msg': 'Instances failed to start.', 'instanceIds': instanceIds});
                }
            });
        } else if (event.clickType === 'DOUBLE') {
            ec2.stopInstances({ InstanceIds: instanceIds }, function(err, data) {
                if (!err) {
                    context.succeed({'msg': 'All affected instances stopped successfully.', 'instanceIds': instanceIds});
                } else {
                    console.log(err, err.stack);
                    context.fail({'msg': 'Instances failed to stop.', 'instanceIds': instanceIds});
                }
            });
        }
    } else {
        context.succeed({'msg': 'No instances in a state to be affected by this operation.', 'instanceIds': instanceIds});
    }
});

};

from iot-button-ec2-controller.

milvain avatar milvain commented on June 2, 2024

image

from iot-button-ec2-controller.

geekmuse avatar geekmuse commented on June 2, 2024

@milvain Try redeploying with the updated index.js (I updated the dependencies in package.json as well, but I'm going to assume you have pretty recent versions of the dependencies if you've just started playing around with this).

https://github.com/geekmuse/iot-button-ec2-controller/releases/tag/v1.1.0

from iot-button-ec2-controller.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.