AWS SAM构建:运行时Python版本与本地PC Python版本不同

发布于 2025-02-11 17:00:20 字数 10793 浏览 3 评论 0原文

我正在尝试构建使用SAM。

我的PC上有Python 3.9,但是我使用Python 3.7作为Lambda的运行时间。

当lambda从API网关触发时,它将在public.ecr.aws/sam/emulation-python3.9:rapid-1.40.0.0.0.0-x86_64容器中运行

lambda 下面的错误

Expected version: python3.7, Found version: /bin/python.

是我的模板。yml

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

  Sample SAM Template for sam-app

# 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.7
      Timeout: 500
      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: /copy_files
            Method: post

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

,当我尝试构建时,我会得到

$ sam build --debug --profile covid
2022-06-30 09:33:28,175 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-06-30 09:33:28,176 | Using config file: samconfig.toml, config environment: default
2022-06-30 09:33:28,176 | Expand command line arguments to:
2022-06-30 09:33:28,176 | --template_file=/home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/template.yaml --build_dir=.aws-sam/build --cache_dir=.aws-sam/cache 
2022-06-30 09:33:28,692 | 'build' command is called
2022-06-30 09:33:28,699 | No Parameters detected in the template
2022-06-30 09:33:28,762 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,762 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,763 | 2 stacks found in the template
2022-06-30 09:33:28,763 | No Parameters detected in the template
2022-06-30 09:33:28,780 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,780 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,781 | 2 resources found in the stack 
2022-06-30 09:33:28,781 | No Parameters detected in the template
2022-06-30 09:33:28,798 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,798 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,799 | Found Serverless function with name='HelloWorldFunction' and CodeUri='hello_world/'
2022-06-30 09:33:28,799 | --base-dir is not presented, adjusting uri hello_world/ relative to /home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/template.yaml
2022-06-30 09:33:28,799 | No Parameters detected in the template
2022-06-30 09:33:28,833 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,833 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-06-30 09:33:30,036 | No Parameters detected in the template
2022-06-30 09:33:30,065 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-06-30 09:33:30,065 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-06-30 09:33:30,066 | Your template contains a resource with logical ID "ServerlessRestApi", which is a reserved logical ID in AWS SAM. It could result in unexpected behaviors and is not recommended.
2022-06-30 09:33:30,113 | Instantiating build definitions
2022-06-30 09:33:30,139 | Unique function build definition found, adding as new (Function Build Definition: BuildDefinition(python3.7, /home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/hello_world, Zip, , fb2df3d3-6088-413d-a68d-fef68238b0c7, {}, {}, x86_64, []), Function: Function(function_id='HelloWorldFunction', name='HelloWorldFunction', functionname='HelloWorldFunction', runtime='python3.7', memory=None, timeout=500, handler='app.lambda_handler', imageuri=None, packagetype='Zip', imageconfig=None, codeuri='/home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/hello_world', environment=None, rolearn=None, layers=[], events={'HelloWorld': {'Type': 'Api', 'Properties': {'Path': '/copy_files', 'Method': 'post', 'RestApiId': 'ServerlessRestApi'}}}, metadata={'SamResourceId': 'HelloWorldFunction'}, inlinecode=None, codesign_config_arn=None, architectures=['x86_64'], stack_path=''))
2022-06-30 09:33:30,142 | Building codeuri: /home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/hello_world runtime: python3.7 metadata: {} architecture: x86_64 functions: ['HelloWorldFunction']
2022-06-30 09:33:30,142 | Building to following folder /home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/.aws-sam/build/HelloWorldFunction
2022-06-30 09:33:30,143 | Loading workflow module 'aws_lambda_builders.workflows'
2022-06-30 09:33:30,147 | Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2022-06-30 09:33:30,150 | Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
2022-06-30 09:33:30,152 | Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
2022-06-30 09:33:30,154 | Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', application_framework=None)'
2022-06-30 09:33:30,156 | Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
2022-06-30 09:33:30,161 | Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
2022-06-30 09:33:30,165 | Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
2022-06-30 09:33:30,167 | Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
2022-06-30 09:33:30,169 | Registering workflow 'CustomMakeBuilder' with capability 'Capability(language='provided', dependency_manager=None, application_framework=None)'
2022-06-30 09:33:30,172 | Registering workflow 'NodejsNpmEsbuildBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm-esbuild', application_framework=None)'
2022-06-30 09:33:30,172 | Found workflow 'PythonPipBuilder' to support capabilities 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2022-06-30 09:33:30,301 | Invalid executable for python at /usr/bin/python
Traceback (most recent call last):
  File "aws_lambda_builders/workflow.py", line 66, in wrapper
  File "aws_lambda_builders/workflows/python_pip/validator.py", line 51, in validate
