A minimal implementation of a containerised C++ AWS lambda function. While AWS provides partial support for C++ in the form of base images, these images are very outdated making them insufficent for more complex function implementations. This repo provides a bare-bones starting point for a custom container, note that not all features have been tested.
AWS provides an emulator which helps test locally. First install the emulator...
mkdir -p ~/.aws-lambda-rie && curl -Lo ~/.aws-lambda-rie/aws-lambda-rie \
https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie \
&& chmod +x ~/.aws-lambda-rie/aws-lambda-rie
Then build and run your docker image with the emulator mounted...
docker build -t my-lambda .
docker run -d \
-v ~/.aws-lambda-rie:/aws-lambda \
-p 9000:8080 \
--entrypoint /aws-lambda/aws-lambda-rie \
my-lambda:latest \
/var/runtime/bootstrap
You may now test your function...
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d 'my payload'
Once satisifed your image is functioning correctly it can be deployed like any other containerised AWS lambda function. First create a new ECR repository (if you have not already done so), and push your new image...
aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
aws ecr create-repository --repository-name "${ECR_REPO}" --region "${REGION}" --no-cli-pager
docker tag ${ECR_REPO}:latest ${IMAGE_URI}
docker push ${IMAGE_URI}
Now create your lambda function referencing your new image...
aws lambda create-function \
--function-name "${FUNCTION_NAME}" \
--package-type Image \
--code ImageUri="${IMAGE_URI}" \
--role arn:aws:iam::${ACCOUNT_ID}:role/lambda-execution \
--region "${REGION}" \
--memory-size 2048 \
--no-cli-pager
Or, if it already exists, just update it to point to the new image...
aws lambda update-function-code \
--function-name "${FUNCTION_NAME}" \
--image-uri "${IMAGE_URI}" \
--region "${REGION}" \
--no-cli-pager