I'm attempting to get a Lambda function working with AWS CDK. I'm implemented the lambda function in python, and want to include external libraries in my Lambda code. Currently this is my CDK code:
import aws_cdk as core
from aws_cdk import (
Stack,
aws_lambda as _lambda,
aws_apigateway as apigw,
)
class SportsTeamGeneratorStack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
my_lambda = _lambda.Function(self, 'HelloHandler',
runtime=_lambda.Runtime.PYTHON_3_8,
code=_lambda.Code.from_asset("lambda",
bundling= core.BundlingOptions(
image=_lambda.Runtime.PYTHON_3_8.bundling_image,
command=[
"bash", "-c",
"pip install --no-cache -r requirements.txt -t /asset-output && cp -au . /asset-output"
],
),
),
handler='hello.handler',
)
apigw.LambdaRestApi(
self, 'Endpoint',
handler=my_lambda,
)
And this is my Lambda code:
import json
import pandas
def handler(event, context):
print('request: {}'.format(json.dumps(event)))
return {
'statusCode': 200,
'headers': {
'Content-Type': 'text/plain'
},
'body': 'Hello, CDK! You have hit {}\n'.format(event['path'])
}
The cdk code is in a directory called sports_team_generator, and the lambda code is in a hello.py file located in a directory called "lambda". Within the "lambda" directory, I also have my requirements.txt file, which contains the follwing:
aws-cdk-lib==2.19.0
constructs>=10.0.0,<11.0.0
pytz==2022.1
requests==2.27.1
sportsipy==0.6.0
numpy==1.22.3
pandas==1.4.2
pyquery >= 1.4.0
I am currently trying to avoid using ECR to upload docker images and then link those images to lambda functions in the console, as I want to do everything through the CDK. I feel as though the lambda itself is small, and have no clue why it might be exceeding the size requirement. It seems as though the requirements.txt is causing the problem, and I'm not sure if there is some workaround to this. Preferably I would fix this error, although if not possible I could be open to creating a docker image and uploading to ECR, and linking that ECR instance to a lambda function through the cdk if possible. If anyone has a solution/suggestions please let me know.
CodePudding user response:
I'm afraid your only option is deploying the lambda from the container, see https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html
Max unzipped deployment package size from zip is 250 MB. Lambda layers also do not help as the combined deployment package size of all layers needs to be lower than 250 MB.
However, container image code package size limit is 10 GB. Therefore if you can not lower down package size of your lambda under 250 MB, containers are the way to go for you. The guide how to register the container and use it for the lambda deploy is at https://docs.aws.amazon.com/lambda/latest/dg/images-create.html and https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/