aws_lambda_builders.exceptions.MisMatchRuntimeError: python executable found in your path does not match runtime. 
 Expected version: python3.7, Found version: /usr/bin/python. 
 Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30
2022-06-30 09:33:30,351 | Invalid executable for python at /bin/python
Traceback (most recent call last):
  File "aws_lambda_builders/workflow.py", line 66, in wrapper
  File "aws_lambda_builders/workflows/python_pip/validator.py", line 51, in validate
aws_lambda_builders.exceptions.MisMatchRuntimeError: python executable found in your path does not match runtime. 
 Expected version: python3.7, Found version: /bin/python. 
 Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30

Build Failed
2022-06-30 09:33:30,353 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '808341cb-9fc5-415b-aa92-6b39608b1ed0', 'installationId': 'a87120b0-ef01-4fa9-a2b4-d881501b50fc', 'sessionId': 'fea24ff9-8135-41b2-b69e-f37904942060', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.7.10', 'samcliVersion': '1.40.0', 'awsProfileProvided': True, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'metricSpecificAttributes': {'projectType': 'CFN'}, 'duration': 2177, 'exitReason': 'WorkflowFailedError', 'exitCode': 1}}]}
2022-06-30 09:33:31,729 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
Error: PythonPipBuilder:Validation - Binary validation failed for python, searched for python in following locations  : ['/usr/bin/python', '/bin/python'] which did not satisfy constraints for runtime: python3.7. Do you have python for runtime: python3.7 on your PATH?

I am trying to build an using sam.

I have python 3.9 on my pc, but I am using python 3.7 as runtime for my lambda.

When lambda is triggered from api gateway it runs the lambda in public.ecr.aws/sam/emulation-python3.9:rapid-1.40.0-x86_64 container

But when it builds it uses the systems python and throws error

Expected version: python3.7, Found version: /bin/python.

Below is my template.yml

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

  Sample SAM Template for sam-app

# 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.7
      Timeout: 500
      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: /copy_files
            Method: post

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

and when i try to build i get

$ sam build --debug --profile covid
2022-06-30 09:33:28,175 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-06-30 09:33:28,176 | Using config file: samconfig.toml, config environment: default
2022-06-30 09:33:28,176 | Expand command line arguments to:
2022-06-30 09:33:28,176 | --template_file=/home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/template.yaml --build_dir=.aws-sam/build --cache_dir=.aws-sam/cache 
2022-06-30 09:33:28,692 | 'build' command is called
2022-06-30 09:33:28,699 | No Parameters detected in the template
2022-06-30 09:33:28,762 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,762 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,763 | 2 stacks found in the template
2022-06-30 09:33:28,763 | No Parameters detected in the template
2022-06-30 09:33:28,780 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,780 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,781 | 2 resources found in the stack 
2022-06-30 09:33:28,781 | No Parameters detected in the template
2022-06-30 09:33:28,798 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,798 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,799 | Found Serverless function with name='HelloWorldFunction' and CodeUri='hello_world/'
2022-06-30 09:33:28,799 | --base-dir is not presented, adjusting uri hello_world/ relative to /home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/template.yaml
2022-06-30 09:33:28,799 | No Parameters detected in the template
2022-06-30 09:33:28,833 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-06-30 09:33:28,833 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-06-30 09:33:30,036 | No Parameters detected in the template
2022-06-30 09:33:30,065 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-06-30 09:33:30,065 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-06-30 09:33:30,066 | Your template contains a resource with logical ID "ServerlessRestApi", which is a reserved logical ID in AWS SAM. It could result in unexpected behaviors and is not recommended.
2022-06-30 09:33:30,113 | Instantiating build definitions
2022-06-30 09:33:30,139 | Unique function build definition found, adding as new (Function Build Definition: BuildDefinition(python3.7, /home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/hello_world, Zip, , fb2df3d3-6088-413d-a68d-fef68238b0c7, {}, {}, x86_64, []), Function: Function(function_id='HelloWorldFunction', name='HelloWorldFunction', functionname='HelloWorldFunction', runtime='python3.7', memory=None, timeout=500, handler='app.lambda_handler', imageuri=None, packagetype='Zip', imageconfig=None, codeuri='/home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/hello_world', environment=None, rolearn=None, layers=[], events={'HelloWorld': {'Type': 'Api', 'Properties': {'Path': '/copy_files', 'Method': 'post', 'RestApiId': 'ServerlessRestApi'}}}, metadata={'SamResourceId': 'HelloWorldFunction'}, inlinecode=None, codesign_config_arn=None, architectures=['x86_64'], stack_path=''))
2022-06-30 09:33:30,142 | Building codeuri: /home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/hello_world runtime: python3.7 metadata: {} architecture: x86_64 functions: ['HelloWorldFunction']
2022-06-30 09:33:30,142 | Building to following folder /home/simha_personal_data/programming_arch_firefox/extra/Unsorted/vid/web_dev/hss_iqgateway/aws_sam/sam-app/.aws-sam/build/HelloWorldFunction
2022-06-30 09:33:30,143 | Loading workflow module 'aws_lambda_builders.workflows'
2022-06-30 09:33:30,147 | Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2022-06-30 09:33:30,150 | Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
2022-06-30 09:33:30,152 | Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
2022-06-30 09:33:30,154 | Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', application_framework=None)'
2022-06-30 09:33:30,156 | Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
2022-06-30 09:33:30,161 | Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
2022-06-30 09:33:30,165 | Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
2022-06-30 09:33:30,167 | Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
2022-06-30 09:33:30,169 | Registering workflow 'CustomMakeBuilder' with capability 'Capability(language='provided', dependency_manager=None, application_framework=None)'
2022-06-30 09:33:30,172 | Registering workflow 'NodejsNpmEsbuildBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm-esbuild', application_framework=None)'
2022-06-30 09:33:30,172 | Found workflow 'PythonPipBuilder' to support capabilities 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2022-06-30 09:33:30,301 | Invalid executable for python at /usr/bin/python
Traceback (most recent call last):
  File "aws_lambda_builders/workflow.py", line 66, in wrapper
  File "aws_lambda_builders/workflows/python_pip/validator.py", line 51, in validate
aws_lambda_builders.exceptions.MisMatchRuntimeError: python executable found in your path does not match runtime. 
 Expected version: python3.7, Found version: /usr/bin/python. 
 Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30
2022-06-30 09:33:30,351 | Invalid executable for python at /bin/python
Traceback (most recent call last):
  File "aws_lambda_builders/workflow.py", line 66, in wrapper
  File "aws_lambda_builders/workflows/python_pip/validator.py", line 51, in validate
aws_lambda_builders.exceptions.MisMatchRuntimeError: python executable found in your path does not match runtime. 
 Expected version: python3.7, Found version: /bin/python. 
 Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30

Build Failed
2022-06-30 09:33:30,353 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '808341cb-9fc5-415b-aa92-6b39608b1ed0', 'installationId': 'a87120b0-ef01-4fa9-a2b4-d881501b50fc', 'sessionId': 'fea24ff9-8135-41b2-b69e-f37904942060', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.7.10', 'samcliVersion': '1.40.0', 'awsProfileProvided': True, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'metricSpecificAttributes': {'projectType': 'CFN'}, 'duration': 2177, 'exitReason': 'WorkflowFailedError', 'exitCode': 1}}]}
2022-06-30 09:33:31,729 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
Error: PythonPipBuilder:Validation - Binary validation failed for python, searched for python in following locations  : ['/usr/bin/python', '/bin/python'] which did not satisfy constraints for runtime: python3.7. Do you have python for runtime: python3.7 on your PATH?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2025-02-18 17:00:21

解决方案是使用- 用例

sam build --debug --use-container

The solution is to use --use-container

sam build --debug --use-container
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文