- Newest
- Most votes
- Most comments
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": "*"
}
]
}
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
This also walks through the steps to create a custom runtime to execute a bash script. Tutorial – Publishing a custom runtime
Relevant content
- Accepted Answerasked 8 months ago
- Accepted Answerasked 2 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 2 years 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.