Skip to content

Commit

Permalink
Merge pull request #99 from NASA-IMPACT/bugfix/lambda-cdk-asset-gener…
Browse files Browse the repository at this point in the history
…ation

Refactor lambda package generation in CDK
  • Loading branch information
leothomas authored Jan 5, 2021
2 parents 0c94790 + ffd46fc commit 71ed733
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
10 changes: 5 additions & 5 deletions Dockerfiles/lambda/Dockerfile
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
COPY lambda/handler.py /var/task/handler.py

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
19 changes: 10 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
}
Expand Down
33 changes: 14 additions & 19 deletions stack/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 71ed733

Please sign in to comment.