Closed28

copilot で laravel 最小アプリを App Runner に deploy してみる

sogaohsogaoh

気づき1

https://twitter.com/sogaoh/status/1422927120175550466

Terraform では start_command に php artisan serve --host=0.0.0.0 を投入できて、ミドルウェアなしの php:8.0-fpm-alpine3.13 イメージで App Runner を deploy できた。

sogaohsogaoh

気づき3

copilot panic に陥る(末尾に注目)

~/ghq/github.com/sogaoh/laravel-app-runner-example main*   
❯ copilot svc deploy
Only found one service, defaulting to: lara-sample
Only found one environment, defaulting to: test
Environment test is already on the latest version v1.5.1, skip upgrade.
Sending build context to Docker daemon  3.072kB
Step 1/11 : FROM php:8.0-apache
 ---> f935acc6d330
Step 2/11 : COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
 ---> Using cache
 ---> 4e3c5d7a7087
Step 3/11 : ENV PHP_MODE=production
 ---> Running in 6ef8bbe285fb
Removing intermediate container 6ef8bbe285fb
 ---> 4be5ca78b0df
Step 4/11 : RUN mv "$PHP_INI_DIR/php.ini-$PHP_MODE" "$PHP_INI_DIR/php.ini" &&     sed -ri -e 's!((post_max_size|upload_max_filesize) = ).M!\110M!g' $PHP_INI_DIR/php.ini &&     sed -ri -e 's!/var/www/html!/var/www/html/public!g' /etc/apache2/sites-available/*.conf &&     sed -ri -e 's!/var/www/!/var/www/html/public!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf &&     apt-get update -y &&     apt-get install -y unzip &&     a2enmod rewrite
 ---> Running in 8221b342cb98
Get:1 http://deb.debian.org/debian buster InRelease [122 kB]
Get:2 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:3 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]
Get:4 http://security.debian.org/debian-security buster/updates/main amd64 Packages [298 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7907 kB]
Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [15.2 kB]
Fetched 8459 kB in 2s (5582 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following package was automatically installed and is no longer required:
  sensible-utils
Use 'apt autoremove' to remove it.
Suggested packages:
  zip
The following NEW packages will be installed:
  unzip
0 upgraded, 1 newly installed, 0 to remove and 4 not upgraded.
Need to get 172 kB of archives.
After this operation, 580 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian buster/main amd64 unzip amd64 6.0-23+deb10u2 [172 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 172 kB in 0s (13.9 MB/s)
Selecting previously unselected package unzip.
(Reading database ... 13547 files and directories currently installed.)
Preparing to unpack .../unzip_6.0-23+deb10u2_amd64.deb ...
Unpacking unzip (6.0-23+deb10u2) ...
Setting up unzip (6.0-23+deb10u2) ...
Processing triggers for mime-support (3.62) ...
Enabling module rewrite.
To activate the new configuration, you need to run:
  service apache2 restart
Removing intermediate container 8221b342cb98
 ---> 76d3d1d3ba72
Step 5/11 : COPY lara-sample /var/www/html
COPY failed: file not found in build context or excluded by .dockerignore: stat lara-sample: file does not exist
✘ build and push image: build Dockerfile at /home/sogaoh3/ghq/github.com/sogaoh/laravel-app-runner-example/appBase/ship/docker/php/Dockerfile: building image: exit status 1

~/ghq/github.com/sogaoh/laravel-app-runner-example main*   7s
❯ copilot svc deploy
✘ select service: retrieve services from workspace: yaml: unmarshal errors:
  line 17: mapping key "image" already defined at line 10

~/ghq/github.com/sogaoh/laravel-app-runner-example main*  
❯ copilot svc deploy
Only found one service, defaulting to: lara-sample
Only found one environment, defaulting to: test
Environment test is already on the latest version v1.5.1, skip upgrade.
Sending build context to Docker daemon  505.3kB
Step 1/11 : FROM php:8.0-apache
 ---> f935acc6d330
Step 2/11 : COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
 ---> Using cache
 ---> 4e3c5d7a7087
Step 3/11 : ARG PHP_MODE
 ---> Using cache
 ---> 047e66bada3c
Step 4/11 : RUN mv "$PHP_INI_DIR/php.ini-$PHP_MODE" "$PHP_INI_DIR/php.ini" &&     sed -ri -e 's!((post_max_size|upload_max_filesize) = ).M!\110M!g' $PHP_INI_DIR/php.ini &&     sed -ri -e 's!/var/www/html!/var/www/html/public!g' /etc/apache2/sites-available/*.conf &&     sed -ri -e 's!/var/www/!/var/www/html/public!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf &&     apt-get update -y &&     apt-get install -y unzip &&     a2enmod rewrite
 ---> Using cache
 ---> 22dfcf0179c4
Step 5/11 : COPY lara-sample /var/www/html
 ---> 3df0e8ef5a57
Step 6/11 : WORKDIR /var/www/html
 ---> Running in 8ac50b2073b8
Removing intermediate container 8ac50b2073b8
 ---> 69f7d1157797
Step 7/11 : ARG NO_DEV_OPT
 ---> Running in 9ecee4eec025
Removing intermediate container 9ecee4eec025
 ---> 77a3f1cccba1
Step 8/11 : RUN composer install ${NO_DEV_OPT}
 ---> Running in 6ed44888f45b
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
  - Downloading symfony/deprecation-contracts (v2.4.0)
  - Downloading symfony/console (v5.3.6)
# ・・・・
  - Downloading phar-io/version (3.1.0)
  - Downloading phar-io/manifest (2.0.3)
  - Downloading myclabs/deep-copy (1.10.2)
  - Downloading phpunit/phpunit (9.5.8)
   0/109 [>---------------------------]   0%
   1/109 [>---------------------------]   0%
   7/109 [=>--------------------------]   6%
   8/109 [==>-------------------------]   7%
  11/109 [==>-------------------------]  10%
  15/109 [===>------------------------]  13%
  21/109 [=====>----------------------]  19%
  25/109 [======>---------------------]  22%
  28/109 [=======>--------------------]  25%
  32/109 [========>-------------------]  29%
  37/109 [=========>------------------]  33%
  41/109 [==========>-----------------]  37%
  46/109 [===========>----------------]  42%
  48/109 [============>---------------]  44%
  52/109 [=============>--------------]  47%
  54/109 [=============>--------------]  49%
  60/109 [===============>------------]  55%
  67/109 [=================>----------]  61%
  69/109 [=================>----------]  63%
  71/109 [==================>---------]  65%
  78/109 [====================>-------]  71%
  80/109 [====================>-------]  73%
  83/109 [=====================>------]  76%
  90/109 [=======================>----]  82%
  92/109 [=======================>----]  84%
  96/109 [========================>---]  88%
 102/109 [==========================>-]  93%
 104/109 [==========================>-]  95%
 108/109 [===========================>]  99%
 109/109 [============================] 100%
  - Installing webmozart/assert (1.10.0): Extracting archive
  - Installing dragonmantank/cron-expression (v3.1.0): Extracting archive
# ・・・・
  - Installing phpunit/phpunit (9.5.8): Extracting archive
   0/109 [>---------------------------]   0%
  10/109 [==>-------------------------]   9%
  17/109 [====>-----------------------]  15%
  27/109 [======>---------------------]  24%
  37/109 [=========>------------------]  33%
  43/109 [===========>----------------]  39%
 109/109 [============================] 100%
Generating optimized autoload files
 ---> f9079e0e4d98
[Warning] One or more build-args [TZ] were not consumed
Removing intermediate container 591cb9af78bc
 ---> 18211a344e29
Step 11/11 : RUN chmod -R a+w storage &&     chmod -R a+w bootstrap/cache
 ---> Running in 7549f923b3e5
Removing intermediate container 7549f923b3e5
 ---> f9079e0e4d98
[Warning] One or more build-args [TZ] were not consumed
Successfully built f9079e0e4d98
Successfully tagged 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/sogaoh-app/lara-sample:latest
WARNING! Your password will be stored unencrypted in /home/sogaoh3/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
Using default tag: latest
The push refers to repository [123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/sogaoh-app/lara-sample]
1ceaf2f568a8: Pushed
2db4f7f98ac9: Pushed
d91f0c662c17: Pushed
da69fe65ebab: Pushed
25db36eb6e02: Pushed
21324278135c: Pushed
9d780940e188: Pushed
913e9b2f1f98: Pushed
00e0c10a90fb: Pushed
8676c125e011: Pushed
d327808125b0: Pushed
007070d11f9e: Pushed
86b5e34374f3: Pushed
c52f69d9a297: Pushed
c24f05541085: Pushed
82f581b09510: Pushed
3b7e206db54c: Pushed
575b147c7c31: Pushed
814bff734324: Pushed
latest: digest: sha256:7f397e4ae5e17cd80070eb0ffdf9f889f2e61d5b1016218a4623430d73ba5d8a size: 4297
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xfa65d0]

goroutine 1 [running]:
github.com/aws/copilot-cli/internal/pkg/deploy/cloudformation/stack.(*appRunnerWkld).Parameters(0xc0001f1a00, 0x0, 0x0, 0x0, 0x0, 0x0)
        /codebuild/output/src729422157/src/internal/pkg/deploy/cloudformation/stack/workload.go:325 +0x250
github.com/aws/copilot-cli/internal/pkg/deploy/cloudformation.toStack(0x2034000, 0xc000b4af00, 0x10, 0x18, 0x7f973cacc108)
        /codebuild/output/src729422157/src/internal/pkg/deploy/cloudformation/cloudformation.go:390 +0xcb
github.com/aws/copilot-cli/internal/pkg/deploy/cloudformation.CloudFormation.DeployService(0x2045220, 0xc0003ea3e0, 0x2004da0, 0xc0003ea440, 0x2004d20, 0xc0003e65c0, 0x2004e40, 0xc0003e66c0, 0xc0003ea7e0, 0x203de40, ...)
        /codebuild/output/src729422157/src/internal/pkg/deploy/cloudformation/workload.go:18 +0x4d
github.com/aws/copilot-cli/internal/pkg/cli.(*deploySvcOpts).deploySvc(0xc00034cd80, 0x0, 0x0, 0x0, 0x0)
        /codebuild/output/src729422157/src/internal/pkg/cli/svc_deploy.go:555 +0x1aa
github.com/aws/copilot-cli/internal/pkg/cli.(*deploySvcOpts).Execute(0xc00034cd80, 0x0, 0x0)
        /codebuild/output/src729422157/src/internal/pkg/cli/svc_deploy.go:190 +0x3fa
github.com/aws/copilot-cli/internal/pkg/cli.buildSvcDeployCmd.func1(0xc0002fc500, 0x2d57df0, 0x0, 0x0, 0xc00011fd48, 0x0)
        /codebuild/output/src729422157/src/internal/pkg/cli/svc_deploy.go:789 +0x114
github.com/aws/copilot-cli/internal/pkg/cli.runCmdE.func1(0xc0002fc500, 0x2d57df0, 0x0, 0x0, 0x0, 0x0)
        /codebuild/output/src729422157/src/internal/pkg/cli/cli.go:66 +0x6d
github.com/spf13/cobra.(*Command).execute(0xc0002fc500, 0x2d57df0, 0x0, 0x0, 0xc0002fc500, 0x2d57df0)
        /go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:856 +0x47c
github.com/spf13/cobra.(*Command).ExecuteC(0xc0002b7180, 0x0, 0x0, 0x0)
        /go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:974 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
        /go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:902
main.main()
        /codebuild/output/src729422157/src/cmd/copilot/main.go:25 +0x2b

~/ghq/github.com/sogaoh/laravel-app-runner-example main*   52s```

sogaohsogaoh
panic のスタックトレース

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xfa65d0]

goroutine 1 [running]:
github.com/aws/copilot-cli/internal/pkg/deploy/cloudformation/stack.(*appRunnerWkld).Parameters(0xc0001f1a00, 0x0, 0x0, 0x0, 0x0, 0x0)
        /codebuild/output/src729422157/src/internal/pkg/deploy/cloudformation/stack/workload.go:325 +0x250
github.com/aws/copilot-cli/internal/pkg/deploy/cloudformation.toStack(0x2034000, 0xc000b4af00, 0x10, 0x18, 0x7f973cacc108)
        /codebuild/output/src729422157/src/internal/pkg/deploy/cloudformation/cloudformation.go:390 +0xcb
github.com/aws/copilot-cli/internal/pkg/deploy/cloudformation.CloudFormation.DeployService(0x2045220, 0xc0003ea3e0, 0x2004da0, 0xc0003ea440, 0x2004d20, 0xc0003e65c0, 0x2004e40, 0xc0003e66c0, 0xc0003ea7e0, 0x203de40, ...)
        /codebuild/output/src729422157/src/internal/pkg/deploy/cloudformation/workload.go:18 +0x4d
github.com/aws/copilot-cli/internal/pkg/cli.(*deploySvcOpts).deploySvc(0xc00034cd80, 0x0, 0x0, 0x0, 0x0)
        /codebuild/output/src729422157/src/internal/pkg/cli/svc_deploy.go:555 +0x1aa
github.com/aws/copilot-cli/internal/pkg/cli.(*deploySvcOpts).Execute(0xc00034cd80, 0x0, 0x0)
        /codebuild/output/src729422157/src/internal/pkg/cli/svc_deploy.go:190 +0x3fa
github.com/aws/copilot-cli/internal/pkg/cli.buildSvcDeployCmd.func1(0xc0002fc500, 0x2d57df0, 0x0, 0x0, 0xc00011fd48, 0x0)
        /codebuild/output/src729422157/src/internal/pkg/cli/svc_deploy.go:789 +0x114
github.com/aws/copilot-cli/internal/pkg/cli.runCmdE.func1(0xc0002fc500, 0x2d57df0, 0x0, 0x0, 0x0, 0x0)
        /codebuild/output/src729422157/src/internal/pkg/cli/cli.go:66 +0x6d
github.com/spf13/cobra.(*Command).execute(0xc0002fc500, 0x2d57df0, 0x0, 0x0, 0xc0002fc500, 0x2d57df0)
        /go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:856 +0x47c
github.com/spf13/cobra.(*Command).ExecuteC(0xc0002b7180, 0x0, 0x0, 0x0)
        /go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:974 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
        /go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:902
main.main()
        /codebuild/output/src729422157/src/cmd/copilot/main.go:25 +0x2b
sogaohsogaoh

環境情報

copilot 1.9.0 on

cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"
docker --version
Docker version 20.10.4, build d3cb89e
sogaohsogaoh

Mac でも同様にダメ

❯ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.7
BuildVersion:	19H1323
❯ docker --version
Docker version 20.10.7, build f0df350
sogaohsogaoh
Dockerfile を build するのを諦めて image 指定にしたら deploy 成功した・・・panic になったのはなぜだ?

sogaohsogaoh
後始末(削除)-> App Runner も ECR もなくなってた

sogaohsogaoh

セキュリティグループが残ってた。
手で消した。

sogaohsogaoh

きれいに後始末するのであれば、「手で消す」はやっちゃいけないな。。

development env を init したのだけど、test env を流用したのでこれはもうわけわからない状態になってしまってるな。。env delete できるかしら。。(やってみるけど失敗しそう)

sogaohsogaoh

どうやら後始末できたっぽい

test env -> (VPCが残ったのでマネコンで手動削除) -> development env -> app の順に削除成功

sogaohsogaoh

KMS が削除されるのは1ヶ月後。7日にしたかったな。。$0.01 / day 抜かれそう・・・

sogaohsogaoh

copilot init 時に作られるリソース(残っていた CloudFormation のスタックより)

app init

  • (CloudFormation用のIAM設定)
    • AdministrationRole (AWS::IAM::Role)
    • ExecutionRole (AWS::IAM::Role)

env init

  • (CodePipeline用の設定)
    • KMSKey (AWS::KMS::Key)
    • PipelineBuiltArtifactBucket (AWS::S3::Bucket)
    • PipelineBuiltArtifactBucketPolicy (AWS::S3::BucketPolicy)
copilot CloudFormation template の流し込み ? : 量が多い。。書く気をなくした。

sogaohsogaoh

svc init

  • ECR Repository
    • (CloudFormation スタックには何も作られてない)
sogaohsogaoh

svc deploy

  • (AppRunner Service)
    • AccessRole (AWS::IAM::Role)
    • InstanceRole (AWS::IAM::Role)
    • Service (AWS::AppRunner::Service)
panic 起きてたところから先の進捗状況表示スクリーンショット

sogaohsogaoh

後始末(2)

  • svc delete
  • env delete
  • app delete

の順に実行

経過スクリーンショット

このスクラップは2021/08/06にクローズされました