myantyuWorld / template-vue-frontend

0 stars 0 forks source link

db = SQLiteで良いので、まずcopilotでDjangoアプリをデプロイしてみる #10

Closed myantyuWorld closed 1 year ago

myantyuWorld commented 1 year ago

以下のサイト

https://www.endpointdev.com/blog/2022/06/how-to-deploy-containerized-django-app-with-aws-copilot/

手順

  1. AWS Copilot CLI install
  2. Django startproject
  3. Deploy with AWS Copilot

手順(実際に試したもの)

参考にしたサイトは、djangoコマンドで、"startproject"するが、すでに作成済("api")のため この手順だと、dbのmigrateはしてない。 copilotでDBを作成?して、それをcopilotで作成するappと連携させる必要があるため

$ docker-compose run --rm aws-cli-container /bin/bash
$ cd api/django-api
$ copilot init -a mydjango

実行結果は以下の通り、 途中で失敗した

bash-4.2# copilot init -a mydjango
Note: It's best to run this command in the root of your Git repository.
Welcome to the Copilot CLI! We're going to walk you through some questions
to help you get set up with a containerized application on AWS. An application is a collection of
containerized services that operate together.

Note: Looks like you're creating an application using credentials set by environment variables.
Copilot will store your application metadata in this account.
We recommend using credentials from named profiles. To learn more:
https://aws.github.io/copilot-cli/docs/credentials/

Workload type: Load Balanced Web Service
Service name: mydjango-web
Docker command is not found; Copilot won't build from a Dockerfile.
X Sorry, your reply was invalid: Value is required
Image: mydjango
Port: 80
Ok great, we'll set up a Load Balanced Web Service named mydjango-web in application mydjango listening on port 80.

✔ Proposing infrastructure changes for stack mydjango-infrastructure-roles
- Creating the infrastructure for stack mydjango-infrastructure-roles                           [create complete]  [94.1s]
  - A StackSet admin role assumed by CloudFormation to manage regional stacks                   [create complete]  [39.5s]
  - An IAM role assumed by the admin role to create ECR repositories, KMS keys, and S3 buckets  [create complete]  [40.2s]
✔ The directory copilot will hold service manifests for application mydjango.

✔ Wrote the manifest for service mydjango-web at copilot/mydjango-web/manifest.yml
Your manifest contains configurations like your container size and port (:80).

- Update regional resources with stack set "mydjango-infrastructure"  [succeeded]  [0.0s]
All right, you're all set for local development.
Deploy: Yes

✔ Wrote the manifest for environment test at copilot/environments/test/manifest.yml
- Update regional resources with stack set "mydjango-infrastructure"  [succeeded]  [0.0s]
- Update regional resources with stack set "mydjango-infrastructure"  [succeeded]        [129.0s]
  - Update resources in region "ap-northeast-1"                       [create complete]  [128.3s]
    - ECR container image repository for "mydjango-web"               [create complete]  [3.2s]
    - KMS key to encrypt pipeline artifacts between stages            [create complete]  [120.3s]
    - S3 Bucket to store local artifacts                              [create complete]  [3.7s]
✔ Proposing infrastructure changes for the mydjango-test environment.
- Creating the infrastructure for the mydjango-test environment.  [create complete]  [115.1s]
  - An IAM Role for AWS CloudFormation to manage resources        [create complete]  [50.3s]
  - An IAM Role to describe resources in your environment         [create complete]  [49.3s]
