Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
31 changes: 31 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Deploy Medusa Backend to Docker Hub

on:
push:
branches:
- ci/cd-project1

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: pavi2244/medusa-project:latest
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM node:18-alpine

WORKDIR /app

# Copy package files
COPY backend/package*.json ./

# Install dependencies
RUN npm install

# Copy backend code
COPY backend/ .

# Set environment (optional)
ENV NODE_ENV=production

# Expose Medusa default port
EXPOSE 9000

# Start Medusa server
CMD ["npm", "run", "start"]
217 changes: 108 additions & 109 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,136 +1,135 @@
<p align="center">
<a href="https://www.medusajs.com">
<img alt="Medusa" src="https://user-images.githubusercontent.com/7554214/153162406-bf8fd16f-aa98-4604-b87b-e13ab4baf604.png" width="100" />
</a>
</p>
<h1 align="center">
Medusa
</h1>

<h4 align="center">
<a href="https://github.com/medusajs/admin">Medusa Admin</a> |
<a href="https://www.medusajs.com">Website</a> |
<a href="https://www.medusajs.com/blog">Blog</a> |
<a href="https://www.linkedin.com/company/medusa-commerce">LinkedIn</a> |
<a href="https://twitter.com/medusajs">Twitter</a> |
<a href="https://docs.medusajs.com">Documentation</a> |
<a href="https://medusajs.notion.site/medusajs/Medusa-Home-3485f8605d834a07949b17d1a9f7eafd">Notion</a>
</h4>

<p align="center">
Medusa is an open-source headless commerce engine that enables developers to create amazing digital commerce experiences.
</p>
<p align="center">
<a href="https://github.com/medusajs/medusa/blob/master/LICENSE">
<img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="Medusa is released under the MIT license." />
</a>
<a href="https://github.com/medusajs/medusa/blob/master/CONTRIBUTING.md">
<img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" alt="PRs welcome!" />
</a>
<a href="https://www.producthunt.com/posts/medusa"><img src="https://img.shields.io/badge/Product%20Hunt-%231%20Product%20of%20the%20Day-%23DA552E" alt="Product Hunt"></a>
<a href="https://discord.gg/xpCwq3Kfn8">
<img src="https://img.shields.io/badge/chat-on%20discord-7289DA.svg" alt="Discord Chat" />
</a>
<a href="https://twitter.com/intent/follow?screen_name=medusajs">
<img src="https://img.shields.io/twitter/follow/medusajs.svg?label=Follow%20@medusajs" alt="Follow @medusajs" />
</a>
</p>


## Please note
This repo is managed by the Medusa Community. Medusa does not provide official support for Docker, but we will accept fixes and documentation. Use at your own risk.

**This project is inteded for development only at this time.**

The files for both the <i>Medusa server</i> and the <i>Storefront</i> are loaded in Bind Mounts allowing you to change the server functionality and have the change be hot-reloaded onto your running containers.

</p>
# 🚀 Deploying Medusa Backend on AWS ECS Fargate using Terraform and GitHub Actions

## 📚 Project Overview

This project demonstrates how to deploy the **Medusa** open-source headless commerce backend on **AWS ECS Fargate** using **Terraform** for infrastructure provisioning. A **CI/CD pipeline** using **GitHub Actions** automates the process of building and pushing the Docker image to **Docker Hub** and then deploying the image to **AWS ECS Fargate**.

---

## Requirements
## 🔥 Key Features
- **CI/CD pipeline in GitHub Actions** for building, testing, and deploying the Medusa backend.
- **Terraform** provisions the AWS infrastructure (ECS Fargate, VPC, Subnets, etc.).
- **Docker Hub** stores the Docker image of the Medusa backend.
- **AWS ECS Fargate** runs the Medusa backend container from Docker Hub.

