Using a Lambda layer

Let's have a go at creating our own Lambda layer using the console and then see if we can share it with another account. For this example, we're going to create a layer in Python that simply passes a message back to our handler. Let's get started:

  1. We'll start by writing the code that will make up our layer. The following is a super simple function that returns a message:
def provide_message():
return "Hello Lambda learners!"
  1. Save that file as message_vendor.py, in a folder called python. This is important because our layer gets extracted into the /opt directory. The directory is automatically added to the path variable, making it easily accessible to our actual function. We must make sure we are following the directory structure that's required for our runtime.
  2. ZIP up the directory and file ready for upload. The following command will help you here:
zip -r layer.zip python/
  1. Jump into the Lambda console, navigate to the Layers page, and click Create layer.
  1. Fill in the layer configuration and upload your ZIP file. The compatible runtimes option is to make things easier when you're choosing from the list of layers to add to your function from the console. It doesn't serve much purpose other than that. The following screenshot gives you an idea about what your layer configuration should look like:

Lambda console  creating a layer for a Python function
  1. After you've created the layer, we next thing we need to create is the function. Like you've done already, create a Python 3.7 function from scratch and jump into the configuration screen. We're going to attach our layer to our new function.
  2. As you can see, we don't have any layers attached at the moment, so let's open the Layers configuration page and click Add a layer. The arrows in the following screenshot show where you can find the necessary buttons:

Lambda console showing where to find the layer configurations
  1. In the Layer selection screen, we can see that there are two options. The first is that we can pick from a list of layers that are available to us in our account already, including the useful layer that AWS provides. The second option is that we can also enter an ARN of another layer that might exist in another account.
  2. Attach our new layer and Save the function configuration.
  1. Next, we'll replace the boilerplate function code with our own. Use the following function in place of the default Lambda function code:
import message_vendor as msg

def lambda_handler(event, context):
message = msg.provide_message()
return {
'statusCode': 200,
'body': message
}

What we're doing here is importing the layer as a new variable and then calling the function inside the layer to retrieve a message. Go ahead and test it to see if it works! 

Assuming the execution ran successfully, you should be treated to a green response in the console with our returned data, as shown in the following example:

A snippet of a successful function execution using layers

As an extension exercise, to show that a layer can be used across multiple functions, you can use another Lambda function and test that they can both be invoked simultaneously. 

You can add multiple lambda layers to a function and also specify the order they are processed in. Be careful, though – a layer that is applied later in the order can override files that have already been extracted to /opt.

If you're developing Lambda functions for a larger organization, you may be working in a multi-account environment. It would be helpful if we could centralize where the layers were deployed to or hosted. Then, we could share access to the accounts that needed it. With this approach, we can reduce the amount of code we need to deploy, thus avoiding duplication, sprawl, and versioning nightmares. In the next section, we'll explain how to add permissions so that other accounts can access a central Lambda layer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.226.79