✔ Provisioned bootstrap resources for environment test in region ap-northeast-1 under application mydjango.
✔ Provisioned bootstrap resources for environment test.
✔ Proposing infrastructure changes for the mydjango-test environment.
- Creating the infrastructure for the mydjango-test environment.              [update in progress]  [0
  - An ECS cluster to group your services                                     [create complete]    [0.0s]
- Creating the infrastructure for the mydjango-test environment.              [update complete]  [76.5s]
  - An ECS cluster to group your services                                     [create complete]  [0.0s]
  - A security group to allow your containers to talk to each other           [create complete]  [4.5s]
  - An Internet Gateway to connect to the public internet                     [create complete]  [17.1s]
  - Private subnet 1 for resources with no internet access                    [create complete]  [2.6s]
  - Private subnet 2 for resources with no internet access                    [create complete]  [2.6s]
  - A custom route table that directs network traffic for the public subnets  [create complete]  [9.5s]
  - Public subnet 1 for resources that can access the internet                [create complete]  [2.6s]
  - Public subnet 2 for resources that can access the internet                [create complete]  [2.6s]
  - A private DNS namespace for discovering services within the environment   [create complete]  [42.8s]
  - A Virtual Private Cloud to control networking of your AWS resources       [create complete]  [13.3s]
✔ Proposing infrastructure changes for stack mydjango-test-mydjango-web
- Creating the infrastructure for stack mydjango-test-mydjango-web                   [rollback complete]  [1104.4s]
  The following resource(s) failed to create: [Service]. Rollback reques                                  
  ted by user.                                                                                            
  - Service discovery for your services to communicate within the VPC                [delete complete]    [0.0s]
  - Update your environment's shared resources                                       [update complete]    [208.3s]
    - A security group for your load balancer allowing HTTP traffic                  [create complete]    [7.1s]
    - An Application Load Balancer to distribute public traffic to your services     [create complete]    [184.3s]
    - A load balancer listener to route HTTP traffic                                 [create in progress]  [837.3s]
  - An IAM role to update your environment stack                                     [delete complete]    [2.1s]
  - An IAM Role for the Fargate agent to make AWS API calls on your behalf           [delete complete]    [1.3s]
  - A HTTP listener rule for forwarding HTTP traffic                                 [delete complete]    [0.0s]
  - A custom resource assigning priority for HTTP listener rules                     [delete complete]    [4.6s]
  - A CloudWatch log group to hold your service logs                                 [delete complete]    [1.3s]
  - An IAM Role to describe load balancer rules for assigning a priority             [delete complete]    [1.6s]
  - An ECS service to run and maintain your tasks in the environment cluster         [delete complete]    [95.4s]
    Resource handler returned message: "Error occurred during operation 'E                                
    CS Deployment Circuit Breaker was triggered'." (RequestToken: 5873efe6                                
    -78b6-3c96-b63d-8cf92e593349, HandlerErrorCode: GeneralServiceExceptio                                
    n)                                                                                                    
    Deployments                                                                                            
               Revision  Rollout   Desired  Running  Failed  Pending                                               
      PRIMARY  1         [failed]  1        0        10      1                                                     
  - A target group to connect the load balancer to your service                      [delete complete]    [3.3s]
  - An ECS task definition to group your containers and run them on ECS              [delete complete]    [3.6s]
  - An IAM role to control permissions for the containers in your tasks              [delete complete]    [4.6s]
✘ deploy service mydjango-web to environment test: deploy service: stack mydjango-test-mydjango-web did not complete successfully and exited with status ROLLBACK_COMPLETE

エラー内容

Resource handler returned message: "Error occurred during operation 'E                                
    CS Deployment Circuit Breaker was triggered'." (RequestToken: 5873efe6                                
    -78b6-3c96-b63d-8cf92e593349, HandlerErrorCode: GeneralServiceExceptio                                
    n)

mini cheat sheet

https://github.com/myantyuWorld/template-vue-frontend/wiki#mini-cheat-sheet

myantyuWorld commented 1 year ago

原因(未精査)

これ? https://www.tsuna-engineering.jp/2022/07/aws-copilot-svc.html

myantyuWorld commented 1 year ago

まずは、、、

aws-cliコンテナだと、Dockerがインストールされていないので、途中で失敗してた まずは、以下のサイト通りやってみて、docker-compose.yml, Dockerfileを"api"と見比べてみて、 何がダメなのかを切り分けたい

以下サイトのアプリは以下で管理

/Users/*********/Documents/Study/trash/django-copilot

https://www.endpointdev.com/blog/2022/06/how-to-deploy-containerized-django-app-with-aws-copilot/

できた。 ので、実装中の"api"のDockerfileを修正して実施 以下の警告が出ている MacBookPro.M2でコンテナ作っているから?

Note: Architecture type arm64 has been detected. We will set platform 'linux/x86_64' instead. If you'd rather build and run as architecture type arm64, please change the 'platform' field in your workload manifest to 'linux/arm64'.
✔ Wrote the manifest for service myantyu-api at copilot/myantyu-api/manifest.yml
Your manifest contains configurations like your container size and port (:8080).

- Update regional resources with stack set "myantyu-api-infrastructure"  [succeeded]  [0.0s]
All right, you're all set for local development.
Deploy: Yes

✔ Wrote the manifest for environment test at copilot/environments/test/manifest.yml
- Update regional resources with stack set "myantyu-api-infrastructure"  [succeeded]  [0.0s]
- Update regional resources with stack set "myantyu-api-infrastructure"  [succeeded]        [136.2s]
  - Update resources in region "ap-northeast-1"                          [create complete]  [137.0s]
    - KMS key to encrypt pipeline artifacts between stages               [create complete]  [120.2s]
    - ECR container image repository for "myantyu-api"                   [create complete]  [0.0s]
    - S3 Bucket to store local artifacts                                 [create complete]  [1.1s]