To use Docker with Medusa, you should have created a Medusa project. Check out our [Quickstart](https://github.com/medusajs/medusa#quickstart) to get started.
---

Additionally, you should have `docker` and `docker-compose` installed on your system.
## 🛠️ Technologies Used
- **Terraform** for Infrastructure as Code (IaC).
- **GitHub Actions** for CI/CD automation.
- **Docker** for containerizing the Medusa backend.
- **Docker Hub** for storing the Docker image.
- **AWS ECS Fargate** for serverless container deployment.

## Getting Started
---

To set up Medusa in a development environment with Docker, you should copy files `docker-compose.yml`, `docker-compose.override.yml, `backend/develop.sh`, and `backend/Dockerfile` to your Medusa project.
## 🚀 How it Works

Then build the images since they are not published on dockerhub. This is accomplished by adding the `--build` flag as shown below:
### 1. **Terraform Infrastructure Provisioning**
Terraform provisions the necessary AWS resources to deploy the Medusa backend:
- **VPC** and **subnets** for networking.
- **ECS Cluster** and **Fargate Task** to run the container.
- **IAM roles** for ECS task execution.
- **ECS Service** to run and manage the Medusa backend container.

(![image](https://github.com/user-attachments/assets/cf186c03-ac42-425c-a9d4-637aa1dfab9b)
)


```bash
docker compose up --build
```
### 2. **CI Pipeline in GitHub Actions**
The **CI pipeline** in GitHub Actions automates the following:
- **Build the Docker image** using the `Dockerfile`.
- **Push the image to Docker Hub** to make it available for deployment.

Having already built the Docker images you can run docker compose without the `--build` flag.

![image](https://github.com/user-attachments/assets/994404c1-6092-4837-9792-0d5714c2f944)

```
docker compose up
```

Your local Medusa setup is now running with each of the services occupying the following ports:
### 3. **Continuous Delivery (CD) Pipeline**
The **CD pipeline** automates the process of **deploying the Docker image** stored in Docker Hub to **AWS ECS Fargate**. After the Docker image is pushed to Docker Hub via the CI pipeline, Terraform provisions the ECS service to use this image and deploy it to the cloud.

<ul>
<li><b>Medusa Server</b>: 9000
<li><b>Medusa Admin</b>: 7000
<li><b>Storefront</b>: 8000
<li><b>postgres</b>: 5432
<li><b>redis</b>: 6379
</ul>
![image](https://github.com/user-attachments/assets/07fa0eaa-25a0-44da-b670-635b76a59c46)

_Note: If you change the dependencies of your projects by adding new packages you can simply rebuild that package with the same tag `test` and run `docker compose up` once again to update your environment._

### Seeding your Medusa store

To add seed data to your medusa store run this command in a seperate
### 4. **Deploying Medusa Backend on AWS ECS Fargate**
The **Medusa backend** is deployed to **AWS ECS Fargate** using the image from **Docker Hub**. The deployment is managed by **Terraform**.

```
docker exec medusa-server medusa seed -f ./data/seed.json
```
![image](https://github.com/user-attachments/assets/37b7efa9-5e70-483a-b257-7fcfb08c95fc)

## Running Medusa with docker in production

This repository and each of the services contain dockerfiles for both development and production, named `Dockerfile` and `Dockerfile.prod` respectively. The `Dockerfile.prod` copies the local files from disk and builds a production ready image based on your local development progress. Your specific needs for a production like container might differ from the `Dockerfile.prod` but it should provide a template and an idea of the requirements for each of the basic services.

To run the services in a production state `docker compose` is simply run with the `docker-compose.production.yml` file as well as the basic `docker-compose.yml` file as seen below. If you wish to build the production ready images and then start them run `docker compose up` with the `--build` flag as described above.

```
docker compose up -f docker-compose.yml -f docker-compose.production.yml up
```
### 5. **Accessing the Medusa Backend**
The **Medusa backend** is now live and can be accessed via the public IP of the ECS task. The backend is accessible via port `9000`.

![image](https://github.com/user-attachments/assets/542da64b-82ef-4d8a-b632-5498b40dad0b)


---
## 📑 Setup and Installation

### Step 1: Set Up GitHub Secrets
In your GitHub repository (`Settings > Secrets and variables > Actions`), add the following secrets:
- **DOCKERHUB_USERNAME** → Your Docker Hub username
- **DOCKERHUB_TOKEN** → Your Docker Hub access token

These are used by the GitHub Actions workflow for pushing images

---

### Step 2: Install Terraform
Install Terraform from the [official website](https://www.terraform.io/downloads.html) if it is not already installed.

`docker-compose.production.yml` contains production relevant overrides to the services described in the `docker-compose.yml` development file.
---

### Step 3: Install AWS CLI
Install the AWS Command Line Interface (CLI) from the [official AWS documentation](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html).

## Try it out
---

### Step 4: Configure AWS CLI
Set up your AWS credentials locally by running:
```bash
aws configure
```
curl -X GET localhost:9000/store/products | python -m json.tool
Enter your AWS Access Key, Secret Access Key, region, and output format.

---

### Step 5: Clone the Repository
Clone the repository to your local machine:
```bash
git clone https://github.com/amie2001/docker-medusa-/tree/project
cd docker-medusa-
```

### Step 4: Apply Terraform Configuration
Run the following Terraform commands to create the necessary AWS resources:
```bash
terraform init
terraform apply
```

After the seed script has run you will have the following things in you database:

- a User with the email: [email protected] and password: supersecret
- a Region called Default Region with the countries GB, DE, DK, SE, FR, ES, IT
- a Shipping Option called Standard Shipping which costs 10 EUR
- a Product called Cool Test Product with 4 Product Variants that all cost 19.50 EUR

Visit [docs.medusa-commerce.com](https://docs.medusa-comerce.com) for further guides.

<p>
<a href="https://www.medusa-commerce.com">
Website
</a>
|
<a href="https://medusajs.notion.site/medusajs/Medusa-Home-3485f8605d834a07949b17d1a9f7eafd">
Notion Home
</a>
|
<a href="https://twitter.com/intent/follow?screen_name=medusajs">
Twitter
</a>
|
<a href="https://docs.medusa-commerce.com">
Docs
</a>
</p>
### Step 5: Set Up GitHub Actions Pipeline
Once you push your changes to the `project` branch, GitHub Actions will automatically:
- Build the Docker image.
- Push the image to Docker Hub.
- Make the image available for deployment to ECS Fargate.

---

## 🎥 Video Walkthrough
👉 [Video link](https://drive.google.com/file/d/1veK8mLx2Qqqb4E328lnvLuNVM8HlaCyK/view?usp=sharing)

---

## 🔗 GitHub Repository
👉 [GitHub Repo link](https://github.com/amie2001/docker-medusa-/tree/project)

---

## 🎯 Conclusion
With this setup, the Medusa backend is deployed on **AWS ECS Fargate**, utilizing **Terraform** for infrastructure provisioning and **GitHub Actions** for continuous delivery of the Docker image.

# 🌟 Thank you for visiting!
21 changes: 0 additions & 21 deletions admin/Dockerfile

This file was deleted.

19 changes: 0 additions & 19 deletions backend/Dockerfile

This file was deleted.

10 changes: 5 additions & 5 deletions backend/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:17.1.0 as builder
FROM node:20 as builder

WORKDIR /app/medusa

Expand All @@ -8,7 +8,7 @@ RUN rm -rf node_modules

RUN apt-get update

RUN apt-get install -y python
RUN apt-get install -y python3

RUN npm install -g npm@latest

Expand All @@ -17,7 +17,7 @@ RUN npm install --loglevel=error
RUN npm run build


FROM node:17.1.0
FROM node:20

WORKDIR /app/medusa

Expand All @@ -33,7 +33,7 @@ COPY medusa-config.js .

RUN apt-get update

RUN apt-get install -y python
RUN apt-get install -y python3
# RUN apk add --no-cache python3

RUN npm install -g @medusajs/medusa-cli
Expand All @@ -44,4 +44,4 @@ COPY --from=builder /app/medusa/dist ./dist

EXPOSE 9000

ENTRYPOINT ["./develop.sh", "start"]
ENTRYPOINT ["./develop.sh", "start"]
12 changes: 12 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const app = express();
const PORT = process.env.PORT || 9000;

app.get('/', (req, res) => {
res.send('Medusa Backend is Running!');
});

app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

Loading