- Build the docker image with some tag.
docker build -t ping .
- Run the docker container and ensure it works.
docker run -p 8080:8080 ping
Test the function locally by using requests on the local endpoint.
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())
- Set the AWS region, Account ID and other credentials as environment variables. Run the commands.
export AWS_REGION=us-east-2
export AWS_ACCOUNT_ID=********
export TAG=ping
export ECR_REPO_NAME=ping
# Create ECR repository
aws ecr create-repository --repository-name $ECR_REPO_NAME
# Login to ECR using Docker
aws ecr get-login-password \
--region "$AWS_REGION" \
| docker login \
--username AWS \
--password-stdin "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com"
# Tag local image to ECR endpoint
docker tag $TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME
# Push the docker image to ECR
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())