02 Apr Building a serverless website with S3
After working with Amazon Web Services for some time now I’m still amazed at the flexibility each solution holds. Out of all the services AWS houses the one I use the most is S3. S3 currently holds over 40 TB’s of data for me in a multi-tiered secure environment with multiple redundancy features in place. So what does that mean exactly? Essentially, my data is fairly secure and duplicated so I can make sure that my files never get lost even if an Amazon data center were completely lost.
What is S3: Simple Storage Solution
Amazon S3 is a cloud based storage solution that gives its users the ability to have a highly-scalable, reliable, and low-latency data storage infrastructure at very low costs.
S3 Overview
Simple Storage Service: A safe place to store your files
- Object based storage: Users can store flat files like word doc’s, videos, photos, pdf’s, etc.
- Files can be from 0 bytes to 5TB in size: AWS provides unlimited storage
- Files stored in Buckets that must be unique globally: Buckets are just Folders
- Read after Write Consistency for PUTS of new objects and Eventual Consistency for overwrite PUTS and DELETES
- Amazon offers a 99.99 Availability and 99.99999999% Durability Guarantee
- S3 is designed to sustain a 2 facility loss without the loss of your data
How Can I Use S3
S3 provides a simple web service interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web. Using this web service, developers can easily build applications that make use of Internet storage. S3 is highly scalable which allows developers to start small and grow their applications with time without the worry of running out of available resources.
Amazon S3 is designed to be highly flexible. Users can store any type or amount of data they want.
What S3 is not
S3 is not a server or place where you can install an operating system or database. Those types of files require block storage which you can think of as a type of storage that is similar to your computer’s hard drive while S3 provides you with object based storage which is just a series of folders to house data.
Running a serverless website
So I just said that S3 can’t house operating systems or have functional platforms installed onto it so how can I run a website from it?
Bringing the Static
As long the website you’d like to deploy is static there really isn’t a need for a server. What do I mean by static? A static site is just any website that doesn’t have the need to write to a backend and the pages you show have fixed content. Each page is coded in HTML and displays the same information to every visitor. Static sites are the most basic type of websites and are the easiest to create and unlike dynamic websites, they do not require any backend programming or database deployment. For my example I’ll be uploading the frontend of an application my flatiron partner and I built as our Mod3 final project.(Thanks to Melissa Sames for being a great Mod3 project partner. You can find her GitHub here.) I’ll be uploading all the front end files including the css but will not be including any of the rails backend database functionality we built into the application.
Nicolas Cage Minesweeper Game S3 Demo
Steps To Build
- Go to S3 inside of AWS Services
- Create a new bucket
- Name your bucket: Must be a globally unique name
- Set permissions to grant public read access
- Go to bucket’s properties and select static web hosting
- Enter the name of your index document. Mine was index.html
- Enter the name of your error / 404 page. I just entered the same index.html
- Upload all your HTML, CSS, and JS files to the bucket. It doesn’t matter if files are nested within folders everything will be read
- On the upload of your files, set permissions to grant public read access
- For the demo I left all other settings at default
- Get your endpoint(your url) inside of the static web hosting properties page
- That’s it your static application is live.
But Wait There’s More…
Did you say you needed a backend like rails or need a bit of processing power to run. Not a problem. AWS Lambda to the rescue.
Lambda
AWS Lambda is an event-driven, serverless computing platform. Lambda is a compute service that runs code in response to events and automatically manages the compute resources required by that code. That means that you can run code without provisioning or managing servers. You pay only for the compute time you consume — there is no charge when your code is not running. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability.
Going A Bit Deeper
Amazon’s API Gateway
Now that you have lambda doing processing for you how about we make an API call. That’s where AWS’s API Gateway comes in. It allows you to process hundreds of thousands of concurrent API calls and handles traffic management, authorization, access control, monitoring, and API version management.
DynamoDB
Do you need to persist data? Well they’ve got you covered on that front as well. DynamoDB is a fast and flexible NoSQL database service for all applications that need consistent, millisecond latency at any scale. It is a fully managed cloud database and supports both document and key-value store models. That means you can you can create database tables that can store and retrieve any amount of data, and serve any level of request traffic.
Weather App Reference Architecture
Want to see this implementation running? Feel free to go to my githubaccount here to see the entire code setup for the above example.
Conclusion
S3 can be an insanely easy way to deploy a static website that doesn’t need to persist data. The best part about the entire setup is that it’s one of the cheapest ways to host an application on AWS because you’re using only one AWS resource instead of having to setup computing instances, block storage, security groups, network access controllers and all the other steps to setting up a normal virtual private cloud.
You get a hosted site with no server management needed, completely flexible scaling, built-in availability and fault tolerance. Did your head just explode? Probably, because serverless deployment really is that amazing. You don’t need to architect for any of the above capabilities since the services running the application provide them by default.
AWS even has an Application Repository where you’re able to find and deploy prebuilt serverless applications and application components for a variety of use cases, including web and mobile back-ends, chatbots, IoT, Alexa Skills, data processing, stream processing, and more. You can also find integrations with popular third-party services like Slack, Twilio, Sumo Logic, Box, and more.
You can read more about AWS Lambda and serverless computing on the AWS website here.
Also published on Medium.