|
| 1 | +# AWS Lambda with Amazon S3 Files Mount |
| 2 | + |
| 3 | +This pattern deploys an AWS Lambda function with an Amazon S3 Files file system mounted as a local directory, enabling standard file operations on S3 data without downloading objects. |
| 4 | + |
| 5 | +Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-s3-files-cdk |
| 6 | + |
| 7 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +- [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The AWS IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 12 | +- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 13 | +- [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 14 | +- [Node and NPM](https://nodejs.org/en/download/) installed |
| 15 | +- [AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/cli.html) installed |
| 16 | + |
| 17 | +## Deployment Instructions |
| 18 | + |
| 19 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 20 | + ```bash |
| 21 | + git clone https://github.com/aws-samples/serverless-patterns |
| 22 | + ``` |
| 23 | +2. Change directory to the pattern directory: |
| 24 | + ```bash |
| 25 | + cd serverless-patterns/lambda-s3-files-cdk |
| 26 | + ``` |
| 27 | +3. Install CDK dependencies: |
| 28 | + ```bash |
| 29 | + npm install |
| 30 | + ``` |
| 31 | +4. Deploy the stack: |
| 32 | + ```bash |
| 33 | + cdk deploy |
| 34 | + ``` |
| 35 | + |
| 36 | +## How it works |
| 37 | + |
| 38 | +[Amazon S3 Files](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-files.html) provides NFS access to S3 buckets with full POSIX semantics. This pattern mounts an S3 bucket on a Lambda function at `/mnt/s3data`. |
| 39 | + |
| 40 | +### What gets deployed |
| 41 | + |
| 42 | +| Resource | Purpose | |
| 43 | +|---|---| |
| 44 | +| S3 Bucket | Data store backing the file system | |
| 45 | +| VPC (2 AZs) | Network for Lambda and mount targets | |
| 46 | +| S3 Files FileSystem | NFS file system linked to the S3 bucket | |
| 47 | +| S3 Files MountTargets | Network endpoints in each private subnet | |
| 48 | +| S3 Files AccessPoint | Application entry point (UID/GID 1000, root `/lambda`) | |
| 49 | +| Security Group | Allows NFS traffic (port 2049) | |
| 50 | +| Lambda Function | Reads, writes, and lists files via the mount | |
| 51 | + |
| 52 | +### Architecture |
| 53 | + |
| 54 | +``` |
| 55 | +┌──────────┐ ┌─────────────────────────────────────────┐ |
| 56 | +│ S3 Bucket│◄───►│ S3 Files FileSystem │ |
| 57 | +└──────────┘ │ (auto-sync between S3 and filesystem) │ |
| 58 | + └──────────────┬────────────────────────────┘ |
| 59 | + │ NFS (port 2049) |
| 60 | + ┌──────────────┴────────────────────────────┐ |
| 61 | + │ VPC │ |
| 62 | + │ ┌────────────────┐ ┌────────────────┐ │ |
| 63 | + │ │ Mount Target │ │ Mount Target │ │ |
| 64 | + │ │ (AZ-1) │ │ (AZ-2) │ │ |
| 65 | + │ └────────────────┘ └────────────────┘ │ |
| 66 | + │ ▲ │ |
| 67 | + │ │ │ |
| 68 | + │ ┌────────┴───────┐ │ |
| 69 | + │ │ Lambda Function│ │ |
| 70 | + │ │ /mnt/s3data │ │ |
| 71 | + │ └────────────────┘ │ |
| 72 | + └────────────────────────────────────────────┘ |
| 73 | +``` |
| 74 | + |
| 75 | +### Key S3 Files concepts |
| 76 | + |
| 77 | +- **FileSystem** — A shared file system linked to your S3 bucket. Changes sync bidirectionally. |
| 78 | +- **MountTarget** — Network endpoint in a specific AZ. Lambda must be in the same VPC/subnet. |
| 79 | +- **AccessPoint** — Application-specific entry point with POSIX user identity and root directory. |
| 80 | +- **High-performance storage** — Actively used data cached locally for sub-millisecond latency. |
| 81 | + |
| 82 | +## Testing |
| 83 | + |
| 84 | +1. After deployment, note the `FunctionName` and `BucketName` outputs. |
| 85 | + |
| 86 | +2. **Write a file** through the Lambda mount: |
| 87 | + ```bash |
| 88 | + aws lambda invoke \ |
| 89 | + --function-name <FunctionName> \ |
| 90 | + --payload '{"action": "write", "filename": "hello.txt", "content": "Hello from Lambda via S3 Files!"}' \ |
| 91 | + --cli-binary-format raw-in-base64-out \ |
| 92 | + output.json |
| 93 | + |
| 94 | + cat output.json |
| 95 | + ``` |
| 96 | + |
| 97 | +3. **Verify the file appeared in S3** (sync takes ~1 minute): |
| 98 | + ```bash |
| 99 | + aws s3 ls s3://<BucketName>/lambda/ |
| 100 | + ``` |
| 101 | + |
| 102 | +4. **Read the file** back through Lambda: |
| 103 | + ```bash |
| 104 | + aws lambda invoke \ |
| 105 | + --function-name <FunctionName> \ |
| 106 | + --payload '{"action": "read", "filename": "hello.txt"}' \ |
| 107 | + --cli-binary-format raw-in-base64-out \ |
| 108 | + output.json |
| 109 | + |
| 110 | + cat output.json |
| 111 | + ``` |
| 112 | + |
| 113 | +5. **List directory** contents: |
| 114 | + ```bash |
| 115 | + aws lambda invoke \ |
| 116 | + --function-name <FunctionName> \ |
| 117 | + --payload '{"action": "list"}' \ |
| 118 | + --cli-binary-format raw-in-base64-out \ |
| 119 | + output.json |
| 120 | + |
| 121 | + cat output.json |
| 122 | + ``` |
| 123 | + |
| 124 | +## Cleanup |
| 125 | + |
| 126 | +```bash |
| 127 | +cdk destroy |
| 128 | +``` |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 133 | + |
| 134 | +SPDX-License-Identifier: MIT-0 |
0 commit comments