linux 在aws lambda中运行apt-get

lf5gs5x2  于 2023-01-20  发布在  Linux
关注(0)|答案(1)|浏览(123)

我有一个Lambda函数,它使用了一个库lightgbm。不幸的是,当尝试将它导入Python时,它给出了一个错误,并且在开始时说libgomp.so.1: cannot open shared object file,所以我想我需要做apt-get install libgomp1,也许还有更多的事情。
我该如何运行这些命令?
我认为最好使用Layers或类似的命令,因为每次启动Lambda时运行这些命令没有意义。
但我如何做sudo apt-获取在一个特定的文件夹,从我知道这是不可能的。
因此,我的问题归结为-如何运行这些不同的bash命令,并安装包,就像当你在Dockerfile,但在Lambda的Zip文件。
我正在使用AWS SAM进行部署和开发。

hjzp0vay

hjzp0vay1#

您可以从自己的Docker映像中运行lambda函数,在那里您几乎可以完全控制映像所包含的内容。
下面是一个在容器中执行的Python应用程序的简单示例:https://docs.aws.amazon.com/lambda/latest/dg/python-image.html
Dockerfile

FROM public.ecr.aws/lambda/python:3.8

# Install the function's dependencies using file requirements.txt
# from your project folder.

COPY requirements.txt  .
RUN  pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"

# Copy function code
COPY app.py ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.handler" ]

app.py

import sys
def handler(event, context):
    return 'Hello from AWS Lambda using Python' + sys.version + '!'

我使用SAM和一个Docker映像来运行一个大型Java应用程序的一部分作为lambda函数。

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

Description: '...'

Parameters:
  Customer:
    Type: String
    Description: Customer ID
  Environment:
    Type: String
    Description: Environment
    AllowedValues: ["production", "test"]
  AppVersion:
    Type: String
    Description: App version

Resources:

  AppLambda:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub 'app-${Customer}-${Environment}'
      PackageType: Image
      ImageUri: 'applambda:latest'
      Role: !GetAtt AppLambdaRole.Arn
      Architectures:
        - x86_64
      Timeout: 30
      MemorySize: 1024
      Description: 'App lambda endpoint (see tags for more info).'
      Environment:
        Variables:
          # This is here to improve cold start speed.
          # https://aws.amazon.com/blogs/compute/optimizing-aws-lambda-function-performance-for-java/
          JAVA_TOOL_OPTIONS: '-XX:+TieredCompilation -XX:TieredStopAtLevel=1'
      Tags:
        Name: !Sub 'app-${Customer}-${Environment}'
        Customer: !Ref Customer
        Environment: !Ref Environment
        AppVersion: !Ref AppVersion
        Application: myApp
    Metadata:
      Dockerfile: Dockerfile
      DockerContext: ./
      DockerTag: latest

  AppLambdaRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

  LogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Join ['/', ['/aws/lambda', !Ref AppLambda]]
      RetentionInDays: 7
    DeletionPolicy: Delete
    UpdateReplacePolicy: Retain

  AppLambdaPinger:
    Type: AWS::Events::Rule
    Properties:
      Description: Keeps the app lambda warm.
      ScheduleExpression: 'rate(15 minutes)'
      Targets:
        - Arn: !GetAtt AppLambda.Arn
          Id: TargetLambda
          Input: '{"ping":"pong"}'

  AppLambdaPingerPermission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref AppLambda
      Action: lambda:InvokeFunction
      Principal: events.amazonaws.com
      SourceArn: !GetAtt AppLambdaPinger.Arn

相关问题