AWS SAMパイプライン機能で .NET Core 3.1 を指定するとパイプラインの実行に失敗する

2021.07.27

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

いわさです。

先日、以下のSAMパイプライン機能を試しました。

その際に、SAMの初期化時に.NET Core 3.1を選択するとパイプラインが失敗しました。
現時点では、動作させるためにはパイプラインの修正を行う必要がありますので方法を紹介します。

.NET Core 3.1を選択しパイプラインを失敗させる

sam init.NET Core 3.1ランタイムを選択します。

Which runtime would you like to use?
    1 - nodejs14.x
    2 - python3.8
    3 - ruby2.7
    4 - go1.x
    5 - java11
    6 - dotnetcore3.1
    7 - nodejs12.x
    8 - nodejs10.x
    9 - python3.7
    10 - python3.6
    11 - python2.7
    12 - ruby2.5
    13 - java8.al2
    14 - java8
    15 - dotnetcore2.1
Runtime: 6

前回のブログの要領でCI/CDパイプラインをデプロイします。

すると...

BuildAndPackageで失敗しました。

失敗情報の確認

デプロイステージの結果から見てみます。

COMMAND_EXECUTION_ERROR: Error while executing command: sam build --use-container --template ${SAM_TEMPLATE}. Reason: exit status 1

CodeBuildの詳細ログを確認してみましょう。

[Container] 2021/07/23 13:08:37 Running command sam build --use-container --template ${SAM_TEMPLATE}

    SAM CLI now collects telemetry to better understand customer needs.

    You can OPT OUT and disable telemetry collection by setting the
    environment variable SAM_CLI_TELEMETRY=0 in your shell.
    Thanks for your help!

    Learn More: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-telemetry.html

Starting Build inside a container
Building codeuri: /codebuild/output/src408350542/src/src/HelloWorld runtime: dotnetcore3.1 metadata: {} functions: ['HelloWorldFunction']

Build Failed
Error: We do not support building .NET Core Lambda functions within a container. Try building without the container. Most .NET Core functions will build successfully.

[Container] 2021/07/23 13:08:39 Command did not exit successfully sam build --use-container --template ${SAM_TEMPLATE} exit status 1
[Container] 2021/07/23 13:08:39 Phase complete: BUILD State: FAILED
[Container] 2021/07/23 13:08:39 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: sam build --use-container --template ${SAM_TEMPLATE}. Reason: exit status 1
[Container] 2021/07/23 13:08:39 Entering phase POST_BUILD

.NET Core 3.1ではLambda関数のコンテナ実行がサポートされていないとのこと。
どこのことを指しているかというと、buildspec_build_package.ymlのコマンドでコンテナを使うよう指定されている。

pipeline/buildspec_build_package.yml

version: 0.2
phases:
  install:
    runtime-versions:
      python: 3.8
    commands:
      - pip install --upgrade pip
      - pip install --upgrade awscli aws-sam-cli
      # Enable docker https://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker-custom-image.html
      - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2 &
      - timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
  build:
    commands:
      #- sam build --use-container --template ${SAM_TEMPLATE}
      - sam build --template ${SAM_TEMPLATE}
      - . ./assume-role.sh ${TESTING_PIPELINE_EXECUTION_ROLE} test-package
      - sam package --s3-bucket ${TESTING_ARTIFACT_BUCKET}
                    --region ${TESTING_REGION}
                    --output-template-file packaged-test.yaml
      - . ./assume-role.sh ${PROD_PIPELINE_EXECUTION_ROLE} prod-package
      - sam package --s3-bucket ${PROD_ARTIFACT_BUCKET}
                    --region ${PROD_REGION}
                    --output-template-file packaged-prod.yaml
artifacts:
  files:
    - packaged-test.yaml
    - packaged-prod.yaml
    - assume-role.sh
    - pipeline/*

pipeline/buildspec_feature.yml

version: 0.2
phases:
  install:
    runtime-versions:
      python: 3.8
    commands:
      - pip install --upgrade pip
      - pip install --upgrade awscli aws-sam-cli
      # Enable docker https://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker-custom-image.html
      - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2 &
      - timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
  build:
    commands:
      #- sam build --use-container --template ${SAM_TEMPLATE}
      - sam build --template ${SAM_TEMPLATE}
      - . ./assume-role.sh ${TESTING_PIPELINE_EXECUTION_ROLE} feature-deploy
      - sam deploy --stack-name $(echo ${FEATURE_BRANCH_NAME} | tr -cd '[a-zA-Z0-9-]')
                    --capabilities CAPABILITY_IAM
                    --region ${TESTING_REGION}
                    --s3-bucket ${TESTING_ARTIFACT_BUCKET}
                    --no-fail-on-empty-changeset
                    --role-arn ${TESTING_CLOUDFORMATION_EXECUTION_ROLE}

コードをリモートブランチへプッシュし、失敗し停止していたパイプラインを再試行します。

パイプラインの実行に成功し、デプロイまで完了しました。
サイトへアクセスしてみます。

表示されました。
念の為パイプラインの動作確認のためソースコードを修正しプッシュしてみます。

無事、変更が反映されました。

注意

SAMパイプライン機能はパブリックプレビューであり、これは現時点での問題を解決するためのものとなります。
将来的にはおそらく機能側で修正されると思います。