AWS Lambda
S3
file creation
serverless computing
cloud storage

Is it possible to create a file in my S3 lambda function?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, an AWS Lambda function can create a file during execution. The important detail is where that file lives: Lambda can write temporary files to its local /tmp directory, and it can upload objects to Amazon S3 through the AWS API.

That distinction matters because S3 is object storage, not a mounted filesystem. In practice, the usual flow is to build a file in /tmp, then upload it to a bucket.

Lambda Filesystem Versus S3

When Lambda starts your function, it gives the runtime a small writable area on the local execution environment. That writable location is /tmp. Anything outside that path should be treated as read-only unless the runtime documentation says otherwise.

S3 works differently. You do not "open a file in S3" with normal filesystem calls. Instead, you create bytes in memory or on disk and send them with the SDK using operations such as put_object or upload_file.

That leads to two common patterns:

  1. Create a temporary file in /tmp, then upload it.
  2. Skip the local file and upload a string or byte buffer directly.

The first pattern is useful when another library expects a real file path. The second is simpler for JSON, CSV, text, or small binary payloads.

Writing a Temporary File in Lambda

The example below creates a CSV report in /tmp/report.csv. It then returns the path so the caller can upload it or inspect it.

python
1from pathlib import Path
2import csv
3
4
5def build_report_file() -> str:
6    rows = [
7        ["name", "score"],
8        ["Ada", 98],
9        ["Linus", 93],
10        ["Grace", 100],
11    ]
12
13    output_path = Path("/tmp/report.csv")
14
15    with output_path.open("w", newline="", encoding="utf-8") as handle:
16        writer = csv.writer(handle)
17        writer.writerows(rows)
18
19    return str(output_path)
20
21
22if __name__ == "__main__":
23    print(build_report_file())

This works because /tmp is writable during the function invocation. The file is temporary, so you should not rely on it being there for the next invocation even if Lambda reuses the execution environment.

Uploading the Result to S3

Once the file exists, upload it with boto3. The example below is a complete Lambda handler that generates a file and stores it in a bucket. It expects the bucket name in the REPORT_BUCKET environment variable.

python
1import os
2from pathlib import Path
3import csv
4import boto3
5
6s3 = boto3.client("s3")
7
8
9def lambda_handler(event, context):
10    bucket = os.environ["REPORT_BUCKET"]
11    key = "reports/report.csv"
12    path = Path("/tmp/report.csv")
13
14    with path.open("w", newline="", encoding="utf-8") as handle:
15        writer = csv.writer(handle)
16        writer.writerow(["name", "score"])
17        writer.writerow(["Ada", 98])
18        writer.writerow(["Grace", 100])
19
20    s3.upload_file(str(path), bucket, key)
21
22    return {
23        "bucket": bucket,
24        "key": key,
25        "message": "report uploaded"
26    }

For small payloads, you can avoid local disk entirely:

python
1import boto3
2
3s3 = boto3.client("s3")
4
5
6def upload_text(bucket: str, key: str, body: str) -> None:
7    s3.put_object(
8        Bucket=bucket,
9        Key=key,
10        Body=body.encode("utf-8"),
11        ContentType="text/plain; charset=utf-8",
12    )

This second approach is usually cleaner when the content already exists in memory.

Common Pitfalls

The most common mistake is treating S3 like a normal disk path. A call such as open("s3://my-bucket/file.txt", "w") will not work unless you have added a separate library that simulates filesystem semantics.

Another frequent problem is writing to the wrong location. In Lambda, /tmp is the safe writable path. If your code writes to the current working directory or to an application folder, it will often fail with a permissions error.

Permissions are also easy to miss. Your function role needs S3 permissions such as s3:PutObject for the target bucket. If encryption or bucket policies are involved, you may also need kms:Encrypt or additional bucket-level permissions.

It is also risky to assume temporary files are durable. Lambda execution environments can be recycled at any time, so /tmp should be treated as scratch space, not long-term storage.

Finally, be careful with file size. If the generated artifact is large, building it entirely in memory may cause memory pressure, while writing a large local file may exceed available ephemeral storage. In those cases, stream data in chunks or write directly to S3 using multipart upload patterns.

Summary

  • Lambda can create files locally, but the writable path is typically /tmp.
  • S3 is object storage, so uploads happen through the AWS SDK rather than normal filesystem writes.
  • Use /tmp when a library needs a real file path.
  • Use put_object when you can upload data directly from memory.
  • Make sure the Lambda execution role has the right S3 permissions.
  • Treat local files as temporary and disposable.

Course illustration
Course illustration

All Rights Reserved.