How do I run a shell script in a Lambda function?

0

I have a '.sh' file in my s3 storage to be run via a lambda function using Python 3.11. I found a code snippet shared below but it throws an error saying that there is no path as such. What's the correct way of running a shell script using Python inside a Lambda function?

import subprocess
try:
    subprocess.run(script_path, shell=True)
except Exception as e:
    print(f"Error running shell script: {e}")
asked 5 months ago1368 views
3 Answers
0

Hello.

If a shell script file exists in S3, I think it is necessary to retrieve the shell script from S3 using download_file() and then execute it.

The code below downloads a shell script from S3 to Lambda's temporary folder.
I also run the "chmod" command to change the permissions of the shell script.

import subprocess
import boto3

s3 = boto3.client('s3')
bucket_name = 'testbucket'
key = 'example.sh'
script_path = '/tmp/local_example.sh'


def lambda_handler(event, context):

    s3.download_file(bucket_name, key, script_path)
    try:
        subprocess.run(["chmod","755",script_path])
        subprocess.run(script_path, shell=True)
    except Exception as e:
        print(f"Error running shell script: {e}")

Please set the following IAM policy for the Lambda IAM role to download the shell script from S3.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "s3:GetObject",
            "Resource": "*"
        }
    ]
}
profile picture
EXPERT
answered 5 months ago
profile picture
EXPERT
Kallu
reviewed 5 months ago
  • Would I require to create an IAM role for Lambda to access S3 storage? If yes, what permissions do I give?

  • Yes, you need to configure an IAM policy for Lambda to access S3. Attach the following IAM policy to the Lambda IAM role.

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "VisualEditor0",
                "Effect": "Allow",
                "Action": "s3:GetObject",
                "Resource": "*"
            }
        ]
    }
    
0

Hi,

May I advertise one of my personal projects ? https://github.com/didier-durand/lambda-cobol

It creates a Custom Lambda runtime able to run a bash shell to launch the Cobol program. So, if you replicate this setup, you will be able to run any shell script.

Have a look at lambda-cobol.sh and how it is triggered to replicate that in your own use case.

Best,

Didier

profile pictureAWS
EXPERT
answered 5 months ago
0

This also walks through the steps to create a custom runtime to execute a bash script. Tutorial – Publishing a custom runtime

profile pictureAWS
EXPERT
kentrad
answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions