Skip to content

Instantly share code, notes, and snippets.

@manasmbellani
Last active December 6, 2023 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save manasmbellani/b66642343058064c13044e4fe31dfad5 to your computer and use it in GitHub Desktop.
Save manasmbellani/b66642343058064c13044e4fe31dfad5 to your computer and use it in GitHub Desktop.
aws-lambda-layers: Example code for the aws lambda layer
Please review the `README.md` file.

aws-lambda-layers

An example of using AWS Lambda Layers

Usage

Locally (via Docker)

docker build -t aws-lambda-layers:latest .; docker run --rm -p 9000:8080 aws-lambda-layers:latest
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

On AWS

Follow the steps above first to build container image locally

Deploy the container image to AWS Elastic Container Registry

account_id=$(aws sts get-caller-identity --query "Account" --output text)
password=$(aws ecr get-login-password --region ap-southeast-2)
docker login -u AWS -p "$password" $account_id.dkr.ecr.ap-southeast-2.amazonaws.com
aws ecr create-repository --repository-name aws-lambda-layers --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE
docker tag aws-lambda-layers:latest $account_id.dkr.ecr.ap-southeast-2.amazonaws.com/aws-lambda-layers:latest
docker push $account_id.dkr.ecr.ap-southeast-2.amazonaws.com/aws-lambda-layers:latest

Then create a new Lambda Function from the container image loaded to ECR above via AWS console. Use x86_64 type lambda, if possible.

#!/usr/bin/env python3
import shlex
import subprocess
import sys
def handler(event, context):
o = subprocess.check_output(shlex.split("ls -la ."))
subprocess.check_output(shlex.split("cp hakcertstream /tmp/hakcertstream"))
subprocess.check_output(shlex.split("/bin/chmod 777 /tmp/hakcertstream"))
p = subprocess.Popen(shlex.split('/tmp/hakcertstream'), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
i = 0
o = ''
while True:
l = p.stdout.readline()
i += 1
o += l.decode() + "\n"
if i > 11:
break
return o
FROM public.ecr.aws/lambda/python:3.8
# Copy function code
COPY app.py ${LAMBDA_TASK_ROOT}
# Install the function's dependencies using file requirements.txt
# from your project folder.
COPY hakcertstream .
COPY requirements.txt .
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.handler" ]
requests
awslambdaric
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment