-
Install NestJS CLI:
npm install -g @nestjs/cli
-
Create a new NestJS project:
nest new nestjs-todo-app
-
Navigate into the project directory:
cd nestjs-todo-app
-
Install dependencies:
npm install
-
Set up PostgreSQL:
- Make sure PostgreSQL is running.
- Create a new PostgreSQL database for the application.
-
Configure environment variables:
- Rename
.env.example
file to.env
and fill in the necessary environment variables such asDB_HOST
,DB_PORT
,DB_USERNAME
,DB_PASSWORD
, andDB_DATABASE
.
- Rename
-
Create a new Todo module using Nest CLI:
nest g module todos
-
Create a Todo entity class under the new module:
nest g class todos/entities/todo
-
Create a Todo service under the new module:
nest g service todos/services/todo
-
Create a Todo controller under the new module:
nest g controller todos/controllers/todo
-
Update
src/app.module.ts
to import and use the new Todo module:import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Todo } from './entities/todo.entity'; import { TodosModule } from './todos/todos.module'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [ ConfigModule.forRoot(), TypeOrmModule.forRoot({ type: 'postgres', host: process.env.DB_HOST, port: parseInt(process.env.DB_PORT, 10), username: process.env.DB_USERNAME, password: process.env.DB_PASSWORD, database: process.env.DB_DATABASE, ssl: { rejectUnauthorized: false, }, entities: [ Todo ], synchronize: true, migrations: [], subscribers: [], }), TodosModule ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
-
Change the application port to 8080 (Default port for elastic Beanstalck):
- Open the
src/main.ts
file. - Update the
await app.listen()
line to use port 8080:
await app.listen(process.env.PORT);
- Open the
-
Start the application:
npm run start:dev
-
Open your browser and navigate to
http://localhost:3000
to see the app in action.
- Go to the AWS Management Console and navigate to IAM.
- Create a new IAM role with necessary permissions for Elastic Beanstalk to access other AWS services like RDS and EC2.
- Attach policies like
AWSElasticBeanstalkFullAccess
,AmazonRDSFullAccess
, andAmazonEC2FullAccess
to the IAM role.
- Navigate to the RDS service in the AWS Management Console.
- Create a new PostgreSQL database instance.
- Configure the database instance with desired specifications (instance type, storage, etc.).
- Make sure to note down the endpoint, username, password, and database name for later use.
- Grant acces do the database from my local machine (make it public)
-
Navigate to Elastic Beanstalk:
- Go to the AWS Management Console.
- In the "Find Services" search bar, type "Elastic Beanstalk" and select it from the options.
-
Create a New Application:
- Click on "Create Application".
- Enter a name for your application.
- Optionally, add a description.
- Click on "Create".
-
Create a New Environment:
- Once your application is created, click on "Create a new environment".
- Choose the web server environment type that matches your application (e.g., Node.js, Python, PHP, etc.).
- Select the appropriate platform version.
- Choose a preconfigured sample application or upload your application code.
- Click on "Configure more options" to customize your environment settings if needed.
-
Configure Environment Settings:
- Configure the environment settings such as instance type, key pair (for SSH access), environment variables, and database configuration (if using RDS).
- Configure db environment also exposed port
- Optionally, enable logging, monitoring, and other advanced settings as per your requirements.
-
Create Environment:
- Review the configuration details.
- Click on "Create environment" to launch your Elastic Beanstalk environment.
-
Wait for Deployment:
- Elastic Beanstalk will start creating your environment and deploying your application.
- This process may take several minutes to complete. You can monitor the progress in the Elastic Beanstalk console.
-
Accessing Your Application:
- Once the deployment is complete, Elastic Beanstalk will provide you with a URL where you can access your application.
- Click on the URL to view your deployed application in a web browser.
-
Manage and Monitor:
- You can manage and monitor your Elastic Beanstalk environment from the AWS Management Console.
- View environment health, logs, and metrics to monitor the performance of your application.