DEV Community

Cover image for AWS official lambda layers with AWS CDK
Mick Jacobsson for AWS Community Builders

Posted on • Originally published at talkncloud.com

AWS official lambda layers with AWS CDK

This is my first official post since joining the AWS community builders and well it's not super exciting but none the less it was a source of frustration for me so it might be for others...lambda layers.

If you've been writing lambda functions for a while, maybe before layers, you might not be aware of layers. The basic idea of layers is to separate out your code so that you can better share and reuse your functions. For most people this might be libraries or modules, python is a good example where you might have several common libraries across many functions.

This sounds pretty basic, what gives?

It gets a little more complicated when you have different environments, AWS has a specific environment and for the most part it's not a problem. But, every now and then you'll come across a module that isn't compatible. Like I did today. AWS has created a few public lambda layers so now I'll walk through how to use them.

Finding the Lambda Layer AWS Resource Name (ARN)

If you head over to the AWS management console and go to Lambda and then click on an existing function. If you don't have a function just make a dummy one for now. Once you click on your function, head to the very bottom of the page and you'll see a section for layers.

alt text

Now you can select Add a layer

The very first screen that you go to next will let you choose from the very few AWS official layers, custom or by ARN. The options are pretty straight forward:

  1. AWS created layers
  2. Layers created by you / your org
  3. Layers shared or accessible external e.g. community / public

alt text

In my case, I wanted numpy which is part of the AWSLambda-Python38-SciPy1x layer. Select the version (I've only ever seen 1 version at a time, anyone else noticed multiple versions?) go ahead and select add.

You should be redirected back to the console and now you can see your new lambda added to your lambda function with a new ARN:

alt text

Keep that ARN, we'll come back to that...

CDK to the rescue!

Now we are finally getting to the CDK, I'm going to assume you have a CDK project already with Lambda function and like me you're trying to add a Lambda layer from an ARN like the AWS official layer above.

If you don't have a CDK project or you're unsure you can check one of my previous posts, open source on github (look in the lib/lambda folder):

https://github.com/talkncloud/aws/tree/main/athena-appsync

If you do have some code, here is an example of adding the layer:

myLambdaFunction.addLayers(
    lambda.LayerVersion.fromLayerVersionArn(this, 'awsNumpyLayer', 'arn:aws:lambda:ap-southeast-2:817496625479:layer:AWSLambda-Python38-SciPy1x:29')
)
Enter fullscreen mode Exit fullscreen mode

That's it! Now if you deploy your stack you'll have the SciPy AWS Official Lambda layer added to your function. If you're wondering if you need to do anything special in your function, no, just import the module as per normal and it will be accessible in your function.

Can we skip some steps?

Look, I mean, I agree, if you just want to use numpy this sounds like way too many steps. I did go searching for a list of AWS layers and didn't get very far. If you know of a list reach out, I'd be keen to have a look. When you start searching out there you'll find various ARN's for different layers but it seems like it's all over the place.

There is another reason for doing this rather than creating you're own, this particular package, numpy needs to be compiled using the AWS environment rather than just uploading it with your function. You can create your own layers with the AWS Linux Container (maybe another post?) but if you're in a bind and just need numpy this will get you out of a jam.

Wrapping up

Hopefully this will be of use to someone, personally it just wasn't obvious to me how to get the AWS official ARN and then use it in CDK. I was actually thinking i'd be able to reference the name (alias?) somehow in CDK but once you have the ARN it makes more sense. If you are using layers quite heavily I'd be interested to hear from anyone on their workflow for building modules that are Lambda friendly.

Top comments (0)