I have a bash script that I want to run through lambda function.
Can bash script be written inside a AWS Lambda function
I found this post and tried running bash script by python os.system('bash_script.sh')
. It works but the python lambda does not wait for the bash script to finish executing.
Then I was about to try the other solution provided in the post which is making the lambda function runtime Custom
.
However, since I need to deploy the custom runtime lambda function via cloudformation template, I am stuck how to point the files and deploy it correctly.
Here are files I got so far by reading docs and watching videos about it.
filename: bootstrap
#!/bin/sh
set -euo pipefail
# Handler format: <script_name>.<bash_function_name>
#
# The script file <script_name>.sh must be located at the root of your
# function's deployment package, alongside this bootstrap executable.
source $(dirname "$0")/"$(echo $_HANDLER | cut -d. -f1).sh"
while true
do
# Request the next event from the Lambda runtime
HEADERS="$(mktemp)"
EVENT_DATA=$(curl -v -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next")
INVOCATION_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)
# Execute the handler function from the script
RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")
# Send the response to Lambda runtime
curl -v -sS -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$INVOCATION_ID/response" -d "$RESPONSE"
done
filename: hello.sh
function handler () {
EVENT_DATA=$1
# more logics come in here
RESPONSE="{"statusCode": 200, "body": "Hello from Lambda!"}"
echo $RESPONSE
}
cloudformation template
CustomRuntimeLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: custom-runtime-lambda-function
Description: custom runtime lambda function
Runtime: provided
Handler: <WHAT COMES HERE??>
Role: !GetAtt LambdaRole.Arn
Timeout: 60
CodeUri: ../src
The above 2 files, bootstrap and hello.sh
are just exact same copies generated by AWS lambda Console.
So I am not sure what to insert in Handler
. in python or node we put there like app:handler
But, what about the custom runtime case? I know that Runtime has two options as provided
and provided.al2
. My goal is to just run the bash script as lambda function. And the lambda function will be called by schedule or other aws services like api-gateway or step-function.
I saw the example of custom runtime with a webpack. It was using a specific type of file Makefile
and under Metadata
in cloudformation, the path to it was specified.
If that is the configuration I am missing in my cloudformation, then what would be the contents of Makefile
in the usecase?
Any suggestion or help will be much appreciated. Thank you very much in advance.