1

我有一个 AWS CodeBuild 脚本,它使用两个第 3 方(纯)Python 包构建 Lambda 层,markdown并且yaml-

version: 0.2
phases:
  install:
    runtime-versions:
      python: $PYTHON_VERSION
    commands:
      - echo "installing pip packages"
      - apt-get update
      - apt-get install zip python3-pip -y
      - mkdir -p site-packages
      - pip3 install --upgrade --target site-packages markdown
      - pip3 install --upgrade --target site-packages pyyaml
  build:
    commands:
      - echo "building layer deployable"
      - mkdir -p build/python
      - cd build && cp -r ../site-packages/markdown python/markdown && cd ..
      - cd build && cp -r ../site-packages/yaml python/yaml && cd ..
artifacts:
  files:
    - '**/*'
  base-directory: build
  name: layer.zip

然后我有一个 Cloudformation 模板来部署它,然后分别测试每个包导入 -

---
AWSTemplateFormatVersion: 2010-09-09
Parameters:
  AppName:
    Type: String
  S3DeployBucket:
    Type: String
  S3LayerKey:
    Type: String
  LambdaRuntime:
    Type: String
    Default: python3.7
  LambdaMemory:
    Type: Number
    Default: 512
  LambdaTimeout:
    Type: Number
    Default: 30
  LambdaHandler:
    Type: String
    Default: index.handler
Outputs:
  MarkdownLambdaARN:
    Value: !GetAtt MarkdownLambda.Arn
  YamlLambdaARN:
    Value: !GetAtt YamlLambda.Arn
Resources:
  MarkdownLambda:
    Properties:
      Code:
        ZipFile: |
          import markdown
          def handler(event, context):
            return "Hello World markdown!"
      FunctionName: !Sub "${AppName}-markdown-lambda"
      Handler: !Ref LambdaHandler
      Layers:
        - Ref: LambdaLayer
      MemorySize: !Ref LambdaMemory
      Role: !GetAtt LambdaRole.Arn
      Runtime: !Ref LambdaRuntime
      Timeout: !Ref LambdaTimeout
    Type: AWS::Lambda::Function
  YamlLambda:
    Properties:
      Code:
        ZipFile: |
          import yaml
          def handler(event, context):
            return "Hello World yaml!"
      FunctionName: !Sub "${AppName}-yaml-lambda"
      Handler: !Ref LambdaHandler
      Layers:
        - Ref: LambdaLayer
      MemorySize: !Ref LambdaMemory
      Role: !GetAtt LambdaRole.Arn
      Runtime: !Ref LambdaRuntime
      Timeout: !Ref LambdaTimeout
    Type: AWS::Lambda::Function
  LambdaRole:
    Properties:
      Policies:
        - PolicyDocument:
            Statement:
              - Action: logs:*
                Effect: Allow
                Resource: '*'
            Version: 2012-10-17
          PolicyName: !Sub "${AppName}-lambda-role"
      AssumeRolePolicyDocument:
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
        Version: 2012-10-17
    Type: AWS::IAM::Role
  LambdaLayer:
    Properties:
      CompatibleRuntimes:
        - Ref: LambdaRuntime
      Content:
        S3Bucket: !Ref S3DeployBucket
        S3Key: !Ref S3LayerKey
      LayerName: !Sub "${AppName}-layer"
    Type: AWS::Lambda::LayerVersion

请特别注意两个内联 Lambda 函数。

当我 ping YAML Lambda 时,我得到 -

justin@justin-XPS-13-9360:~/work$ python ping_lambda.py arn:aws:lambda:eu-west-1:119552584133:function:markdown-layer-demo-yaml-lambda
b'"Hello World yaml!"'

但是,当我 ping Markdown Lambda 时,我得到 -

justin@justin-XPS-13-9360:~/work$ python ping_lambda.py arn:aws:lambda:eu-west-1:119552584133:function:markdown-layer-demo-markdown-lambda
b'{"errorMessage": "Unable to import module \'index\': No module named \'importlib_metadata\'", "errorType": "Runtime.ImportModuleError"}'

因此,随着 YAML Lambda 的工作,该层似乎构建得很好。CodeBuild 代码段中对pyyaml和包的处理是相同的。markdownmarkdown包似乎是纯 Python 并且似乎不需要任何额外的依赖项即可工作。

那么,当工作正常markdown时,导致导入 Lambda 失败的包结构是什么?yaml

TIA。

4

0 回答 0