Learn How to run Bash script with AWS Lambda

Learn How to run Bash script with AWS Lambda

In this Article learn how you can easily run Bash scripts with AWS Lambda

Open console.aws.amazon.com/lambda/

Create a Function

create a new script file in the console with any name and adjust the name in the below handler

import os
import json
import subprocess

def lambda_handler(event, context):
    body = event.get('body', '')
    # Execute backup script
    #change file name 
    subprocess.check_call(["cp ./backup.sh /tmp/backup.sh && chmod 755 /tmp/backup.sh"], shell=True)
    subprocess.check_call(["/tmp/backup.sh", "myfoldername" ])


    return {
            'statusCode': 200,
            'body': 'Bash script Run successfully'
    }

same file backup.sh

#!/bin/bash
# Input Variables
FOLDERNAME=$1

# we can Use an environment variable here pass in lambda

echo $FOLDERNAME  > /tmp/echoresult.txt

ls -l

cat /tmp/echoresult.txt