aws lambda build by docker when oversize

必須なファイルをローカルで用意する

  1. main.py
  2. Docker
  3. requirements.txt

・main.pyにはLambdaのcodeを入れる

import sys
sys.path.append('/opt/site-packages')
from ldap3 import Server, Connection, ALL, MODIFY_ADD ,Tls
import os


def lambda_handler(event, context):

    
    try:
        today_date = (datetime.datetime.now() - datetime.timedelta(days=0)).strftime('%Y-%m-%d')
        print(today_date)

        df = main()

        send_message_to_teams(f'denodo_user_role_list_{today_date}','success',webhook_link_success)
        
        return {
            'statusCode': 200,
            'body': json.dumps('denodo_user_role_list export complete')
        }
    
    except Exception as e:
        print(f"Error in lambda_handler: {str(e)}")
        import traceback
        print(traceback.format_exc())
        send_message_to_teams(f'denodo_user_role_list_{today_date}','failed',webhook_link_failed)

        return {
            'statusCode': 500,
            'body': json.dumps('Failed to complete denodo_user_role_list export')
        }
Python

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

# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}

# Install the specified packages
RUN pip install -r requirements.txt

# Copy function code
COPY main.py ${LAMBDA_TASK_ROOT}
COPY permission_key.py ${LAMBDA_TASK_ROOT}

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

AWS ECRを新規作成

・作成できたら、プッシュコマンドを表示をクリックして、コマンドをメモする

aws cloud shellで、VENV環境を作成し、ファイルをUPLOAD

・以下のファイルを、venv環境にUPLOAD

  1. main.py
  2. Docker
  3. requirements.txt

・ファイルの直下で、プッシュコマンドを実行

Lamdaを新規作成

・create from imageを選択して、ECRのURLを設定

・lambdaを作成した後に、VPCを作成することも可能

・lambda関数をpreviewすることができないので、できるかぎりlocalでcodeをしっかり管理する

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注