{"id":23748028,"url":"https://github.com/indiependente/aws-lambda-container","last_synced_at":"2025-09-04T22:31:09.000Z","repository":{"id":39993066,"uuid":"317875254","full_name":"indiependente/aws-lambda-container","owner":"indiependente","description":"Example Go AWS lambda packaged in a Docker container","archived":false,"fork":false,"pushed_at":"2023-05-05T15:58:49.000Z","size":35,"stargazers_count":9,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-06-21T18:53:34.375Z","etag":null,"topics":["aws","distroless","docker","dockerfile","go","lambda"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/indiependente.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-02T13:44:49.000Z","updated_at":"2023-05-08T09:47:14.000Z","dependencies_parsed_at":"2024-06-21T17:46:36.065Z","dependency_job_id":null,"html_url":"https://github.com/indiependente/aws-lambda-container","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indiependente%2Faws-lambda-container","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indiependente%2Faws-lambda-container/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indiependente%2Faws-lambda-container/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indiependente%2Faws-lambda-container/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/indiependente","download_url":"https://codeload.github.com/indiependente/aws-lambda-container/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231999624,"owners_count":18458180,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","distroless","docker","dockerfile","go","lambda"],"created_at":"2024-12-31T14:58:52.177Z","updated_at":"2024-12-31T14:58:52.594Z","avatar_url":"https://github.com/indiependente.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ƛ📦 aws-lambda-container\n\nExample Go AWS lambda packaged in a Docker container\n\n\n## Description\n\nThis repo contains an example Go application that can run as a Dockerkised lambda both on AWS and locally.\n\n### Why not using the aws base image?\n\nBecause it's huge! \n\nThe official image (public.ecr.aws./lambda/go) provided by AWS ships a whole Linux distro and weights about 670MB, which is definitely too much.\n\n### Distroless image\n\nThis container uses the images provided by the Google Container Tools here: https://github.com/GoogleContainerTools/distroless\n\nIn particular it uses the gcr.io/distroless/static image which is ideal for statically compiled languages like Go.\n\nThe resulting image weights about 10MB.\n\n\n## Local execution\n\nSo the whole reason for this being interesting is that it allows developers to improved their feedback loop when working with lambdas, without having to use external tools like SAM.\n\n**But**, in order to use a custom image, you need to either bake into the image the `aws-lambda-runtime-interface-emulator` or install it on the host machine and point the Docker entrypoint to that executable.\n\nMy approach here is to bake it into the local test image, following these steps:\n - build the app and copy it into a distroless image (multi-stage docker build)\n   - this is the lambda image that can be pushed to ECR\n - build a test image that uses the lambda image as a base layer and uses the `aws-lambda-rie` as entrypoint. \n\n\n## How can I test this lambda locally?\n1. Package the application:\n\n```\nmake lambda\n```\n\nThis will build the docker image and tag it as `fastfib:latest`\n\n2. Package the test image that will be executed locally\n\n```\nmake testlambda\n```\n\nThis will build the docker image and tag it as `testfastfib:latest`\n\n3. Run the lambda locally mapping port 8080 of the container to 9000 on the host machine\n\n```\ndocker run -p 9000:8080 testfastfib:latest\n```\n\n4. Send a request to the lambda\n\nThe code itself implements a fast fibonacci sequence algorithm based on https://www.nayuki.io/page/fast-fibonacci-algorithms\n\nSo the lambda will reply with the n-th element of the Fibonacci sequence in JSON content encoding.\n\nRequest:\n```\ncurl -XPOST \"http://localhost:9000/2015-03-31/functions/function/invocations\" -d '{\"n\":7}'\n```\nResponse:\n```\n{\"result\":13}\n```\n\n## API Gateway Proxy Request/Response\n\nUsually lambdas are sitting behind an API gateway or a load balancer, for that reason the code has to use the right AWS event structure.\n\nIn this example I've added an API gawaway handler. It can be used by passing the `--apigw` flag when running the binary.\n\nI've added a `CMD` in the [Dockerfile.test](./Dockerfile.test) file, that can be commented out to test this type of event.\n\nIn order to test the API gateway handler, we need to send an API gateway JSON event.\nI've added an example one called [apigw_request.json](./apigw_request.json) to this repo, that can be used to test locally.\n\nRequest:\n```\ncurl -i -X POST localhost:9000/2015-03-31/functions/function/invocations \\\n  -H \"Content-Type: application/json\" \\\n  --data-binary \"@apigw_request.json\"\n```\n\nResponse:\n```\nHTTP/1.1 200 OK\nDate: Wed, 02 Dec 2020 18:44:33 GMT\nContent-Length: 115\nContent-Type: text/plain; charset=utf-8\n\n{\"statusCode\":200,\"headers\":{\"Content-Type\":\"application/json\"},\"multiValueHeaders\":null,\"body\":\"{\\\"result\\\":233}\"}\n```\n\n\n## TODO\n- [x] ~implement an API Gateway Proxy request/response handler~\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Findiependente%2Faws-lambda-container","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Findiependente%2Faws-lambda-container","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Findiependente%2Faws-lambda-container/lists"}