From ffd46fc8a37a4fa656a4cf0e79238c4e6be2b6a6 Mon Sep 17 00:00:00 2001 From: Leo Thomas Date: Wed, 23 Dec 2020 14:35:16 -0800 Subject: [PATCH] refactored lambda pacakge generation to use docker --- Dockerfiles/lambda/Dockerfile | 10 +++++----- package.json | 1 + setup.py | 19 ++++++++++--------- stack/app.py | 33 ++++++++++++++------------------- 4 files changed, 30 insertions(+), 33 deletions(-) create mode 100644 package.json diff --git a/Dockerfiles/lambda/Dockerfile b/Dockerfiles/lambda/Dockerfile index b8358fc..fec40c5 100644 --- a/Dockerfiles/lambda/Dockerfile +++ b/Dockerfiles/lambda/Dockerfile @@ -1,14 +1,15 @@ FROM lambci/lambda:build-python3.7 -WORKDIR /tmp # We install covid_api and mangum +WORKDIR /app COPY README.md /app/README.md COPY covid_api/ /app/covid_api/ COPY setup.py /app/setup.py -RUN pip install /app/. "mangum>=0.9.0" -t /var/task --no-binary numpy +RUN pip install --upgrade pip +RUN pip install . "mangum>=0.9.0" -t /var/task --no-binary numpy, pydantic # Reduce package size and remove useless files RUN cd /var/task && find . -type f -name '*.pyc' | while read f; do n=$(echo $f | sed 's/__pycache__\///' | sed 's/.cpython-[2-3][0-9]//'); cp $f $n; done; @@ -18,7 +19,6 @@ RUN find /var/task -type d -a -name 'tests' -print0 | xargs -0 rm -rf RUN rm -rdf /var/task/numpy/doc/ RUN rm -rdf /var/task/stack -RUN cd /var/task && zip -r9q /tmp/package.zip * -COPY lambda/handler.py handler.py -RUN zip -r9q /tmp/package.zip handler.py \ No newline at end of file +COPY lambda/handler.py /var/task/handler.py + diff --git a/package.json b/package.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/package.json @@ -0,0 +1 @@ +{} diff --git a/setup.py b/setup.py index c875435..12637f9 100644 --- a/setup.py +++ b/setup.py @@ -25,15 +25,16 @@ "deploy": [ "docker", "attrs", - "aws-cdk.core==1.72.0", - "aws-cdk.aws_lambda==1.72.0", - "aws-cdk.aws_apigatewayv2==1.72.0", - "aws-cdk.aws_ecs==1.72.0", - "aws-cdk.aws_ec2==1.72.0", - "aws-cdk.aws_autoscaling==1.72.0", - "aws-cdk.aws_ecs_patterns==1.72.0", - "aws-cdk.aws_iam==1.72.0", - "aws-cdk.aws_elasticache==1.72.0", + "aws-cdk.core>=1.72.0", + "aws-cdk.aws_lambda>=1.72.0", + "aws-cdk.aws_apigatewayv2>=1.72.0", + "aws-cdk.aws_apigatewayv2_integrations>=1.72.0", + "aws-cdk.aws_ecs>=1.72.0", + "aws-cdk.aws_ec2>=1.72.0", + "aws-cdk.aws_autoscaling>=1.72.0", + "aws-cdk.aws_ecs_patterns>=1.72.0", + "aws-cdk.aws_iam>=1.72.0", + "aws-cdk.aws_elasticache>=1.72.0", ], "test": ["moto[iam]", "mock", "pytest", "pytest-cov", "pytest-asyncio", "requests"], } diff --git a/stack/app.py b/stack/app.py index 9581c36..3c4a1b7 100644 --- a/stack/app.py +++ b/stack/app.py @@ -5,6 +5,7 @@ import config from aws_cdk import aws_apigatewayv2 as apigw +from aws_cdk import aws_apigatewayv2_integrations as apigw_integrations from aws_cdk import aws_ec2 as ec2 from aws_cdk import aws_ecs as ecs from aws_cdk import aws_ecs_patterns as ecs_patterns @@ -117,30 +118,24 @@ def __init__( apigw.HttpApi( self, f"{id}-endpoint", - default_integration=apigw.LambdaProxyIntegration(handler=lambda_function), + default_integration=apigw_integrations.LambdaProxyIntegration( + handler=lambda_function + ), ) def create_package(self, code_dir: str) -> aws_lambda.Code: """Build docker image and create package.""" - # print('building lambda package via docker') - # print(f'code dir: {code_dir}') - # client = docker.from_env() - # print('docker client up') - # client.images.build( - # path=code_dir, - # dockerfile="Dockerfiles/lambda/Dockerfile", - # tag="lambda:latest", - # ) - # print('docker image built') - # client.containers.run( - # image="lambda:latest", - # command="/bin/sh -c 'cp /tmp/package.zip /local/package.zip'", - # remove=True, - # volumes={os.path.abspath(code_dir): {"bind": "/local/", "mode": "rw"}}, - # user=0, - # ) - return aws_lambda.Code.asset(os.path.join(code_dir, "package.zip")) + return aws_lambda.Code.from_asset( + path=os.path.abspath(code_dir), + bundling=core.BundlingOptions( + image=core.BundlingDockerImage.from_asset( + path=os.path.abspath(code_dir), + file="Dockerfiles/lambda/Dockerfile", + ), + command=["bash", "-c", "cp -R /var/task/. /asset-output/."], + ), + ) class covidApiECSStack(core.Stack):