Lesson 3 of 3 AWS Lambda for Data Engineering

Choosing Storage and Packaging for Data Processing Functions

This lesson explains the Lambda storage and packaging options for functions that need large libraries, temporary space for files or shared persistent storage, and how to stay within size limits.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose between /tmp, Amazon EFS and Amazon S3 for storage.
  • Choose between .zip packages, layers and container images.
  • Apply good practices for data processing functions.

Storage options

A function can use three kinds of storage. The /tmp directory is local disk space of 512 MB to 10 GB in each execution environment. It is fast but temporary and not shared, so it suits downloading a file, processing it and uploading the result within one invocation. Amazon EFS is a persistent file system shared by every invocation and every function that mounts it, which suits large reference data or ML models; the function must be connected to a VPC to reach it. Amazon S3 is durable object storage read and written through the SDK, and is where a pipeline's input and output data normally live. The following table summarises the options.

StorageCharacteristicsUse for
/tmp512 MB to 10 GB per execution environment; temporaryDownloading and processing files within one invocation
Amazon EFSPersistent, shared across invocations and functions; the function must be in a VPCLarge reference data or models shared by many functions
Amazon S3Durable object storage, accessed through the SDKInput and output data

The following diagram shows where each kind of storage sits relative to a function.

The three kinds of storage a function can use. Each execution environment has its own temporary /tmp space of 512 MB to 10 GB. Amazon EFS is a persistent file system shared by every function that mounts it, through a VPC. Amazon S3 holds the pipeline's input and output data, read and written through the SDK.
S3 is reached through the SDK; a function in a VPC needs an endpoint or NAT gateway for it.
Sort into groups

Which Lambda storage?

Exam skills 1.4.7

Choose the storage for each need.

Log in to try this activity Interactive activities are free with an account.

Packaging options

Data processing functions often need large libraries, and the packaging choice depends on their size. A .zip package can hold up to 250 MB unzipped, including its layers, which is enough for most functions. Layers package shared libraries separately, up to five per function, so many functions can use, for example, the AWS SDK for pandas without each bundling it. When dependencies exceed 250 MB, such as pandas with PyArrow or an ML model, a container image of up to 10 GB is the answer. The following table summarises the options.

OptionLimitUse for
.zip package250 MB unzipped, including layersMost functions
LayersUp to five per function, within the 250 MB limitSharing libraries such as the AWS SDK for pandas
Container imageUp to 10 GBLarge dependencies such as pandas, PyArrow or ML models

The following diagram shows the chosen packaging for a Lambda function that converts Excel workbooks to Parquet, and the alternatives a team considered.

The Excel-to-Parquet function after the fix. The .zip package failed because pandas and PyArrow exceed 250 MB unzipped, so the function uses the AWS-provided layer for the AWS SDK for pandas. Each workbook is downloaded to /tmp, raised to 2 GB, converted and written to S3 as Parquet.
The layer includes pandas and PyArrow.

250MB

Unzipped .zip package, including layers

10GB

Maximum container image

Scenario questions

Packaging a function

Exam skills 1.4.6 · 1.4.7

Choose how to package each function.

Log in to try this activity Interactive activities are free with an account.

Good practice

Lambda functions in data pipelines follow the same good practices as any Lambda function, with a few that matter especially for data. SDK clients and database connections are created outside the handler, so that every invocation in the same execution environment reuses them instead of reconnecting. Functions are deployed with AWS SAM or the AWS CDK, so their configuration is versioned with the rest of the pipeline.

Functions attached to a VPC have no internet access, so they need VPC endpoints or a NAT gateway to reach AWS APIs such as Amazon S3 and AWS Glue. Finally, functions must be idempotent, because event sources deliver events at least once and a function may process the same event twice.

Spot the bug

A function that reconnects every time

Exam skills 1.4.1 · 1.4.4

Find the lines that slow the function and risk duplicates.

Log in to try this activity Interactive activities are free with an account.
Worked example

Packaging pandas and PyArrow

A function converts small uploaded Excel files to Parquet with pandas and PyArrow. The .zip deployment fails because the libraries exceed 250 MB unzipped. The options compare as follows.

OptionResult
Layer with the AWS SDK for pandasFits within the limit for many uses; managed by AWS
Container imageUp to 10 GB; any libraries; built and pushed to ECR
Moving the work to Glue Python shellPossible, but slower to start for small files

The team uses the AWS-provided layer for the AWS SDK for pandas, which includes pandas and PyArrow. Each file is downloaded to /tmp, raised to 2 GB for the largest workbooks, converted, and written to S3.

Check yourself

Several functions need the same files, which must persist between invocations. What should they use?

Show answer

Amazon EFS, mounted by functions connected to the VPC.

Exam Tips

Remember for the exam

  • For shared, persistent files across functions, mount EFS, with the function in a VPC.
  • Large dependencies such as pandas or ML models belong in a container image of up to 10 GB.
  • To share libraries between functions, use layers.
  • Ephemeral /tmp storage can be raised to 10 GB.
  • Create clients and connections outside the handler.