Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 56

Core AWS Services for Web Applications

Recommended resources

Welcome to the next stage of our journey into building cloud-native applications. In the last lesson, you took a massive step by containerizing your entire Django application stack with Docker and Docker Compose. You now have a portable, reproducible development environment. However, it still lives on your local machine. Today, we prepare the ground for deploying it to the world.

This lesson is all about provisioning the core infrastructure on Amazon Web Services (AWS), the public cloud provider of choice for many startups like Infilect. We won't be deploying our code just yet; instead, we will be setting up the "real estate" in the cloud where our application will live. This directly addresses the key job requirement of having "experience with public cloud services like AWS."

By the end of this lesson, you will have provisioned and configured the three fundamental services for hosting a modern web application:

  • Amazon EC2 (Elastic Compute Cloud): A virtual server that will provide the raw computing power to run our application containers.
  • Amazon RDS (Relational Database Service): A managed PostgreSQL database, freeing us from the complexities of database administration.
  • Amazon S3 (Simple Storage Service): A highly scalable object storage service where we will store our application's media files, such as the retail shelf images central to our project.

The "Why" Behind Managed Services

Before we begin, it's important to understand why we're using separate services like RDS and S3 instead of just installing PostgreSQL and a file server on our EC2 instance. The answer is scalability and separation of concerns.

By using RDS, AWS handles backups, security patching, and scaling for our database. By using S3, we get virtually limitless, durable, and cost-effective storage for files. This architecture allows each component to scale independently and is a hallmark of modern cloud application design.

This diagram shows the high-level architecture we are building towards. Our Django application will run on an EC2 instance, which will in turn connect to a managed RDS database for data and an S3 bucket for file storage.

This diagram shows the high-level architecture we are building towards. Our Django application will run on an EC2 instance, which will in turn connect to a managed RDS database for data and an S3 bucket for file storage.

Let's begin by creating your AWS account if you haven't already, and then we'll provision each service one by one. Make sure you are signed up for the AWS Free Tier, as the services we select will be eligible.


Step 1: Provisioning Your Virtual Server with EC2

EC2 is essentially a virtual computer that you rent from AWS. This will be the host machine where we will eventually run our Docker containers.

Our goal is to launch a basic EC2 instance, create a way to securely connect to it (an SSH key pair), and configure its network firewall (a Security Group).

Deploy Django Application on AWS EC2 with PostgreSQL and S3 Storage

The video "Deploy Django Application on AWS EC2" provides a clear, step-by-step walkthrough of launching an EC2 instance through the AWS Management Console.

Please follow the segment on creating an EC2 instance. As you follow along, pay close attention to these key configuration choices: Name and Tags: Give your instance a clear name, like infilytics-web-server. Application and OS Images (AMI): Select Ubuntu, as shown in the video. We will use the latest LTS (Long Term Support) version available, which is a common choice for servers. Instance type: Choose t2.micro. This is eligible for the AWS Free Tier and is sufficient for our initial development. Key pair (login): This is critical. Follow the video to create a new key pair. Give it a memorable name (e.g., aws-infilytics-key) and download the .pem file. Store this file in a secure location on your computer; you will lose access to your server if you lose this file. Network settings (Security Group): A Security Group acts as a virtual firewall. The video shows how to allow HTTP and HTTPS traffic. For now, ensure these rules are set: Allow SSH traffic from: Set this to My IP. This is a critical security measure that ensures only your computer can attempt to log into the server. Allow HTTP traffic from the internet: Check this box. Allow HTTPS traffic from the internet: Check this box. Go ahead and launch the instance. You've just provisioned your first virtual server in the cloud!

Go ahead and launch the instance. You've just provisioned your first virtual server in the cloud!


Step 2: Setting Up a Managed Database with RDS

While we could install PostgreSQL inside a Docker container on our EC2 instance, a much more robust and scalable approach is to use Amazon RDS. RDS handles tedious database administration tasks, allowing you to focus on your application logic.

We will now create a PostgreSQL database instance and, most importantly, configure it to securely communicate with our EC2 instance.

Deploy Django Application on AWS EC2 with PostgreSQL and S3 Storage

The same video provides an excellent guide to creating a PostgreSQL database instance using RDS.

Follow the instructions in the video for launching an RDS instance. Here are the key points to focus on: Engine options: Choose PostgreSQL. Templates: Select Free tier. This will automatically limit options to what is free. DB instance identifier: Give your database a name, like infilytics-db. Master username and password: Set a username (e.g., infilytics_user) and a secure password. Record these credentials in a safe place. Connectivity: This is the most important part. In the "Public access" section, select Yes. While this sounds insecure, we will lock it down immediately using a security group. This step is necessary for now to make it possible for the EC2 instance to connect. For the VPC security group, choose "Create new" and give it a name like rds-sg. Additional configuration: Expand this section and set the Initial database name to something like infilytics_prod. If you forget this, Django will have trouble connecting. After creating the database, it will take a few minutes to become available. Once it's ready, we need to perform a crucial security step: linking the EC2 and RDS security groups. Navigate to the EC2 dashboard. Find your infilytics-web-server instance and click on its Security tab. Note the ID of its security group (e.g., sg-0123...). Navigate to the RDS dashboard. Go to your new database and click on its Connectivity & security tab. Click on the VPC security group (rds-sg). Select the Inbound rules tab and click Edit inbound rules. Delete the existing rule that allows traffic from your IP address. Click Add rule. Type: PostgreSQL. Source: Custom. In the search box, paste the ID of your EC2 instance's security group (the sg-0123... ID). Click Save rules. This change ensures that the only thing that can connect to your database is an EC2 instance within your specified security group. This is a fundamental cloud security practice. Step 3: Creating an Object Storage Bucket with S3 Our application needs to handle file uploads, specifically the images for retail analysis. Storing these files directly on the EC2 instance's hard drive is not scalable. Amazon S3 is the industry standard for this task. Here, we will create an S3 "bucket" (which is like a folder in the cloud) and create a special user (an IAM User) that has programmatic permission to add and retrieve files from it.

After creating the database, it will take a few minutes to become available. Once it's ready, we need to perform a crucial security step: linking the EC2 and RDS security groups.

  1. Navigate to the EC2 dashboard.
  2. Find your infilytics-web-server instance and click on its Security tab. Note the ID of its security group (e.g., sg-0123...).
  3. Navigate to the RDS dashboard.
  4. Go to your new database and click on its Connectivity & security tab.
  5. Click on the VPC security group (rds-sg).
  6. Select the Inbound rules tab and click Edit inbound rules.
  7. Delete the existing rule that allows traffic from your IP address.
  8. Click Add rule.
    • Type: PostgreSQL.
    • Source: Custom. In the search box, paste the ID of your EC2 instance's security group (the sg-0123... ID).
  9. Click Save rules.

This change ensures that the only thing that can connect to your database is an EC2 instance within your specified security group. This is a fundamental cloud security practice.


Step 3: Creating an Object Storage Bucket with S3

Our application needs to handle file uploads, specifically the images for retail analysis. Storing these files directly on the EC2 instance's hard drive is not scalable. Amazon S3 is the industry standard for this task.

Here, we will create an S3 "bucket" (which is like a folder in the cloud) and create a special user (an IAM User) that has programmatic permission to add and retrieve files from it.

Deploy Django Application on AWS EC2 with PostgreSQL and S3 Storage

Once again, the video will guide us through the process in the AWS Console.

First, follow the portion of the video that shows how to create an S3 bucket. Bucket name: This must be globally unique. Try something like infilytics-data-yourname-2024. AWS Region: Keep it the same as your EC2 and RDS instances. Block Public Access settings: For our use case (storing private user data), keep all public access blocked. We will access files through our application, not by making them public on the internet. Next, you need to grant your application permission to interact with this bucket. We do this by creating an IAM (Identity and Access Management) User. Follow the video's guide on creating an IAM user and generating keys. User name: infilytics-s3-user. Permissions: Attach the AmazonS3FullAccess policy directly. In a real-world scenario, you would create a more restrictive policy, but this is sufficient for now. Create access key: In the user's "Security credentials" tab, create an access key. Select Local code as the use case. Store credentials: When the access key is created, you will be shown an Access key ID and a Secret access key. This is the only time you will see the secret key. Copy both values and store them securely with your database password. Never, ever commit these keys to your Git repository.


Conclusion

Excellent work. You have now provisioned the foundational pillars of your cloud infrastructure on AWS. You haven't deployed any code yet, but you have done the essential work of an infrastructure-aware backend engineer.

Let's recap what you have accomplished:

  • You launched an EC2 virtual server, which will be the home for your application code. You secured it with a key pair and a firewall rule that only allows you to connect.
  • You created a managed PostgreSQL database with RDS, taking the burden of database administration off your shoulders. You configured its security group to only accept connections from your EC2 instance.
  • You set up an S3 bucket for scalable file storage and created an IAM user with the credentials your Django application will need to store and retrieve files.

In our next lesson, we will bring everything together. We will connect to our EC2 instance, install Docker, and deploy our containerized Django application. Finally, we'll configure it to use the RDS and S3 resources you just created, bringing your application to life in the cloud.