PeakGen
PeakGen

Reputation: 22995

Unable to build a python app with AWS SAM

I am trying to build and run the sample python application from AWS SAM. I just installed python, below is what command lines gives..

D:\Udemy Work>python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

D:\Udemy Work>pip -V
pip 21.1.3 from c:\users\user\appdata\local\programs\python\python39\lib\site-packages\pip (python 3.9)

When I run sam build, I get the following error

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

Below is my code

template.yaml

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

  Sample SAM Template for python-test

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

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
      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

app.py

AWSTemplateFormatVersion: '2010-09-09'
    Transform: AWS::Serverless-2016-10-31
    Description: >
      python-test
    
      Sample SAM Template for python-test
    
    # More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
    Globals:
      Function:
        Timeout: 3
    
    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.9
          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

If I change the run time in yaml, then I get the following error

PS D:\Udemy Work\awslambda\python-test> sam build
Building codeuri: D:\Udemy Work\awslambda\python-test\hello_world runtime: python3.9 metadata: {} functions: ['HelloWorldFunction']

Build Failed
Error: 'python3.9' runtime is not supported

What is the solution here?

Upvotes: 3

Views: 7592

Answers (3)

Michael Ribbons
Michael Ribbons

Reputation: 1975

It's 2023 and Python 3.9 and 3.10 are now supported.

The example app for your course may still be targeting Python 3.7.

Just change to your desired version:

  1. Update sam:
pip3 install -U aws-sam-cli
  1. Open template.yaml and replace Runtime: python3.7 with Runtime: python3.10

The error above should be resolved.

Upvotes: 2

MeneerDeUil
MeneerDeUil

Reputation: 9

Also ensure to update SAM CLI to latest version to support Python 3.9 runtime.

Upvotes: 0

Marcin
Marcin

Reputation: 238051

python3.9 is not supported. The supported runtimes are listed here and the latest python that you can use is python3.8.

SAM supports docker through --use-container flag. Thus you can build your packages using it for any python version you want SAM.

Upvotes: 3

Related Questions