Can’t seem to find a way to connect. Tried running a few different Lambda functions but I just can’t seem to make the connection happen. Basically I want to have it set up so that when a file is uploaded to S3 it automatically delivers it to the dynamo database for my web app to pull from. Thanks in advanced!
You can configure your S3 to emit events each time an object gets uploaded. From there you will need to subscribe a lambda function, that would read the object, deserialise it and put it as an item into the table.
… or you if you need to import a whole file (csv for example) you can use this import feature of DynamoDb
That makes sense! I don’t know if importing them directly would work as there will be regular updates to the S3 table throughout the day and every day. So I would need it automatically to upload to the database or somewhere that my web app can read all of them.
Just use S3, why store it in dynamodb?
Yeah if that would work better! I’m still pretty new to this stuff, so sorry if I’m not understanding this correctly. Would that mean Id just be able to write a Lambda function that could show all of the items in the S3 bucket in order of recency and pass them along to the API Gateway?
What you should do is, setup a lambda function that’ll trigger when a file is uploaded to s3.
This lambda function should get the file key and maybe also generate a presignedUrL, alongside other important metadata for the uploaded file and save into a dynamodb table.
Then your web app can read the dynamodb table, get all created items and display them either based on their pre signed url or their s3 key.
You can checkout https://educloud.academy/ for workshops around these use cases
You definitely can, though S3 List calls are not free so this may end up costing you if your web app is popular. API gateway calls Lambda which calls S3 list and then either downloads the objects and passes their contents or just passes the list of files to API Gateway
———
To match your existing architecture you need a
- A dynamo table
- An S3 bucket
- A lambda function
- Configure your S3 bucket to emit Put Events to the Lambda function
- Configure your lambda function to crack open the supplied event, read the bucket and object key from the event. Download that object, read its contents. Loop over those contents and push them to DynamoDB.
As long as that last step doesn’t take more than 15 minutes, you’re fine.
One thing you need to figure out if if you have any concerns about multiple files each updating the same dynamo key at the exactly same time. Do you have any ordering concerns with these updates? If so you may want to limit the lambda to 1 concurrent execution
Without knowing more about the type of file that’s being uploaded, it’s difficult to make a meaningful recommendation, but in general, S3 is a great place to store objects and DynamoDB is a great place to store information. For example, if you are uploading images (a common need), then consider leaving the image in S3 and store only a pointer to the image (the object key) in DynamoDB. You can of course store any other related data your application needs (a description, the owner’s identity, etc.) When it comes time to serve the image, you can use CloudFront with S3 as its origin.
As others have stated, it’s easy enough to get a Lambda function to trigger when a file is stored in S3, and that function can do the database writes. If you need help with that, let us know. Likewise, let us know if you need help with using CloudFront to serve content in S3.
Other things to consider:
You could store the uploaded file in DynamoDB and that may suit your needs. However that would mean the file is stored in two places which you might want to avoid. I would recommend against storing your files in DynamoDB unless you have a compelling reason for doing so.
Also, think about how you’re going to serve the files to your web app if they are stored in DynamoDB. If that is your intent, then your options are very limited and none of them are very good options. CloudFront and API Gateway are two common ways to serve content to a web app. To my knowledge, you cannot serve content from DynamoDB through CloudFront or API Gateway. You could use a Lambda function, but large files going through Lambda is not ideal. That leaves one option: give your web app access to the database API and I strongly advise against doing that. In fact, please don’t do that. I can elaborate if the reasons are not self evident.
before writing code, look at what can be done natively. pretty sure S3 can put an event in sqs, then use eventbridge pipes to fire off a step function that can write to dynamo