Can bash script be written inside a AWS Lambda function

后端 未结 7 1904
北荒
北荒 2020-12-05 09:40

Can I write a bash script inside a Lambda function? I read in the aws docs that it can execute code written in Python, NodeJS and Java 8.

It is mentioned in some doc

7条回答
  •  生来不讨喜
    2020-12-05 10:25

    AWS supports custom runtimes now based on this announcement here. I already tested bash script and it worked. All you need is to create a new lambda and choose runtime of type Custom it will create the following file structure:

    mylambda_func
     |- bootstrap 
     |- function.sh 
    

    Example Bootstrap:

    #!/bin/sh
    
    set -euo pipefail
    
    # Handler format: .
    # The script file .sh  must be located in
    # the same directory as the 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
    

    Example handler.sh:

    function handler () {
        EVENT_DATA=$1
    
        RESPONSE="{\"statusCode\": 200, \"body\": \"Hello from Lambda!\"}"
        echo $RESPONSE
    }
    

    P.S. However in some cases you can't achieve what's needed because of the environment restrictions, such cases need AWS Systems Manager to Run command, OpsWork (Chef/Puppet) based on what you're more familiar with or periodically using ScheduledTasks in ECS cluster.

    More Information about bash and how to zip and publish it, please check the following links:

    • https://docs.aws.amazon.com/en_us/lambda/latest/dg/runtimes-walkthrough.html
    • https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html

提交回复
热议问题