FARM Stack on Amazon Linux 2 (X86-64) with support by Hfami

AWS-Marketplace

https://aws.amazon.com/marketplace/pp/prodview-httboy3ha5y24

Usage Instructions

*Note
Please replace the instance ID with the actual instance ID of your running EC2 instance, and the Public IP your actual public DNS address.

1.Enter the my-react-app directory

  • cd my-reat-app

2.Start the React application

  • npm start

The following information is displayed:

Image_1

2.Open a browser

Visit http://{public-ip}:3000 to view the application.
The following React logo is displayed:

Image_1

3.Create a simple FastAPI application

Create a directory to store your FastAPI project and create a main.py file in that directory:


from fastapi import FastAPI app = FastAPI() @app.get("/") async def read_root(): return {"message": "Hello, World!"} @app.get("/items/{item_id}") async def read_item(item_id: int, q: str = None): return {"item_id": item_id, "q": q}


4.Run the FastAPI application

Use Uvicorn to run your FastAPI application.
Open a terminal, navigate to your project directory, and run the following command:
uvicorn main:app --reload --host {public-ip} --port 8000
You will see output similar to this:
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [2872] using statreload
INFO: Started server process [2874]
INFO: Waiting for application startup.
INFO: Application startup complete.

Open a browser and visit http://{public-ip}:8000, you should see: { "message": "Hello, World!" } Visit http://{public-ip}:8000/items/1?q=somequery, you should see: { "item_id": 1, "q": "somequery" }

5.Deploy the FastAPI application

Using Uvicorn and Gunicorn
To run the FastAPI application in a production environment:
Create a Gunicorn configuration file, such as gunicorn_config.py:


import multiprocessing workers = multiprocessing.cpu_count() * 2 + 1 bind = "0.0.0.0:8000" worker_class = "uvicorn.workers.UvicornWorker"


Run the following command to start the server: gunicorn -c gunicorn_config.py main:app Open a browser and visit http://{public-ip}:8000, you should see: { "message": "Hello, World!" } Visit http://{public-ip}:8000/items/1?q=somequery, you should see: { "item_id": 1, "q": "somequery" } Note: EC2 has already been created and is running. After deploying the simple application and starting the server with gunicorn -c gunicorn_config.py main:app, open a browser and enter the IP address.

6.MongoDB and Firewall commands

- To start mongod: `sudo systemctl start mongod`
- To enable mongod: `sudo systemctl enable mongod`
- To check mongod status: `sudo systemctl status mongod`
- To stop mongod: `sudo systemctl stop mongod`
- Connect to MongoDB: `mongosh`
- Start the firewall: `sudo systemctl start firewalld`
- Stop the firewall: `sudo systemctl stop firewalld`
- Check the status of the firewall: `sudo systemctl status firewalld`
- Reload firewall rules: `sudo firewall-cmd --reload`
- List all open ports: `sudo firewall-cmd --list-ports`
- Add Prot: `sudo firewall-cmd --zone=public --add-port=8000/tcp --permanent`
Open a specific port (e.g., open TCP port 8000)