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}")
질문됨 6달 전1522회 조회
3개 답변
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
전문가
답변함 6달 전
profile picture
전문가
Kallu
검토됨 6달 전
  • 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
전문가
답변함 6달 전
0

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

profile pictureAWS
전문가
kentrad
답변함 6달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