Ingest from Amazon S3


In this guide you’ll learn how to automatically synchronize all the CSV files in an Amazon S3 to a Tinybird Data Source.

Perform a once-off load

Often when you’re building a new use-case with Tinybird, you’ll want to load some historical data that comes from another system. You call this ‘seeding’ or ‘backfilling’.

A very common pattern for exporting historical data is to create a dump of CSV files onto an Amazon S3 bucket. Once you have your CSV files, you need to ingest these into Tinybird.

You can append these files to a Data Source in Tinybird using the Data Sources API.

Let’s assume we have a set of CSV files in our S3 bucket:

To ingest a single file, you can generate a signed URL in AWS, and simply send the URL to the Data Sources API using the append mode flag.

For example:

curl -H "Authorization: Bearer <your_auth_token>" \
   -X POST "<my_data_source_name>&mode=append" \
   --data-urlencode "url=<my_s3_file_http_url>"

However, if you want to send many files, you probably don’t want to manually write each cURL. So, we can use a simple script to iterate over our files in the bucket and generate the cURL commands automatically.

This script requires the AWS CLI and assumes you have already created your Tinybird Data Source.

You can use the AWS CLI to list the files in the bucket, extract the name of the CSV file and create a signed URL. Then, we can generate a cURL to send the signed URL to Tinybird.

To avoid hitting API rate limits you should delay 15 seconds between each request.

Here’s an example script in bash:

The script uses the following variables:

  • TB_HOST as the corresponding URL for your region. We currently have: for the US region, and for the EU region.

  • TB_TOKEN as a Tinybird auth token with DATASOURCE:CREATE or DATASOURCE:APPEND scope. See the Token APIfor more information.

  • BUCKET as the S3 URI of the bucket containing the events CSV files.

  • DESTINATION_DATA_SOURCE as the name of the Data Source in Tinybird.

Automatically sync files with AWS Lambda

While the previously described scenario consisted of a one time dump of CSV files in a bucket to Tinybird, a different and more interesting scenario consists of appending to a Data Source each time a new CSV file is dropped into a bucket.

That way you can have your ETL process exporting some data from your Data Warehouse (such as Snowflake or BigQuery) or any other origin and you can forget about synchronizing those files to Tinybird.

This can be achieved using AWS Lambda.

Imagine you have an S3 bucket named s3://automatic-ingestion-poc/ and each time you put a CSV there you want to sync it automatically to an events Data Source in Tinybird.

This is how you do it:

  • Clone this GitHub repository

  • Install the AWS cli and run aws configure. You need to provide the region as well since it is required by the script.

  • Now run cp .env_sample .env and set the TB_HOST, TB_TOKEN, and``S3_BUCKET`` (in our case S3_BUCKET=automatic-ingestion-poc)

  • Run the ./ script. It’ll deploy a Lambda function with name TB_FUNCTION_NAME to your AWS account, which will listen for new files in the S3_BUCKET and automatically append them to a Tinybird Data Source described by the FILE_REGEXP environment variable.

It creates a Lambda function in your AWS account:

Lambda function to sync an S3 bucket to Tinybird

Lambda function to sync an S3 bucket to Tinybird

Each time you drop a new CSV file it is automatically ingested in the destination Data Source. In this case, the Lambda function is automatically configured to support this naming convention in the CSV files {DESTINATION_DATA_SOURCE}_**.csv. For instance, if you drop a file named events_0.csv it’ll be appended to the events Data Source. You can modify this behaviour with the FILE_REGEXP environment variable or by modifying the code of the Lambda function.

Drop files to an S3 bucket and check the datasources_ops_log

Drop files to an S3 bucket and check the datasources_ops_log