Skip to content

Latest commit

 

History

History
513 lines (248 loc) · 14.7 KB

File metadata and controls

513 lines (248 loc) · 14.7 KB

github-header-image (1)

 

 

 

 

Prerequisites

1. Sign up for a free account and subcription with Serpapi to get an API key.

  • Go to Serpapi website and register, confirm registartion.

image

image

  • Confirm registration in email and verify phone number.

image

  • Click "Verify phone number" and enter your phone number.

image

  • Enter verification number sent to your phone. You will be notified a successfull confirmation.

image

image

2. Must have an AWS Account & have basic understanding of ECS, API Gateway, Docker & Python.

  • Verfiy if you have AWS CLI installed using the "version" command

image

  • If AWS CLI is not installed, you will not see any output. Use the "Pip install" command to install AWS CLI.
pip install aws cli
  • Configure AWS

    • The information needed can be found in your AWS account, you may need to create a Secret Access Key if you don't already have one. Input "json" for your format.
aws configure

image

3. Install Serpapi library in local environment.

pip install google-search-results

image

4. Docker CLI and Desktop Installed: To build & push container images.

  • Verify Docker is installed. If docker is not installed, no output will be returned.
docker --version
``

  - If not output was returnted, Docker is not installed. Enter these commands below to install.

  - First, update system to ensure everything is up to date. 

```bash
sudo apt-get update

image

  • Install Docker.
sudo apt install docker.io

image

  • Enable Docker.
sudo systemctl enable docker

image

  • Verify Docker is enabled.
systemctl status docker

image

  • Start Docker, enter password.
systemctl start docker

image

  • Run Docker to verify everthing is working correctly. A message will display it's working coreclty if it's functioning correctly.

    sudo docker run hello-world

image

 

 


 

 

Steps:

 

 

1. Clone this Repository

git clone https://github.com/MJaloui/Containerized-Sports-API.git
cd containerized-sports-api

2. Create ECR Repository

aws ecr create-repository --repository-name sports-api --region us-east-1

image

- Verify the repository was created in "Amazon ECR > Private registry > Repositories".

image

image

3. Authenticate Build, and Push the Docker Image

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com

image

sudo docker build --platform linux/amd64 -t sports-api .

image

docker tag sports-api:latest <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/sports-api:sports-api-latest

image

docker push <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/sports-api:sports-api-latest

image

  • Verify the image was created, go to Elastic Container Registry > Images > Sports-API.

image

image

4. Set Up ECS Cluster with Fargate

  1. Create an ECS Cluster:
  • Go to the ECS Console → Clusters → Create Cluster

image

image

image

  • Name your Cluster (sports-api-cluster)

image

  • For Infrastructure, select Fargate, then create Cluster. It it was succesfull, a green successfull banner will be displayed.

image

image

image

2. Create a Task Definition:

  • Go to Task Definitions → Create New Task Definition.

image

image

  • Name your task definition (sports-api-task)

image

  • For Infrastructure, select "Fargate", leave everything else at default and click "Create".

image

image

  • Add the container:

    • Name your container (sports-api-container)

image

  • Image URI: <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/sports-api:sports-api-latest

image

image

  • Container Port: 8080
  • Protocol: TCP
  • Port Name: Leave Blank
  • App Protocol: HTTP

image

  • Add Environment Variables:

image

  • Key name: SPORTS_API_KEY
  • Value: <YOUR_SPORTSDATA.IO_API_KEY>
  • Create task definition
  • Verify creation is successful, look for green banner display.

image

image

image

image

  • If you scroll down to "Container name" you can see the image.

image

3. Run the Service with an ALB

  • Go to Clusters → Select Cluster → Service → Create.

image

image

image

  • Capacity provider: Fargate

image

  • Select Deployment configuration family (sports-api-task)

image

  • Name your service (sports-api-service)

image

  • Desired tasks: 2

image

image

  • Networking: Create new security group

image

image

  • Networking Configuration:
    • Type: All TCP
    • Source: Anywhere

image

  • Load Balancing: Select "Use load Balancing" and "Application load Balancer (ALB)"

image

  • ALB Configuration:

  • Create a new ALB:

  • Name: sports-api-alb

image

  • Target Group health check path: "/sports"
  • Create service

image

image

  • Verify successful creation, a green banner will be displayed. This may take a few minutes to appear.

    image

image

4. Test the ALB:

  • After deploying the ECS service, note the DNS name of the ALB (e.g., sports-api-alb-<AWS_ACCOUNT_ID>.us-east-1.elb.amazonaws.com)

image

image

image

  • Confirm the API is accessible by visiting the ALB DNS name in your browser and adding /sports at end (e.g, http://sports-api-alb-<AWS_ACCOUNT_ID>.us-east-1.elb.amazonaws.com/sports). Select "Pretty print" to display readable text

image

image

image

image

Configure API Gateway

  1. Create a New REST API:
  • Go to API Gateway Console and scroll down → Build API

image

image

  • Name the API (e.g., Sports API Gateway), then click create.

image

image

  • Verify successful creation, a green banner will be displayed.

image

  1. Set Up Integration (create a resource):
  • Click "Create resource"

    image

  • Create the resource "/sports", (Resource path = /) (Resource name = sports).

  • Click "Create resource".

  • Verify successful creation, a green banner will be displayed along with the resource "/sports".

image

image

  • Create a GET method

image

  • Choose HTTP Proxy as the integration type, Method type = Get, Integration type = HTTP, HTTP proxy integration should be on.

image

  • Select "GET" for HTTP method.

  • Enter the DNS name of the ALB that includes "/sports" (e.g. http://sports-api-<AWS_ACCOUNT_ID>.us-east-1.elb.amazonaws.com/sports).

  • Create Method.

  • Verify successful banner

image

image

image

  1. Deploy the API:
  • Deploy the API, Select "New stage" for Stage and enter "Dev" for stage name.
  • Verify Successful creation, a green banner will be displayed.

image

image

image

image

  • Copy the endpoint URL.

image

Test the System

  • Use curl or a browser to test(I tested on the browser):

  • Enter the command below if you'd like to test it in the command line instead.

curl https://<api-gateway-id>.execute-api.us-east-1.amazonaws.com/prod/sports

image

  • Select "Petty-print" to view data in a readable text.

image

image