Skip to content

Commit

Permalink
fix: make lambda layer
Browse files Browse the repository at this point in the history
  • Loading branch information
billmetangmo committed Aug 30, 2024
1 parent 84b3d2a commit 2b8d48a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 45 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/deploy_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,17 @@ jobs:
- name: Build python lambda layer
id: build
run: |
cd make_lamnda_layer
cd infra/make_lamda_layer
chmod +x runner.sh
./runner.sh
- name: Terraform Plan
id: plan
run: |
cd infra
tfcmt plan -- terraform plan -no-color
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
Expand Down
2 changes: 1 addition & 1 deletion infra/api/test_liveness_mtchoun-mouh.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setup_method(self, method):
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.driver = webdriver.Chrome(options=chrome_options)
self.vars = {}

def teardown_method(self, method):
Expand Down
44 changes: 1 addition & 43 deletions infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -140,49 +140,7 @@ resource "aws_lambda_permission" "apigw_lambda" {
# More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html
source_arn = "arn:aws:execute-api:${var.region}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.api.id}/*/${aws_api_gateway_method.method.http_method}${aws_api_gateway_resource.resource.path}"
}
# ======================================> HERE

# # create zip file from requirements.txt. Triggers only when the file is updated
# resource "null_resource" "lambda_layer" {
# triggers = {
# requirements = filesha1(local.requirements_path)
# }
# # the command to install python and dependencies to the machine and zips
# provisioner "local-exec" {
# command = <<EOT
# set -e
# apt-get update
# apt install python3 python3-pip zip -y
# rm -rf python
# mkdir python
# pip3 install -r ${local.requirements_path} -t python/
# zip -r ${local.layer_zip_path} python/
# EOT
# }
# }


# # create zip file from requirements.txt. Triggers only when the file is updated
resource "null_resource" "lambda_layer" {
# triggers = {
# requirements = filesha1(local.requirements_path)
# }
# the command to install python and dependencies to the machine and zips
provisioner "local-exec" {
command = <<EOT
/bin/bash
pip install virtualenv &&\
virtualenv --python=/usr/bin/python3.8 python &&\
. python/bin/activate &&\
pip install -r api/requirements.txt -t python/lib/python3.8/site-packages &&\
ls python &&\
wget https://mongulu-files.s3.eu-central-1.amazonaws.com/7zz && chmod u+x 7zz &&\
./7zz a python.zip python/ &&\
./7zz l python.zip
EOT
}
}
# Archive a single file.

# data "archive_file" "test_zip" {
Expand All @@ -193,7 +151,7 @@ resource "null_resource" "lambda_layer" {
# }

resource "aws_lambda_layer_version" "test_lambda_layer" {
filename = "python.zip"
filename = "make_lamda_layer/python.zip"
layer_name = "test_lambda_layer"
compatible_runtimes = ["python3.8", "python3.7"]
}
Expand Down
10 changes: 10 additions & 0 deletions infra/make_lamda_layer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM amazonlinux:2018.03
RUN ulimit -n 1024 && yum -y update && yum -y install \
python38 \
python38-pip \
python38-devel \
zip \
&& yum clean all

RUN python3 -m pip install pip==22.1.2
RUN python3 -m pip install virtualenv==20.14.1
6 changes: 6 additions & 0 deletions infra/make_lamda_layer/docker_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -ex
virtualenv --python=/usr/bin/python3.8 python
source /python/bin/activate
pip install -r requirements.txt -t /python/lib/python3.8/site-packages
zip -r9 python.zip python
1 change: 1 addition & 0 deletions infra/make_lamda_layer/requirements.txt
14 changes: 14 additions & 0 deletions infra/make_lamda_layer/runner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -ex
container_name=lambda_docker
docker_image=aws_lambda_builder_image

docker build --tag $docker_image .

docker run -td --name=$container_name $docker_image
docker cp ../api/requirements.txt $container_name:/requirements.txt

docker exec -i $container_name /bin/bash < ./docker_install.sh
docker cp $container_name:/python.zip python.zip
docker stop $container_name
docker rm $container_name

0 comments on commit 2b8d48a

Please sign in to comment.