Store AWS Python Lambda Output in S3
How can you set up an S3 bucket in CDK and send JSON to it from a Python Lambda?
Imagine you’ve built a serverless application that processes massive datasets, like ZIP files full of user data. You’re struggling to store the results reliably without bloating your costs or complexity. What if you could seamlessly integrate Amazon S3 into your AWS CDK stack, use a Python Lambda to process and store outputs, and manage it all with a few lines of code?
In this guide, we’ll walk through how to do that, helping you build scalable, cost-effective storage for your serverless workflows — whether you’re archiving processed data or preparing for downstream analytics. Let’s dive into one of the cleanest ways to store data in the cloud.
Step 1 —Add an S3 Bucket to Your CDK Stack
This section assumes you already have a TypeScript CDK stack. If that’s not the case, don’t worry — this part might not apply to you yet, but stick around! The second half of the article shows you how to use an S3 bucket in your Python lambda, regardless of how it got there.
To add an S3 bucket to your stack, add the following construct:
const bucketName = "YourBucketNameHere";
// Add a bucket to store lambda outputs
const lambdaOutputsBucket = new s3.Bucket(this, "LambdaOutputsBucket", {
bucketName: bucketName,
versioned: true,
removalPolicy: cdk.RemovalPolicy.RETAIN, // <-- This policy preserves data on stack deletion
});
This will create a bucket to store all of your Lambda outputs. The construct parameters mean:
bucketName— The name of the bucket in your S3.versioned— Iftrueuploading an object to the same path will create a new version of the object, not delete the old one.removalPolicy— This is the CDK removal policy. It controls what CDK does on stack deletion. Setting it toRETAINmeans the bucket and its data will be left on AWS when you delete the stack. You can also useDESTROYwhich will delete everything, in combination withautoDeleteObjects: trueas a further input parameter.
To let your Lambda access the bucket, add the following:
// Allow the lambda function to read the bucket
lambdaOutputsBucket.grantReadWrite(yourLambda);
This will give your Lambda the IAM permissions to access the bucket.
Step 2 — Store Data in the Bucket
For this part, I’m assuming you’re using a Python Lambda. To access S3, you need to create an S3 client using boto3. You can do this with:
import boto3
s3 = boto3.client("s3")
Then, to store JSON data in a bucket, you can use the following:
try:
s3.put_object(
Bucket="<your-bucket-name-here>",
Key="<your-file-name-here>",
Body=json.dumps("<your-data-here>"),
ContentType="application/json",
)
except Exception as e:
print(e)
I’ve added a try/except to catch any issues without failing. This is useful when S3 is a nice-to-have, and an error shouldn’t stop a Lambda from returning a response.
Step 3 — Deploy Your Lambda and Test
Now you’ve updated your stack and altered your Lambda code you can re-deploy it with:
cdk deploy --hotswap
Then call it by its URL, or invoke it directly with a boto3 lambda client. Check your S3 manually in the AWS console, and you’ll see your file. That’s it, thank you for reading!