简体   繁体   中英

Running apt-get in aws lambda

I have a Lambda function that uses a library lightgbm. Unfortunately, it gives and error when trying to import it in Python, and says libgomp.so.1: cannot open shared object file at the beginning, so I figured out I need to do apt-get install libgomp1 and maybe something more.

How am I supposed to run these commands?

I assume it is better to use Layers, or something similar because running these commands every time Lambda starts doesn't make sense.

But how do I do sudo apt-get in a particular folder, from what I know that is not possible.

So my questions boil down to - how to run these various bash commands, and install packages, like when you do in Dockerfile, but for Zip file in Lambda.

I am using AWS SAM for deployment and development.

You can run lambda functions from your own Docker images, where you have almost full control over what the image contains.

Here's a simple example of a Python application executed in a container: 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 + '!'

I use SAM and a Docker image to run a part of a large Java application as a lambda function. Here's what my CF template looks like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM