Skip to content

Commit

Permalink
Make bot dependent from service on deployment (microservices)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChampBVT committed May 1, 2022
1 parent eac56b0 commit 7719a32
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"logDriver": "awslogs",
"secretOptions": null,
"options": {
"awslogs-group": "/ecs/getprice-dex-bkc-task-definition",
"awslogs-group": "/ecs/getprice-dex-bkc-fast-api-task-definition",
"awslogs-region": "ap-southeast-1",
"awslogs-stream-prefix": "ecs"
}
Expand All @@ -29,26 +29,17 @@
{
"name": "REDIS_URL",
"value": "redis://:@127.0.0.1:6379"
},
{
"name": "SERVICE_URL",
"value": "http://localhost:8000"
}
],
"resourceRequirements": null,
"ulimits": null,
"dnsServers": null,
"mountPoints": [],
"workingDirectory": null,
"secrets": [
{
"valueFrom": "arn:aws:secretsmanager:ap-southeast-1:355698733641:secret:TELEGRAM_TOKEN-3cCpCK",
"name": "TELEGRAM_TOKEN"
}
],
"secrets": null,
"dockerSecurityOptions": null,
"memory": null,
"memoryReservation": 256,
"memoryReservation": null,
"volumesFrom": [],
"stopTimeout": null,
"image": "",
Expand All @@ -68,15 +59,15 @@
"dockerLabels": null,
"systemControls": null,
"privileged": null,
"name": "getprice-dex-bkc-container"
"name": ""
}
],
"placementConstraints": [],
"memory": "256",
"memory": "128",
"taskRoleArn": "arn:aws:iam::355698733641:role/ecsTaskExecutionRole",
"compatibilities": ["EXTERNAL", "EC2"],
"taskDefinitionArn": "",
"family": "getprice-dex-bkc-task-definition",
"family": "getprice-dex-bkc-fast-api-task-definition",
"requiresAttributes": [
{
"targetId": null,
Expand All @@ -96,18 +87,6 @@
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "ecs.capability.secrets.asm.environment-variables"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
},
{
"targetId": null,
"targetType": null,
Expand All @@ -120,7 +99,7 @@
"networkMode": null,
"runtimePlatform": null,
"cpu": "256",
"revision": 3,
"revision": 4,
"status": "ACTIVE",
"inferenceAccelerators": null,
"proxyConfiguration": null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: Deploy to ECS (EC2)
name: Deploy Telegram bot service to ECS (EC2)

on:
push:
paths:
- telegram_bot/*
branches:
- multiple-services-in-container
- develop
Expand Down Expand Up @@ -54,24 +56,24 @@ jobs:
with:
context: .
push: true
tags: ${{ secrets.AWS_ECR_REGISTRY }}/${{ secrets.AWS_ECR_REPOSITORY }}:${{ github.sha }}
tags: ${{ secrets.AWS_ECR_REGISTRY }}/${{ secrets.AWS_ECR_REPOSITORY_TELEGRAM_BOT }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: .github/task-definition.json
task-definition: .github/task-definition-telegram-bot.json
container-name: ${{ secrets.AWS_ECS_CONTAINER }}
image: ${{ secrets.AWS_ECR_REGISTRY }}/${{ secrets.AWS_ECR_REPOSITORY }}:${{ github.sha }}
image: ${{ secrets.AWS_ECR_REGISTRY }}/${{ secrets.AWS_ECR_REPOSITORY_TELEGRAM_BOT }}:${{ github.sha }}
environment-variables: |
APP_REVISION=${{ github.sha }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ secrets.AWS_ECS_SERVICE }}
service: ${{ secrets.AWS_ECS_SERVICE_TELEGRAM_BOT }}
cluster: ${{ secrets.AWS_ECS_CLUSTER }}
wait-for-service-stability: true
79 changes: 79 additions & 0 deletions .github/workflows/develop-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Deploy FastAPI service to ECS (EC2)

on:
push:
paths:
- service/*
branches:
- multiple-services-in-container
- develop

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: true

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-southeast-1

# For private ECR Repository
# - name: Login to Amazon ECR
# id: login-ecr
# uses: aws-actions/amazon-ecr-login@v1

# For private ECR Repository
# - name: Login to ECR via docker/login-action
# uses: docker/[email protected]
# with:
# registry: ${{ steps.login-ecr.outputs.registry }}

- name: Login to Public ECR
uses: docker/[email protected]
with:
registry: public.ecr.aws
username: ${{ secrets.AWS_ACCESS_KEY_ID }}
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
env:
AWS_REGION: us-east-1

- name: Docker Setup Buildx
uses: docker/[email protected]

- name: Build and push Docker images
uses: docker/[email protected]
with:
context: .
push: true
tags: ${{ secrets.AWS_ECR_REGISTRY }}/${{ secrets.AWS_AWS_ECR_REPOSITORY_FAST_API_SERVICE }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: .github/task-definition-fast-api.json
container-name: ${{ secrets.AWS_ECS_CONTAINER_FAST_API }}
image: ${{ secrets.AWS_ECR_REGISTRY }}/${{ secrets.AWS_ECR_REPOSITORY_FAST_API_SERVICE }}:${{ github.sha }}
environment-variables: |
APP_REVISION=${{ github.sha }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ secrets.AWS_ECS_SERVICE_FAST_API }}
cluster: ${{ secrets.AWS_ECS_CLUSTER }}
wait-for-service-stability: true
1 change: 0 additions & 1 deletion Dockerfile → Dockerfile.fast_api
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ COPY --from=builder /dependencies/requirements.txt .

RUN pip install -r requirements.txt && pip cache purge

COPY /telegram_bot/ ./telegram_bot
COPY /service ./service

COPY start.sh .
Expand Down
39 changes: 39 additions & 0 deletions Dockerfile.telegram_bot
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# use python container image
FROM python:3.9.5-alpine as builder

# set the working directory of the image filesystem
WORKDIR /dependencies

RUN pip install pipenv==2021.5.29

COPY Pipfile Pipfile.lock ./

# Generate requirements file for pip to install dependencies
RUN pipenv lock --keep-outdated --requirements > requirements.txt

####################################################################

FROM python:3.9.5-slim as production

# Fix .local/bin not in PATH warning on pip
ENV PATH "$PATH:/home/python/.local/bin"

WORKDIR /usr/src/app

RUN apt-get update \
&& apt-get install gcc dumb-init redis-server -y \
&& apt-get clean

RUN groupadd -r python && useradd -m -r -g python python

USER python

COPY --from=builder /dependencies/requirements.txt .

RUN pip install -r requirements.txt && pip cache purge

COPY /telegram_bot/ ./telegram_bot

# Start the bit
CMD ["dumb-init", "python", "telegram_bot/main.py"]

2 changes: 1 addition & 1 deletion service/src/get_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from web3 import Web3
import os

# TODO Make function generalized
# TODO: Make function generalized


def getPrice(chain, factory, pair):
Expand Down
3 changes: 0 additions & 3 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ cleanup()
# TODO: Proper redis config file
redis-server --daemonize yes

# Start the bit
dumb-init python telegram_bot/main.py &

# Start the service
# Using cd first to prevent the issue with open() & uvicorn --app-dir
cd service/src && uvicorn --host 0.0.0.0 main:app --log-level warning&
Expand Down

0 comments on commit 7719a32

Please sign in to comment.