1

I am writing a ec2 scheduler logic to start and stop ec2 instances. The lambda works for stopping instances. However the start function is not initiating ec2 start. The logic is to filter based on tags and status of ec2 and start or stop based on current status.

Below is the code snippet to start EC2 instances. But this isn't starting the instances. The filtering happens correctly and pushes the instances to "stopParams" object.

The same code works if I change the logic to ec2.stopInsatnces by filtering the running state instances. The role has permissions to start and stop .

Any ideas why its not triggering start ?

 if (instances.length > 0){
                var stopParams = { InstanceIds: instances };
                ec2.startInstances(stopParams, function(err,data) {
                    if (err) {
                       console.log(err, err.stack);
                    } else {
                       console.log(data);
                    }
                    context.done(err,data);
                }); 

1 Answer 1

6

Finally got this working. There were no issues with the nodejs lambda code. Even though was able to stop instances but start instances were not invoking the start method. Found that all volumes are encrypted.

To start an instance using API call the lambda role used by lambda should have permission to kms key which is used for encrypting the volume. After adding the lambda role arn in the principal section of kms key policy permission the lambda was able to start instances. But key permission is not necessary for stopping the instance. Hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

In particular I needed: "kms:CreateGrant"
Thanks a bunch for this!! I was going mad as to why a perfectly working lambda function failed to start servers. Then it hit that volumes are encrypted! Saved me from going mad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.