|
5 | 5 | Here we have an example how to deploy "hello-world" AWS Lambda with Hamilton functions.
|
6 | 6 | This example is based on the official instruction: https://docs.aws.amazon.com/lambda/latest/dg/python-image.html#python-image-instructions
|
7 | 7 |
|
8 |
| -0. Set up AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-quickstart.html |
| 8 | +## Prerequisites |
9 | 9 |
|
10 |
| -1. Docker image build: |
| 10 | +- **AWS CLI Setup**: Make sure the AWS CLI is set up on your machine. If you haven't done this yet, no worries! You can follow the [Quick Start guide](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-quickstart.html) for easy setup instructions. |
11 | 11 |
|
12 |
| -```shell |
13 |
| -docker build --platform linux/amd64 -t aws-lambda-hamilton . |
14 |
| -``` |
| 12 | +## Step-by-Step Guide |
15 | 13 |
|
16 |
| -2. Local tests: |
| 14 | +### 1. Build Docker image: |
17 | 15 |
|
18 |
| -```shell |
19 |
| -docker run -p 9000:8080 aws-lambda-hamilton |
20 |
| -``` |
| 16 | +- **Build Docker image for deploy in AWS ECR** |
21 | 17 |
|
22 |
| -```shell |
23 |
| -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]]}}' |
24 |
| -``` |
| 18 | + ```shell |
| 19 | + docker build --platform linux/amd64 -t aws-lambda-hamilton . |
| 20 | + ``` |
25 | 21 |
|
26 |
| -3. Create AWS ECR repository: |
| 22 | +- **Local tests:** |
27 | 23 |
|
| 24 | + Run Docker container: |
28 | 25 |
|
29 |
| -```shell |
30 |
| -aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 111122223333.dkr.ecr.us-east-1.amazonaws.com |
31 |
| -``` |
| 26 | + ```shell |
| 27 | + docker run -p 9000:8080 aws-lambda-hamilton |
| 28 | + ``` |
32 | 29 |
|
33 |
| -```shell |
34 |
| -aws ecr create-repository --repository-name aws-lambda-hamilton --region us-east-1 --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE |
35 |
| -``` |
| 30 | + Send test request to check if Docker container executes it correctly: |
36 | 31 |
|
37 |
| -4. Deploy image to AWS ECR: |
| 32 | + ```shell |
| 33 | + 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]]}}' |
| 34 | + ``` |
38 | 35 |
|
39 |
| -```shell |
40 |
| -docker tag aws-lambda-hamilton 111122223333.dkr.ecr.us-east-1.amazonaws.com/aws-lambda-hamilton:latest |
41 |
| -``` |
| 36 | +### 2. Create AWS ECR repository: |
| 37 | + |
| 38 | +Ensure the AWS account number (`111122223333`) is correctly replaced with yours: |
| 39 | + |
| 40 | +- **Authenticate Docker to Amazon ECR**: |
| 41 | + |
| 42 | + Retrieve an authentication token to authenticate your Docker client to your Amazon Elastic Container Registry (ECR): |
| 43 | + |
| 44 | + ```shell |
| 45 | + aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 111122223333.dkr.ecr.us-east-1.amazonaws.com |
| 46 | + ``` |
| 47 | + |
| 48 | +- **Create the ECR Repository**: |
| 49 | + |
| 50 | + ```shell |
| 51 | + aws ecr create-repository --repository-name aws-lambda-hamilton \ |
| 52 | + --region us-east-1 \ |
| 53 | + --image-scanning-configuration scanOnPush=true \ |
| 54 | + --image-tag-mutability MUTABLE |
| 55 | + ``` |
| 56 | + |
| 57 | +### 3. Deploy the Image to AWS ECR |
| 58 | + |
| 59 | +Ensure the AWS account number (`111122223333`) is correctly replaced with yours: |
42 | 60 |
|
43 | 61 | ```shell
|
| 62 | +docker tag aws-lambda-hamilton 111122223333.dkr.ecr.us-east-1.amazonaws.com/aws-lambda-hamilton:latest |
44 | 63 | docker push 111122223333.dkr.ecr.us-east-1.amazonaws.com/aws-lambda-hamilton:latest
|
45 | 64 | ```
|
46 | 65 |
|
47 |
| -4.5. Create simple AWS Lambda role (if needed): |
| 66 | +### 4. Create a simple AWS Lambda role: |
| 67 | + |
| 68 | +Example of creating an AWS Role for Lambda execution: |
48 | 69 |
|
49 | 70 | ```shell
|
50 |
| -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"}]}' |
| 71 | +aws iam create-role \ |
| 72 | + --role-name lambda-ex \ |
| 73 | + --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}' |
51 | 74 | ```
|
52 | 75 |
|
53 |
| -5. Create AWS Lambda |
| 76 | +### 5. Create AWS Lambda |
| 77 | + |
| 78 | +Ensure the AWS account number (`111122223333`) is correctly replaced with yours: |
54 | 79 |
|
55 | 80 | ```shell
|
56 | 81 | aws lambda create-function \
|
57 |
| - --function-name aws-lambda-hamilton \ |
58 |
| - --package-type Image \ |
59 |
| - --code ImageUri=111122223333.dkr.ecr.us-east-1.amazonaws.com/aws-lambda-hamilton:latest \ |
60 |
| - --role arn:aws:iam::111122223333:role/lambda-ex |
| 82 | + --function-name aws-lambda-hamilton \ |
| 83 | + --package-type Image \ |
| 84 | + --code ImageUri=111122223333.dkr.ecr.us-east-1.amazonaws.com/aws-lambda-hamilton:latest \ |
| 85 | + --role arn:aws:iam::111122223333:role/lambda-ex |
61 | 86 | ```
|
62 | 87 |
|
63 |
| -6. Test AWS Lambda |
| 88 | +### 6. Test AWS Lambda |
64 | 89 |
|
65 | 90 | ```shell
|
66 |
| -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 |
| 91 | +aws lambda invoke \ |
| 92 | + --function-name aws-lambda-hamilton \ |
| 93 | + --cli-binary-format raw-in-base64-out \ |
| 94 | + --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]]}}' \ |
| 95 | + response.json |
67 | 96 | ```
|
0 commit comments