Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch a shell script from Lambda in AWS

If I have a bash script sitting in an EC2 instance, is there a way that lambda could trigger it?

The trigger for lambda would be coming from RDS. So a table in mysql gets updated and a specific column in that table gets updated to "Ready", Lambda would have to pull the ID of that row with a "Ready" status and send that ID to the bash script.

like image 644
lightweight Avatar asked Feb 01 '16 14:02

lightweight


People also ask

Can AWS Lambda run shell scripts?

AWS recently announced the "Lambda Runtime API and Lambda Layers", two new features that enable developers to build custom runtimes. So, it's now possibile to directly run even bash scripts in Lambda without hacks. This actually opens up the possibility to run any programming language within a Lambda.

Can I ssh from AWS Lambda?

In addition, you can use AWS Lambda to connect to your Linux instances by using SSH and run desired commands and scripts at regular time intervals.

How do I run AWS scripts?

Run a shell script from Amazon S3Open the AWS Systems Manager console at https://console.aws.amazon.com/systems-manager/ . In the navigation pane, choose Run Command. If the AWS Systems Manager home page opens first, choose the menu icon ( ) to open the navigation pane, and then choose Run Command. Choose Run command.


2 Answers

Let's assume some things. First, you know how to set up a "trigger" using sns (see here) and how to hang a lambda script off of said trigger. Secondly, you know a little about python (Lambda's syntax offerings are Node, Java, and Python) because this example will be in Python. Additionally, I will not cover how to query a database with mysql. You did not mention whether your RDS instance was MySQL, Postgress, or otherwise. Lastly, you need to understand how to allow permission across AWS resources with IAM roles and policies.

The following script will at least outline the method of firing a script to your instance (you'll have to figure out how to query for relevant information or pass that information into the SNS topic), and then run the shell command on an instance you specify.

import boto3
def lambda_handler(event, context):
     #query RDS to get ID or get from SNS topic
     id = *queryresult*
     command = 'sh /path/to/scriptoninstance' + id
     ssm = boto3.client('ssm')
     ssmresponse = ssm.send_command(InstanceIds=['i-instanceid'], DocumentName='AWS-RunShellScript', Parameters= { 'commands': [command] } ) 

I would probably have two flags for the RDS row. One that says 'ready' and one that says 'identified'. So SNS topic triggers lambda script, lambda script looks for rows with 'ready' = true and 'identified' = false, change 'identified' to true (to make sure other lambda scripts that could be running at the same time aren't going to pick it up), then fire script. If script doesn't run successfully, change 'identified' back to false to make sure your data stays valid.

like image 144
ncarmona Avatar answered Sep 26 '22 16:09

ncarmona


Using Amazon EC2 Simple Systems Manager, you can configure an SSM document to run a script on an instance, and pass that script a parameter. The Lambda instance would need to run the SSM send-command, targeting the instance by its instance id.

Sample SSM document: run_my_example.json:

{
  "schemaVersion": "1.2",
  "description": "Run shell script to launch.",
  "parameters": {
         "taskId":{
            "type":"String",
            "default":"",
            "description":"(Required) the Id of the task to run",
            "maxChars":16
        }
  },
  "runtimeConfig": {
    "aws:runShellScript": {
      "properties": [
        {
          "id": "0.aws:runShellScript",
          "runCommand": ["run_my_example.sh"]
        }
      ]
    }
  }
}

The above SSM document accepts taskId as a parameter.

Save this document as a JSON file, and call create-document using the AWS CLI:

aws ssm create-document --content file:///tmp/run_my_example.json --name  "run_my_example"

You can review the description of the SSM document by calling describe-document:

aws ssm describe-document --name "run_my_example"

You can specify the taskId parameter and run the command by using the document name with the send-command

aws ssm send-command --instance-ids i-12345678 --document-name "run_my_example" --parameters --taskid=123456

NOTES

  • Instances must be running the latest version of the SSM agent.

  • You will need to have some logic in the Lambda script to identify the instance ids of the server EG look up the instance id of a specifically tagged instance.

like image 28
Rodrigo Murillo Avatar answered Sep 25 '22 16:09

Rodrigo Murillo