From ef3fc8e9d254c3f6a697b1a0e53166b36b43cb43 Mon Sep 17 00:00:00 2001 From: Thibault Derousseaux Date: Wed, 9 Feb 2022 22:33:34 +0100 Subject: [PATCH 1/5] Deploy to AWS --- .github/workflows/deploy.yml | 48 ++++++++++++++++++++++++++ README.md | 67 ++---------------------------------- task-definition.json | 41 ++++++++++++++++++++++ 3 files changed, 92 insertions(+), 64 deletions(-) create mode 100644 .github/workflows/deploy.yml create mode 100644 task-definition.json diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..5efa3dd --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,48 @@ +name: Deploy + +on: + push: + branches: + - deploy-to-aws + +permissions: + id-token: write + contents: read + +jobs: + deploy: + runs-on: ubuntu-20.04 + name: Deploy + environment: deploy-to-aws + steps: + - uses: actions/checkout@v2 + - uses: aws-actions/configure-aws-credentials@v1 + with: + role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/atoti-project-template-deployment + aws-region: ${{ secrets.AWS_REGION }} + - uses: aws-actions/amazon-ecr-login@v1 + id: login-ecr + - name: Build, tag, and push Docker image to Amazon ECR + id: build-tag-and-push-docker-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY: atoti-project-template + IMAGE_TAG: ${{ github.sha }} + run: | + DOCKER_BUILDKIT=1 docker build --tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" + - name: Inline variables in the task definition + run: sed -i -e 's/AWS_ACCOUNT_ID/${{ secrets.AWS_ACCOUNT_ID }}/g' -e 's/AWS_REGION/${{ secrets.AWS_REGION }}/g' task-definition.json + - uses: aws-actions/amazon-ecs-render-task-definition@v1 + id: render-task-definition + with: + task-definition: task-definition.json + container-name: atoti-session + image: ${{ steps.build-tag-and-push-docker-image.outputs.image }} + - uses: aws-actions/amazon-ecs-deploy-task-definition@v1 + with: + task-definition: ${{ steps.render-task-definition.outputs.task-definition }} + service: atoti-project-template + cluster: atoti-project-template + wait-for-service-stability: true diff --git a/README.md b/README.md index 653a93e..9273d62 100644 --- a/README.md +++ b/README.md @@ -1,68 +1,7 @@ # atoti Project Template -This template can be used to start atoti projects where the goal is to go into production rather than prototyping in a notebook. +Switch to the [default branch](https://github.com/atoti/project-template#readme) to learn how to use this template. -On top of the `atoti` package, it comes with: +## Deploy to AWS -- Dependency management with [Poetry](https://python-poetry.org/) -- Testing with [pytest](https://docs.pytest.org/) -- Type checking with [mypy](http://mypy-lang.org/) -- Formatting with [Black](https://black.readthedocs.io/) and [isort](https://pycqa.github.io/isort/) -- Linting with [Pylint](https://www.pylint.org/) -- Continuous testing with [GitHub Actions](https://github.com/features/actions) - -## Usage - -### Installation - -- [Install `poetry`](https://python-poetry.org/docs/#installation) -- Install the dependencies: - - ```bash - poetry install - ``` - -### Commands - -- Start the session: - - ```bash - poetry run python -m app - ``` - -- Run the tests: - - ```bash - poetry run pytest - ``` - -- Check the types: - - ```bash - poetry run mypy --package app --package tests --show-error-codes - ``` - -- Sort the imports: - - ```bash - poetry run isort app/ tests/ - ``` - -- Format the code: - - ```bash - poetry run black app/ tests/ - ``` - -- Lint the code: - - ```bash - poetry run pylint app/ tests/ - ``` - -## Variants - -This repository has the following long-lived branches showcasing different aspects: - -- [`atoti-plus`](https://github.com/atoti/project-template/tree/atoti-plus) for upgrading to Atoti+. -- [`deploy-to-heroku`](https://github.com/atoti/project-template/tree/deploy-to-heroku) for a one-click deploy to Heroku. +This branch shows [the modifications](https://github.com/atoti/project-template/compare/deploy-to-aws) required to deploy a project to Amazon ECS. diff --git a/task-definition.json b/task-definition.json new file mode 100644 index 0000000..15fc8e4 --- /dev/null +++ b/task-definition.json @@ -0,0 +1,41 @@ +{ + "executionRoleArn": "atoti-project-template-role", + "containerDefinitions": [ + { + "name": "atoti-session", + "essential": true, + "healthCheck": { + "retries": 3, + "command": ["curl --fail http://localhost/versions/rest || exit 1"], + "timeout": 5, + "interval": 30 + }, + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/atoti-project-template", + "awslogs-region": "AWS_REGION", + "awslogs-stream-prefix": "ecs" + } + }, + "portMappings": [ + { + "hostPort": 80, + "protocol": "tcp", + "containerPort": 80 + } + ], + "secrets": [ + { + "name": "DATABASE_URL", + "valueFrom": "arn:aws:secretsmanager:AWS_REGION:AWS_ACCOUNT_ID:secret:atoti-project-template/database-dsn-ZS3pzB" + } + ] + } + ], + "family": "atoti-project-template", + "cpu": "1024", + "memory": "2048", + "requiresCompatibilities": ["FARGATE"], + "networkMode": "awsvpc" +} From 9e63936fcf876e038d9521c94c9e1e5c138442fc Mon Sep 17 00:00:00 2001 From: Thibault Derousseaux Date: Thu, 10 Feb 2022 09:55:34 +0100 Subject: [PATCH 2/5] Remove health check redundant with the load balancer's one --- task-definition.json | 6 ------ 1 file changed, 6 deletions(-) diff --git a/task-definition.json b/task-definition.json index 15fc8e4..d013ad4 100644 --- a/task-definition.json +++ b/task-definition.json @@ -4,12 +4,6 @@ { "name": "atoti-session", "essential": true, - "healthCheck": { - "retries": 3, - "command": ["curl --fail http://localhost/versions/rest || exit 1"], - "timeout": 5, - "interval": 30 - }, "logConfiguration": { "logDriver": "awslogs", "options": { From d15a8357d02fa074a4ea2188e607d5fc1ca0e3b2 Mon Sep 17 00:00:00 2001 From: Thibault Derousseaux Date: Fri, 22 Apr 2022 17:19:51 +0200 Subject: [PATCH 3/5] Use composite action and custom command --- .github/workflows/deploy.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 5efa3dd..c42517f 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -16,6 +16,7 @@ jobs: environment: deploy-to-aws steps: - uses: actions/checkout@v2 + - uses: ./.github/actions/install-dependencies - uses: aws-actions/configure-aws-credentials@v1 with: role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/atoti-project-template-deployment @@ -29,7 +30,7 @@ jobs: ECR_REPOSITORY: atoti-project-template IMAGE_TAG: ${{ github.sha }} run: | - DOCKER_BUILDKIT=1 docker build --tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + poetry run app build-docker $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" - name: Inline variables in the task definition From ae90c4d6322e52b271cb5bbfc9f06b56069045c6 Mon Sep 17 00:00:00 2001 From: Thibault Derousseaux Date: Fri, 22 Apr 2022 17:28:53 +0200 Subject: [PATCH 4/5] Fix command --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index c42517f..d00f91f 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -30,7 +30,7 @@ jobs: ECR_REPOSITORY: atoti-project-template IMAGE_TAG: ${{ github.sha }} run: | - poetry run app build-docker $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + poetry run app build-docker $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" - name: Inline variables in the task definition From 9607eb634f0eb84f2d7a9428acf975ffaa7494e2 Mon Sep 17 00:00:00 2001 From: Thibault Derousseaux Date: Tue, 6 Jun 2023 15:11:33 +0200 Subject: [PATCH 5/5] Attempt to fix deployment --- .github/workflows/deploy.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index d00f91f..1bb2869 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -11,12 +11,12 @@ permissions: jobs: deploy: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest name: Deploy environment: deploy-to-aws steps: - - uses: actions/checkout@v2 - - uses: ./.github/actions/install-dependencies + - uses: actions/checkout@v3 + - uses: ./.github/actions/setup - uses: aws-actions/configure-aws-credentials@v1 with: role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/atoti-project-template-deployment