Use custom runtime for a Lambda function

Services Covered

  • lambda Lambda

Lab description

In this lab I will create a lambda Layer and set a custom runtime. I will create a Lambda Layer that contains the bootstrap configuration file needed to run Bash code in Lambda functions.

In this Lab Step, you invoked the Lambda function and created the snapshot. You also checked the existence of the snapshot. You could use the same custom runtime layer to create other Bash functions that perform tasks that are useful for your environment using the AWS CLI or other Bash commands. You can even use CloudWatch Events triggers to make the Lambda functions run on a schedule.


Learning Objectives

  • Creating Lambda Layers
  • Creating custom runtime Lambda function

Lab date

07-12-2021


Prerequisites

  • AWS account

Lab steps

  1. Navigate to the S3 Console and record the Bucket name in a file or other location where you can easily retrieve it (it was provided by the cloud academy).
  2. Navigate to the Lambda Console and Create layer.Name: RuntimeLayer
    Description: Layer for the runtime configuration
    Select Upload a zip file bash-lambda-layer.zip
  3. Navigate to functions and click on Create function

  4. In the Function overview section, click on Layers and then click Add a layer. Select Specify an ARN and paste the ARN you copied before into the Specify and ARN text-field.

  5. Move to the Code source section and delete the bootstrap.sample file by right-clicking the file, selecting Delete, and confirming.
  6. Double-click hello.sh to open the file and replace its entire contents with the following Bash function code:
    function handler () {
     # Event data is passed in as the first argument (unused in this Lab)
     EVENT_DATA=$1 
    
     # Use the ls command to list files and folders
     result=$(ls -a)
     echo $result
    
     # Return a JSON response indicating the function succeeded
     RESPONSE="{"statusCode": 200, "body": "Snapshot complete"}"
     echo $RESPONSE
    }

    To change the function’s timeout, click the Configuration tab and click Edit: The Basic settings form will load. Set the following form values: Memory (MB): 256 MB Timeout: 1 min 10 sec.

  7. Click on Test button at the top of the Code source section and fill the content as described Event name: TestMakeSnapshotBody: {}*

Lab files

Acknowledgements

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *