- Build the docker image with a tag.
docker build -t ping .
- Run and ensure it works
docker run -p 8080:8080 ping
Test using requests
import requests
import json
data = {"text": "This is a message."}
response = requests.post('http://localhost:8080/2015-03-31/functions/function/invocations', json={'body': json.dumps(data)})
print(response.json())
- Create an ECR repo and set the AWS Region and Account ID and push the docker image to ECR.
export AWS_REGION=us-east-2
export AWS_ACCOUNT_ID=********
export TAG=ping
export ECR_REPO_NAME=ping
aws ecr create-repository --repository-name $ECR_REPO_NAME
aws ecr get-login-password \
--region "$AWS_REGION" \
| docker login \
--username AWS \
--password-stdin "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com"
docker tag $TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME
- Install serverless and deploy lambda function to AWS.
serverless deploy
- Try the API by calling endpoint given by serverless framework.
import requests
data = {"text": "This is a message."}
response = requests.post('https://******.execute-api.us-east-2.amazonaws.com/dev/ping', json=data)
print(response.json())