├── .gitignore ├── Dockerfile ├── README.md ├── build.sh ├── example ├── .gitignore ├── handler.py └── serverless.yml ├── package.json └── serverless.yml /.gitignore: -------------------------------------------------------------------------------- 1 | .serverless/ 2 | layer 3 | node_modules/ 4 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM lambci/lambda:build-python3.6 2 | 3 | ENV LIBMAXMINDDB_VERSION=1.3.2 4 | 5 | # Compilation work for libmaxminddb 6 | RUN mkdir -p "/tmp/libmaxminddb-${LIBMAXMINDDB_VERSION}-build" 7 | WORKDIR "/tmp/libmaxminddb-${LIBMAXMINDDB_VERSION}-build" 8 | RUN curl -L -o "libmaxminddb-${LIBMAXMINDDB_VERSION}.tar.gz" "https://github.com/maxmind/libmaxminddb/releases/download/${LIBMAXMINDDB_VERSION}/libmaxminddb-${LIBMAXMINDDB_VERSION}.tar.gz" 9 | RUN tar xf "libmaxminddb-${LIBMAXMINDDB_VERSION}.tar.gz" 10 | WORKDIR "/tmp/libmaxminddb-${LIBMAXMINDDB_VERSION}-build/libmaxminddb-${LIBMAXMINDDB_VERSION}" 11 | RUN ./configure --prefix=/opt/ 12 | RUN make -j 8 13 | RUN make install 14 | RUN ldconfig 15 | 16 | # Compilation work for python maxminddb & geoip2 libraries 17 | RUN pip install \ 18 | --target=/opt/python/ \ 19 | --global-option=build_ext --global-option="-L/var/lang/lib:/opt/lib" \ 20 | --global-option=build_ext --global-option="-I/var/lang/include/python3.6m:/opt/include" \ 21 | maxminddb 22 | RUN pip install --target=/opt/python/ geoip2 23 | 24 | # Download the DBs! 25 | RUN mkdir /opt/maxminddb 26 | WORKDIR /opt/maxminddb 27 | RUN curl http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz | tar xz 28 | RUN curl http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz | tar xz 29 | RUN curl http://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN.tar.gz | tar xz 30 | RUN mv */*.mmdb . && rm -r GeoLite2-{ASN,City,Country}_*/ 31 | 32 | # set workdir back 33 | WORKDIR /var/task 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GeoIP Lambda Layer 2 | This is the source project for an AWS Lambda Layer that contains [MaxMind](https://maxmind.com)'s 3 | free GeoLite2 geo IP databases and the C library for reading them and python3.6 build of the 4 | library against it. 5 | 6 | ## Deploy 7 | ``` 8 | sls deploy 9 | ``` 10 | 11 | ## DB path when Lambda is executing 12 | The GeoLite2 libraries can be found at the following paths in your running Lambda: 13 | * `/opt/maxminddb/GeoLite2-City.mmdb` 14 | * `/opt/maxminddb/GeoLite2-Country.mmdb` 15 | * `/opt/maxminddb/GeoLite2-ASN.mmdb` 16 | 17 | ## How to use 18 | Check out [the example](https://github.com/serverless/geoip-lambda-layer/tree/master/example) 19 | for how to use this layer with the [Serverless Framework](https://serverless.com). 20 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | set -e 4 | 5 | rm -rf layer 6 | docker build -t geoip-lambda-layer . 7 | CONTAINER=$(docker run -d geoip-lambda-layer false) 8 | docker cp $CONTAINER:/opt layer 9 | docker rm $CONTAINER 10 | touch layer/.slsignore 11 | cat > layer/.slsignore << EOF 12 | **/*.a 13 | **/*.la 14 | share/** 15 | include/** 16 | bin/** 17 | EOF 18 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # Distribution / packaging 2 | .Python 3 | env/ 4 | build/ 5 | develop-eggs/ 6 | dist/ 7 | downloads/ 8 | eggs/ 9 | .eggs/ 10 | lib/ 11 | lib64/ 12 | parts/ 13 | sdist/ 14 | var/ 15 | *.egg-info/ 16 | .installed.cfg 17 | *.egg 18 | 19 | # Serverless directories 20 | .serverless -------------------------------------------------------------------------------- /example/handler.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | import geoip2.database 4 | 5 | 6 | def geoip(event, context): 7 | reader = geoip2.database.Reader("/opt/maxminddb/GeoLite2-City.mmdb") 8 | response = reader.city(event["requestContext"]["identity"]["sourceIp"]) 9 | return { 10 | "body": json.dumps( 11 | { 12 | "city": response.city.name, 13 | "state": response.subdivisions.most_specific.name, 14 | "country": response.country.name, 15 | } 16 | ) 17 | } 18 | -------------------------------------------------------------------------------- /example/serverless.yml: -------------------------------------------------------------------------------- 1 | service: geoip 2 | frameworkVersion: ">=1.34.0 <2.0.0" 3 | 4 | provider: 5 | name: aws 6 | runtime: python3.6 7 | 8 | functions: 9 | get-location: 10 | handler: handler.geoip 11 | layers: 12 | # Update this ARN after you've deployed the geoip layer 13 | - arn:aws:lambda:${self:provider.region, 'us-east-1'}:377024778620:layer:geoip:2 14 | events: 15 | - http: 16 | path: geoip 17 | method: get 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aws-lambda-layer", 3 | "description": "", 4 | "version": "0.1.0", 5 | "dependencies": {}, 6 | "devDependencies": {} 7 | } 8 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: geoip-lambda-layer 2 | frameworkVersion: ">=1.34.0 <2.0.0" 3 | 4 | provider: 5 | name: aws 6 | region: us-east-1 7 | 8 | layers: 9 | geoip: 10 | path: layer 11 | --------------------------------------------------------------------------------