How to Run Bash Script in AWS Lambda
Last Updated :
26 Jul, 2024
AWS Lambda is one of the strongest computing services in the serverless domain, via which developers can run their code without the need to provision or manage servers. Writing your code with AWS Lambda is key; AWS takes care of the needed infrastructure for running it. This makes it an excellent choice to build scalable, cost-effective, and highly available applications.
Bash scripting, in UNIX-based systems, is popular automation to undertake different activities. It permits you to run a list of commands into one script; that makes management of the repetitive task much easier and smooth. However, AWS Lambda does not natively support running bash scripts directly. Suppose you need to execute a bash script on an AWS Lambda platform; you can do it using the following workaround.
A supported runtime in a Lambda layer can be used, executing your bash scripts within the AWS Lambda environment. This approach should let you automate a whole range of things from data-processing jobs to system-administration tasks without being required to provision and maintain regular servers. By the end of this article, you will come to know in detail how one can run bash scripts in AWS Lambda and, of course, make the ways of accomplishing tasks automated efficiently inside a serverless environment.
Primary Terminologies
AWS Lambda:
- AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources on your behalf, you are billed for only the compute time consumed.
Serverless:
- Serverless is an execution model of cloud computing where a cloud provider dynamically manages the provisioning and allocation of servers. That explains it all for AWS Lambda: the best form of serverless computing.
Bash Script:
- A bash script is a file that contains a series of commands to be run by the Unix shell, which happens to be the Bourne Again Shell. Most commonly, Bash scripts are used to automate repetitive tasks and in system administration.
Layer:
- In AWS Lambda, a layer is an archive for libraries, custom runtimes, or other functions in order to import them into your Lambda function, layers let you manage your function code with its dependencies in a separate unit.
Runtime:
- An AWS Lambda runtime consists of the environment which executes your Lambda Function. It includes the programming language and the required libraries. Commonly used runtime languages are: Python, Node.js, Java, and Ruby.
Deployment Package:
- An AWS Lambda deployment package is a .zip file consisting of your function code along with its dependencies. This is uploaded to AWS Lambda and used as your Lambda function.
Subprocess:
- Subprocess In computing, a subprocess is a process created by another process (the parent process) to perform a specific task. A "subprocess" in the context of AWS Lambda could be executing commands out of a bash script within the Lambda environment.
IAM Role:
- IAM Role is a set of permissions regarding different actions that an entity in AWS can or cannot do. Lambda functions use roles to interact with other services in the AWS securely.
Step-by-Step Process to Run a Bash Script in AWS Lambda
Step 1: Write Your Bash Script
- Create a bash script file, e.g., script.sh, with the commands you want to execute.
Example:
#!/bin/bash
echo "Hello from the GeeksforGeeks!"
- Change permission of bash file to executable
chmod +x script.sh
Step 2: Create a Lambda Layer
Create a Deployment Package:
- AWS Lambda uses deployment packages to store your function code and dependencies. Create a zip file with your bash script and any other necessary files.
mkdir layer
cp script.sh layer/
cd layer
zip -r my-layer.zip .
- To download zip file from terminal to our desktop or computer use below command
scp -i <.keypair name.pem> ec2-user@<public ip address>:/home/ec2-user/my-lambda-function/my-lambda-function.zip /path/to/local/destination
Publish the Layer:
- Go to the AWS Lambda console.
- Navigate to "Layers" and click "Create layer".
- Upload the my-layer.zip file.
- Provide a name, description, and select a compatible runtime (e.g., Python 3.x, Node.js).
Step 3: Create the Lambda Function
Set up the Lambda Function:
- Go to the AWS Lambda console.
- Click "Create function"
- Choose "Author from scratch".
- Provide a function name, select a runtime (e.g., Python 3.x, Node.js), and click "Create function".
Add the Layer to the Function:
- In your function's configuration, navigate to the "Layers" section. Click on "Add Layer"
- Choose "Custom layers", and select the layer you created in Step 2
Step 4: Modify the Lambda Function Code
- Modify your Lambda function to execute the bash script using the appropriate runtime. For this example, we'll use Python:
import subprocess
import shutil
def lambda_handler(event, context):
# Copy the script to /tmp directory
shutil.copyfile("/opt/script.sh", "/tmp/script.sh")
# Set the executable permission to the script
subprocess.run(["chmod", "+x", "/tmp/script.sh"])
# Run the bash script
result = subprocess.run(["/bin/bash", "/tmp/script.sh"], capture_output=True, text=True)
return {
'statusCode': 200,
'body': result.stdout
}
Step 5: Test the Lambda Function
- Deploy the function: Click "Deploy" in the AWS Lambda console.
- Create a test event: Go to the "Test" tab, create a new test event with default settings, and click "Test".
- Verify the output: Check the output in the "Execution results". You should see the output of your bash script.
Conclusion
Running a bash script in AWS Lambda is a flexible way to automate any task within a serverless space. Though this is not supported by AWS Lambda natively, this can be achieved with Lambda layers and a runtime supported by it. It's an excellent way to handle repetitive tasks, data processing, and system administration without the overhead of managing servers. We have discussed terms such as AWS Lambda, serverless computing, bash scripts, layers, runtimes, deployment packages, subprocesses, and IAM roles in this document. We also elaborated on a detailed step-by-step approach: how to write your own bash script, creating a Lambda layer, setting up your Lambda function, and making changes in the function code so you can run your script. By following these, you can harness the power of AWS Lambda for running your Bash scripts such that it does refine and enhance your workflows for more automation and fastness. This makes you more competent in many a task without necessarily maintaining conventional server infrastructures, hence bringing productivity with saving cost.
Similar Reads
How To Run Bash Script In Linux? Bash scripts, also called shell scripts, are programs that help automate tasks by storing a series of commands that often go together, like updates or upgrades. These scripts make it easier to perform tasks automatically instead of typing each command manually. After creating a Bash script, you can
6 min read
How to Run Bash Script in Github Actions ? GitHub Actions are helpful resources for coding. They automate processes in your GitHub projects, save you time and effort. It is possible that GitHub Actions will automate the steps involved in testing, deploying, and alerting users of your code. Because you can configure them to run at specified t
5 min read
How to Load NPM Modules in AWS Lambda? AWS Lambda is a serverless computing service within the event-driven model that Amazon Web Services provides; it is purposed for the execution of code without server provisioning or management. It automatically scales your application to handle from a few requests per day to thousands. This integrab
6 min read
How to Test AWS Lambda Locally AWS Lambda is a high-powered, serverless computing service that enables developers to run code without provisioning or managing servers. This service automatically scales, manages infrastructure, and charges only for the compute time consumed. However, developing and testing Lambda functions directl
8 min read
How to Stop AWS Lambda Execution AWS Lambda is a serverless computing service provided by Amazon Web Services that allows developers to run code without the need to provision or manage servers. It is designed to automatically scale with usage, making it highly efficient and cost-effective. It simplifies the process of building and
4 min read
How To Create Cron Job In AWS Lambda? A Cron job works like scheduling a task in any system. It is mainly used for backups, system maintenance, etc. Cron's job works on both local systems as well as cloud services. To run the crown job in AWS, we have to use AWS Lambda. In AWS Lambda, we set up the functions and schedule a time to run t
7 min read
How to Install Python Packages for AWS Lambda Layers? AWS Lambda Layer is a zip file archive that contains the required additional code (libraries, dependencies, or custom runtimes) or data to run your AWS Lambda function. AWS Lambda function can then pull this required content in form of lambda layers. When an AWS Lambda function is invoked, the layer
5 min read
How to Fix 'No Module Named psycopg2' in Python AWS AWS Lambda is a powerful serverless computing service that allows you to run code without provisioning or managing servers. However, running Python code on AWS Lambda can sometimes lead to module import errors, such as the infamous "No module named psycopg2." This article will explore the reasons be
3 min read
How to Set Environment Variable in AWS Lambda ? AWS Lambda is a managed serverless deployment service of AWS that can be used for the implementation of various applications in the cloud. AWS lambda may use various environment variables for the execution of different operations. eg. Database Credentials, API Keys .etc. There are various ways we ca
4 min read
How to get AWS Account Id in Lambda AWS Lambda is a FaaS (Function as a Service) provided by Amazon Web Services. It is a compute service which can be used to run code in response to an event without provisioning or managing servers making it an optimal choice for creating event-driven serverless applications. AWS Lambda provides high
6 min read