nmvictor
nmvictor

Reputation: 1129

Amazon EC2, Elastic Beanstalk: My images disappear

I have deployed my PHP application in amazon EC2 using elastic beanstalk instance. The filesystem structure of my app looks like this:

MyApp
 |-css
 |  |-...
 |-js
 |  |-...
 |-uploads
 |  |- image.png
 |  |- file.pdf
 |  |- ...
 |-index.php
 |-...

My app allows users to upload images. Its a simple application to web-manage some files and is currently only used by my client. So when files are uploaded I am placing them under uploads folder as shown above. Problem is my files don't last in this folder for long. After a day or two,I ssh and find the uploads folder is empty. I am not sure what happens but I suspect elastic beanstalk does create a new instance and consequently overwrites the uploads folder contents.

How do I fix this situation?

Thanks.

Upvotes: 3

Views: 1678

Answers (1)

Sébastien Stormacq
Sébastien Stormacq

Reputation: 14905

Applications deployed on Elastic Beanstalk should be stateless. I.e. They should not store state on the EC2 instances, such as a file or a in memory http session.

AWS Elastic Beanstalk uses autoscaling behind the scene to add or remove instances from your pool, depending on your application workload.

When you change AWS Elastic Beanstalk configuration, it also might replace instances with new one.

Best practice is to store your application state in a shared store, available to all your instances.

Amazon S3 is a very good candidate for this, you just need to slightly modify your application to let users browser securely upload the files to S3.

Look at the answer to this question for more details. Amazon S3 direct file upload from client browser - private key disclosure

You can also implement client side upload without using a form, by directly using Amazon Javascript's SDK in the browser.

Upvotes: 6

Related Questions