DEV Community

Cover image for How to save list on S3 using lambda function and boto3
Wojciech Lepczyński for AWS Community Builders

Posted on • Edited on

8 2 1 2

How to save list on S3 using lambda function and boto3

If you want to save a list on Amazon S3 using a Lambda function and Boto3, you can follow these steps:

1 First, you need to create a new Lambda function in the AWS Console. Choose the Python runtime and give it an appropriate name.
2 In the Lambda function code, you will need to import the Boto3 library by adding the following code at the beginning of your function:

import boto3

Enter fullscreen mode Exit fullscreen mode

3 Next, you need to create a connection to S3 using the Boto3 client. To do this, add the following code to your function:

s3_client = boto3.client('s3', 'eu-central-1')

Enter fullscreen mode Exit fullscreen mode

4 Now you can use the put_object method to save your list to S3. For example, if your list is called my_list and you want to save it to a bucket named bucket_name and a file named my_list.txt, you can use the following code:

s3_client.put_object(Bucket='bucket_name', Key='my_list.txt', Body=str(my_list))

Enter fullscreen mode Exit fullscreen mode

5 Finally, save your Lambda function and test it by invoking it. You should now see your list saved to S3 in the specified bucket and file name.

Full code:

import json
import boto3

def lambda_handler(event, context):

    bucket_name = "save-data-from-lambda"
    list001 = ['this', 'is', 'first', 'list']

    s3_client = boto3.client('s3', 'eu-central-1')
    # save to s3
    save_to_s3 = s3_client.put_object(
        Bucket=bucket_name, 
        Key='my_list.txt', 
        Body=str(list001)
    )

Enter fullscreen mode Exit fullscreen mode

AWS Lambda Function

That's it! You now know how to save a list to S3 using a Lambda function and Boto3.

I also prepared a video in which you will learn more details. You will learn how to save and read files from S3 bucket:

If you are interested, I also encourage you to visit my blog where you will find the code that you can easily copy and adapt to your needs:
https://lepczynski.it/en/aws_en/how-to-read-and-write-a-file-on-s3-using-lambda-function-and-boto3/

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay