0

Like the title said, I would like to register fresh EC2 with OpsWorks stack. Problem is, the command "register" can only be run from CLI (shell script) but not from a Lambda function (Python, Java, or JS). Is there any work-around to do this?

Casper
  • 1,663
  • 7
  • 35
  • 62

1 Answers1

1

Take a look at this: register_instance for Boto3/OpsWork. There are 2 parts in registering the instance and Boto3 can do the second part only.

We do not recommend using this action to register instances. The complete registration operation has two primary steps, installing the AWS OpsWorks agent on the instance and registering the instance with the stack. RegisterInstance handles only the second step. You should instead use the AWS CLI register command, which performs the entire registration operation. For more information, see Registering an Instance with an AWS OpsWorks Stack

To run the CLI in your Lambda function, make sure your Lambda Exec Role has the privileges to execute the OpsWork CLI and call some thing like this in your python Lambda:

import subprocess
subprocess.call(["aws", "--region", "us-east-1", "opsworks", "register-instance", "--stack-id", "<stack-id>"])

Look at OpsWorks CLI for more info.

helloV
  • 50,176
  • 7
  • 137
  • 145
  • Exactly, that's why I need to know if it's possible to run CLI from Lambda – Casper Feb 26 '16 at 23:02
  • Thanks, but does it even work? How do I pass in SSH key and profile? From python, I already have ssh key and access keys as variables. Also, can I retrieve return output? – Casper Feb 26 '16 at 23:20
  • Take a look at the OpsWorks CLI doc. It has the options pass to pem. – helloV Feb 26 '16 at 23:24
  • I already got a CLI command working for this matter. I just want to know how to do it from Lambda, you know it requires the access keys right? Is there documentation on cli for subprocess.call? – Casper Feb 26 '16 at 23:26
  • I just found out there is no aws cli on the Lambda linux machine. Do I need to install it first? – Casper Feb 27 '16 at 00:23