✔ Proposing infrastructure changes for the myantyu-api-test environment.
- Creating the infrastructure for the myantyu-api-test environment.  [create complete]  [110.1s]
  - An IAM Role for AWS CloudFormation to manage resources           [create complete]  [47.8s]
  - An IAM Role to describe resources in your environment            [create complete]  [49.5s]
✔ Provisioned bootstrap resources for environment test in region ap-northeast-1 under application myantyu-api.
✔ Provisioned bootstrap resources for environment test.
✔ Proposing infrastructure changes for the myantyu-api-test environment.
- Creating the infrastructure for the myantyu-api-test environment.           [update complete]  [78.1s]
  - An ECS cluster to group your services                                     [create complete]  [2.0s]
  - A security group to allow your containers to talk to each other           [create complete]  [0.0s]
  - An Internet Gateway to connect to the public internet                     [create complete]  [14.8s]
  - Private subnet 1 for resources with no internet access                    [create complete]  [4.5s]
  - Private subnet 2 for resources with no internet access                    [create complete]  [4.5s]
  - A custom route table that directs network traffic for the public subnets  [create complete]  [10.0s]
  - Public subnet 1 for resources that can access the internet                [create complete]  [4.5s]
  - Public subnet 2 for resources that can access the internet                [create complete]  [4.5s]
  - A private DNS namespace for discovering services within the environment   [create complete]  [45.3s]
  - A Virtual Private Cloud to control networking of your AWS resources       [create complete]  [14.0s]
Building your container image: docker build -t 583492173965.dkr.ecr.ap-northeast-1.amazonaws.com/myantyu-api/myantyu-api --platform linux/x86_64 /Users/ohbay/Documents/Study/template-vue-frontend/api/django_api -f /Users/ohbay/Documents/Study/template-vue-frontend/api/django_api/Dockerfile
[+] Building 2.8s (18/18) FINISHED                                                                                                                                                                                 
 => [internal] load build definition from Dockerfile                                                                                                                                                          0.0s
 => => transferring dockerfile: 971B                                                                                                                                                                          0.0s
 => [internal] load .dockerignore                                                                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                                                               0.0s
 => [internal] load metadata for docker.io/library/python:3                                                                                                                                                   2.4s
 => [auth] library/python:pull token for registry-1.docker.io                                                                                                                                                 0.0s
 => CACHED [ 1/13] FROM docker.io/library/python:3@sha256:1274a1fb3354baf78e80cc7485771175b506a4712e49e272765dceeb0528fad1                                                                                    0.0s
 => => resolve docker.io/library/python:3@sha256:1274a1fb3354baf78e80cc7485771175b506a4712e49e272765dceeb0528fad1                                                                                             0.0s
 => [internal] load build context                                                                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                                                               0.0s
 => CANCELED [ 2/13] RUN apt-get update                                                                                                                                                                       0.2s
 => CACHED [ 3/13] RUN apt-get -y install locales &&     localedef -f UTF-8 -i ja_JP ja_JP.UTF-8                                                                                                              0.0s
 => CACHED [ 4/13] RUN apt-get install -y vim less                                                                                                                                                            0.0s
 => CACHED [ 5/13] RUN pip install --upgrade pip                                                                                                                                                              0.0s
 => CACHED [ 6/13] RUN pip install --upgrade setuptools                                                                                                                                                       0.0s
 => CACHED [ 7/13] RUN pip install django                                                                                                                                                                     0.0s
 => CACHED [ 8/13] RUN pip install psycopg2                                                                                                                                                                   0.0s
 => CACHED [ 9/13] RUN pip install djangorestframework                                                                                                                                                        0.0s
 => CACHED [10/13] RUN pip install django-filter                                                                                                                                                              0.0s
 => CACHED [11/13] RUN pip install django-cors-headers                                                                                                                                                        0.0s
 => CACHED [12/13] WORKDIR /app                                                                                                                                                                               0.0s
 => ERROR [13/13] COPY ./api/django_api /app/                                                                                                                                                                 0.0s
------
 > [13/13] COPY ./api/django_api /app/:
------
failed to compute cache key: failed to walk /var/lib/docker/tmp/buildkit-mount1658684941/api: lstat /var/lib/docker/tmp/buildkit-mount1658684941/api: no such file or directory
upload deploy resources for service myantyu-api: build and push image: build Dockerfile at /Users/ohbay/Documents/Study/template-vue-frontend/api/django_api/Dockerfile: building image: exit status 1
myantyuWorld commented 1 year ago

対応内容

参考サイトのDjangoアプリを使用し、デプロイできた。 ただし、できたため問題と対応する課題が二つできた。

1。copilotでデプロイした時の料金は?(CloudFarmationやCloudWatch, ECR, ECSなどなど) 2。copilotでのアプリ最新化方法は? 3。作成済のAPIを、デプロイ検証できたアプリに移設する