How to get started with Amazon Web Services: Part 3 : The Last One

Riddhiman Adib
My Online Cafe
Published in
8 min readSep 25, 2016

In the last two parts, we started EC2 instance and set up LAMP server. This is the last part of the series where we’ll be working with Amazon RDS as well as Amazon S3. Click here for Part 1 & Part 2.

Let’s start this part by explaining how this all works. Suppose, you already have set up a server (following the last two tutorials, or by any other way). Your awesome app is just a hit and your business is just booming. There’s tons of daily users and there’s tons of selfies being posted in the app everyday. Your database is just enough as it is, but you might need some extra storage, just a few extra TeraBytes. Or consider another case, you might need to shift your database for security purposes. Or you might need to change the OS of the server. In any case, your server, database and storage is all tied up to that single piece of EC2 instance, and you can’t work on any of it as it might hamper the other part.

That’s where Amazon Web Services comes in handy. In case of our product, we have the server set up at an EC2 instance, storage in Amazon RDS & all static file being stored in Amazon S3. We can just work on any one part, without really hampering any other part, awesome, right?

So, in simpler terms, our website (or code, or API, or script) runs on our EC2 instance (the server). If any incoming HTTP request sends any data or needs querying in the database, the API does so on the database on Amazon RDS (the database handler). And if it sends any image or requests any image, it operates on Amazon S3 (the storage) using the respective SDK. That’s how all of this is tied up, three different modules working separately yet synchronously.

Amazon RDS

Let’s take it from their own documentation.

Amazon Relational Database Service (Amazon RDS) is a web service that makes it easier to set up, operate, and scale a relational database in the cloud. It provides cost-efficient, resizeable capacity for an industry-standard relational database and manages common database administration tasks.

In simple terms, Amazon has a Relational Database Service, independent from any server or host. You can host your MySQL or any other relational database here. So, let’s get started with Amazon RDS.

Start RDS instance

  1. Open RDS console page here.
  2. Pick your desired data-centre.

3. Open Subnet Group from left side. Click on Create Subnet Group.

4. Add a name and description (I picked phpMyAdmin as both the name and description, don’t worry too much, it won’t matter). Select a VPC created earlier, or create a new one. Then click on Add all the subnets. It’ll add afew items at the list. Now click Create.

5. We are now back to creating the RDS instance. Click on Instances on the left panel and then on Launch DB Instance.

6. Select MySQL (or any other option, if you want). Click Next.

7. Just in this case, select MySQL Dev/Test environment. For this tutorial, we’ll not be going into MultiAZ Setup. Now go to Next Step.

8. Select DB instance Class as db.t2.micro (because this is what’s offered in AWS Free Tier). Select MutiAZ Deployment as No, Storage type as General Purpose (SSD) and storage (for free tier) as 5GB. For simpler settings, we’ve used the same word phpMyAdmin for all the fields in Identifier, Username and password. Click on Next Step.

9. Set VPC as your earlier created one. Make the publicly accessible option as Yes (EDIT: changed after being notified by Tishad Touhidul). Pick Wide Open (VPC) for VPC Security Group(s). Enter a default Database name, I’ve picked sampledb. Set the backup retention period as 0 days, as we’ll not be needing that right now. Now click on Launch DB Instance.

10. Voila! You’ve setup your RDS instance.

Now, you can connect to your database using any MySQL client like MySQL Workbench, HeidiSQL etc. Just enter RDS instance’s endpoint found at the dashboard (see image below), port 3306, username and password of MySQL, entered earlier, at the client to connect to database and view the db.

Amazon S3

IMAGE

Amazon S3 (Simple Storage Service) is simply a storage for the internet. You can host all the images, videos, static files in Amazon S3, and use the URL anywhere to access those easily. You can even make the URL private or public, for public access. Take it from their doc:

Amazon Simple Storage Service is storage for the Internet. It is designed to make web-scale computing easier for developers.
Amazon S3 has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, fast, inexpensive data storage infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.

And the best thing is, there’s no tutorial needed. You just have to create your first bucket to get started with S3's awesomeness!

Bucket

Now, what’s a bucket?

A bucket is a key-word in Amazon S3 for the top-level container for the files to be stored. Think of it as the Drives ( like c:, or d: )in your PC, where all the other files and folders reside. From their doc:

Amazon S3 provides APIs for you to create and manage buckets. By default, you can create up to 100 buckets in each of your AWS accounts. If you need additional buckets, you can increase your bucket limit by submitting a service limit increase.

You need to create at least one bucket to get started with Amazon S3. Just create one, make folders & sub-folders and store whatever (static file) you want.

Upload

To upload any file to Amazon S3, we just need to use the respective SDK and their functions. We need to provide it user credentials to get upload permissions, and details for the file to be uploaded. That’s it, anyone with the correct credentials canb upload files to S3.

As for example, we have been using the PHP SDK for S3 to upload images from our server to S3 server. The code goes like this:

require ‘aws/aws-autoloader.php’;// Instantiate an Amazon S3 client.
$s3 = new Aws\S3\S3Client([
‘version’ => ‘latest’,
‘region’ => ‘ap-southeast-1’,
‘credentials’ => [
‘key’ => ‘__YOUR API KEY__’,
‘secret’ => ‘__YOUR APP SECRET__’,
],
]);
// try upload
try {
$result = $s3->putObject([
‘Bucket’ => ‘harriken-image-bucket’,
‘Key’ => '__FOLDER_STRUCTURE__',
‘Body’ => fopen($_FILES[‘profilepicture’][‘tmp_name’], ‘r’),
‘ACL’ => ‘public-read’
]);
} catch (Aws\Exception\S3Exception $e) {
echo “There was an error uploading the file.”;
}

// now, handle the result
if($result[‘ObjectURL’]) {
// MEANS UPLOAD IS SUCCESSFUL
// DO SOMETHING
}

Or you can use Mobile SDKs. As for example, in Android, you need to get the right access using Identity and Access Management Tools, declare the service in AndroidManifest.xml like this:

<service
android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
android:enabled="true" />

and finally use something like this:

AmazonS3 s3 = new AmazonS3Client(credentialsProvider);
TransferUtility transferUtility = new TransferUtility(s3, getApplicationContext());
TransferObserver observer = transferUtility.upload(
MY_BUCKET, /* The bucket to upload to */
OBJECT_KEY, /* The key for the uploaded object */
MY_FILE /* The file where the data to upload exists */
);

Of course, it varies slightly if you’re using Composer in PHP or if your setup is different, but you can get an idea about how it works. For further clarifications, visit this link to get a clear idea.

A point to remember though, have you seen the line of code ‘ACL’ => ‘public-read’ in PHP? It means, the file being uploaded is public to view and download. If you don’t make it public, the files uploaded are by default private and can’t be seen without token. So, if you’re planning to make a app with images open for all, make sure it’s all public!

There’s different SDKs out there to work seamlessly with Amazon S3. There’s SDKs in Java, .NET, Python, PHP, Node.js, Ruby, along with different Mobile SDKs for Android, iOS, ReactNative, Unity, Xamarin, Javascript etc. Try the one that suits your need, or the one you’re most comfortable with.

Download

Just visit the public URL of the file or make a http GET request on that URL. It’s all ready for download!

This ends the last part of the tutorial on the AWS series. Hope you liked it as much as I did!

Feel free to comment, like, recommend, share! And follow My Online Cafe to get more updates on Tech Stuff!

--

--

Riddhiman Adib
My Online Cafe

Postdoctoral Researcher. Tech Enthusiast. Occasional Blogger. Self-proclaimed Musician. more at: adib2149.github.io