Skip to content

A minimal example of setting up custom Docker container on Lambda to echo the API request.

License

Notifications You must be signed in to change notification settings

amitness/lambda-docker-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Steps

  1. Build the docker image with some tag.
docker build -t ping .
  1. 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())

image

  1. 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
  1. Install serverless and deploy lambda function to AWS.
serverless deploy
  1. 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())

About

A minimal example of setting up custom Docker container on Lambda to echo the API request.

Resources

License

Stars

Watchers

Forks