AWS SAM CLIをWindowsで動作させたとき、$sam buildでエラーが出た問題を解決した

AWS Lambdaをローカルで実行するには、AWS SAM CLIというものを使えばよいと聞いたので、 AWS SAM CLIを使ったLambdaのローカル実行と簡単デプロイを試してみた。

困ったこと(エラー内容)

$ sam buildを実行時、次のエラーが表示される

Error: PythonPipBuilder:Validation - Binary validation failed for python, searched for python in following locations  : ['C:\\Python38\\python.EXE', 'C:\\Users\\Owner\\AppData\\Local\\Programs\\Python\\Python36\\python.EXE', 'C:\\Users\\Owner\\AppData\\Local\\Microsoft\\WindowsApps\\python.EXE', 'C:\\Users\\Owner\\AppData\\Local\\Microsoft\\WindowsApps\\python3.EXE'] which did not satisfy constraints for runtime: python3.9. Do you have python for runtime: python3.9 on your PATH?

実行環境

詳細など

3年前の記事を参考にしているためか、OSの差異のためか(参考記事はMac、本記事はWindows10で実行している)、 $ sam init時に尋ねられる内容は参考記事とはやや異なっていた。 具体的には、「Which runtime would you like to use?」という質問事項はなかった。

また、AWS SAM, AWS CLIのインストールは次の公式リファレンスを参考にした。

AWS SAM CLIのインストール AWS CLI の最新バージョンを使用してインストールまたは更新を行う

ここで$ sam buildを実行したとき、次のエラーが表示された。

Error: PythonPipBuilder:Validation - Binary validation failed for python, searched for python in following locations  : ['C:\\Python38\\python.EXE', 'C:\\Users\\Owner\\AppData\\Local\\Programs\\Python\\Python36\\python.EXE', 'C:\\Users\\Owner\\AppData\\Local\\Microsoft\\WindowsApps\\python.EXE', 'C:\\Users\\Owner\\AppData\\Local\\Microsoft\\WindowsApps\\python3.EXE'] which did not satisfy constraints for runtime: python3.9. Do you have python for runtime: python3.9 on your PATH?

Python3.9入れてないからエラーが出ているだけだが、Pythonのインストーラを探しにいくと、どうやらもうインストーラが配布されていないようだった。

プログラムが3.9を探しにいってるのでエラーになっているとも考えられ、これを3.8を探すように修正すれば動作するのではないかと感じたので template.yamlを次のように修正したところ、問題なく実行できた。 (後続の$ sam local invokeについても、問題なく実行できた)

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  test_sam

  Sample SAM Template for test_sam

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3
    MemorySize: 128

    Tracing: Active
  Api:
    TracingEnabled: true
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8  # ここを3.9から3.8へ修正
      Architectures:
      - x86_64
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

  ApplicationResourceGroup:
    Type: AWS::ResourceGroups::Group
    Properties:
      Name:
        Fn::Sub: ApplicationInsights-SAM-${AWS::StackName}
      ResourceQuery:
        Type: CLOUDFORMATION_STACK_1_0
  ApplicationInsightsMonitoring:
    Type: AWS::ApplicationInsights::Application
    Properties:
      ResourceGroupName:
        Ref: ApplicationResourceGroup
      AutoConfigurationEnabled: 'true'
Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: API Gateway endpoint URL for Prod stage for Hello World function
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: Hello World Lambda Function ARN
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: Implicit IAM Role created for Hello World function
    Value: !GetAtt HelloWorldFunctionRole.Arn

参考

devblog.thebase.in