AWS Lambda - serverless computation service in AWS.
Here we have an example how to deploy “hello-world” AWS Lambda with Hamilton functions. This example is based on the official instruction: https://docs.aws.amazon.com/lambda/latest/dg/python-image.html#python-image-instructions
Build Docker image for deploy in AWS ECR
docker build --platform linux/amd64 -t aws-lambda-hamilton .
Local tests:
Run Docker container:
docker run -p 9000:8080 aws-lambda-hamilton
Send test request to check if Docker container executes it correctly:
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"body": {"columns":["signups","spend"],"index":[0,1,2,3,4,5],"data":[[1,10],[10,10],[50,20],[100,40],[200,40],[400,50]]}}'
Ensure the AWS account number (111122223333) is correctly replaced with yours:
Authenticate Docker to Amazon ECR:
Retrieve an authentication token to authenticate your Docker client to your Amazon Elastic Container Registry (ECR):
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 111122223333.dkr.ecr.us-east-1.amazonaws.com
Create the ECR Repository:
aws ecr create-repository --repository-name aws-lambda-hamilton \ --region us-east-1 \ --image-scanning-configuration scanOnPush=true \ --image-tag-mutability MUTABLE
Ensure the AWS account number (111122223333) is correctly replaced with yours:
docker tag aws-lambda-hamilton 111122223333.dkr.ecr.us-east-1.amazonaws.com/aws-lambda-hamilton:latest docker push 111122223333.dkr.ecr.us-east-1.amazonaws.com/aws-lambda-hamilton:latest
Example of creating an AWS Role for Lambda execution:
aws iam create-role \ --role-name lambda-ex \ --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}'
Ensure the AWS account number (111122223333) is correctly replaced with yours:
aws lambda create-function \ --function-name aws-lambda-hamilton \ --package-type Image \ --code ImageUri=111122223333.dkr.ecr.us-east-1.amazonaws.com/aws-lambda-hamilton:latest \ --role arn:aws:iam::111122223333:role/lambda-ex
aws lambda invoke \ --function-name aws-lambda-hamilton \ --cli-binary-format raw-in-base64-out \ --payload '{"body": {"columns":["signups","spend"],"index":[0,1,2,3,4,5],"data":[[1,10],[10,10],[50,20],[100,40],[200,40],[400,50]]}}' \ response.json