Project : AWS ECS - RDS Database Configuration (Part 1)

Setup RDS Database and Build Backend Application Docker Image — Deploy to AWS ECS Fargate with Load Balancer (Part 1)

Cloud Architecture of AWS ECS with ALB

Before getting started with setting up AWS ECS Fargate with Load Balancer, let’s get a backend server and create a docker image of it. As I being a Python developer, I am creating a FastAPI application with few API endpoints.

Setup Database

For database, let’s setup a postgresql db instance in AWS RDS.

1. Login to AWS console and go to Amazon RDS dashboard.

2. Click on Create Database button.

3. Create database:

Note: For demo purpose, I will be using free-tier Postgres instance with Password Authentication enabled and all the additional backup, autoscaling, encryption, monitoring, etc. disabled. Please enable the plans based on your requirements.

Section 1. Choose a database creation method: Select Standard create.

Section 2. Engine Options: Select PostgreSQL.

Section 3. Templates: Select Free tier.

Section 5. Settings:

a. DB Instance Identifier: Name your instance. [Name: ecs-fargate-db]

b. Master username: Default is postgres. If you would like to have username other than postgres, add username. It will be login ID for your instance.

c. Master password: Type the password.

d. Confirm password: Type Master password.

Note: Please be sure you store master username and password

Section 7. Storage: Increase Allocated storage if required and Disable autoscaling.

Section 8. Connectivity:

a. Virtual Private Cloud: Default VPC.

b. Subnet group: default

c. Public access: Select Yes if you want to access from outside VPC else select No. Note: Can be controlled from Security Group.

d. VPC security group: Create new and name New VPC security group.

[Name: ecs-fargate-db-SG]

Section 10. Additional configuration:

a. Add initial database name if required, else postgres database is created by default. [Initial Database Name: ecs_alb_db]

b. Disabled Backup, Encryption, Performance Insights, Monitoring, Log Exports, Maintenance, Deletion Protection.

4. Once database instance is created successfully, you can view the connection details on success message toaster and note down password and endpoint.

Note: Endpoint will be used as DB Host.

Build Docker Image of backend server

Here, I have created a FastAPI application and added DB credentials to .env file.

Note: Do not forget to add health check API to the server.

Codebase Structure of FastAPI application

Let’s jump into containerizing the backend application.

1. Add a Dockerfile

2. Build Image

Run the command on project’s root directory.

docker build -t ecs-alb-image .

The command will build an image with name `ecs-alb-image` and tag `latest`.

~ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ecs-alb-image latest 4f8e343e2a11 8 minutes ago 536MB

3. Run Image

Run the image locally to test if the server is running and functioning as desired.

~ docker run -it -p 8000:8000 --rm ecs-alb-image:latest

Thanks to FastAPI providing API documentation by default.

Here, I conclude Part 1 with setting up database in Amazon RDS, dockerizing backend application and building its image.

In order to get your docker image pushed into Amazon Elastic Container Registry (ECR), follow my next article Part 2

Comments

Popular posts from this blog

Terraform

Scrum Master Interview help - Bootcamp

Kubernetes