Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sbstjn committed Nov 3, 2017
0 parents commit 67ad4b2
Show file tree
Hide file tree
Showing 8 changed files with 1,888 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
.serverless
data/*
node_modules
package-lock.json
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# MIT License

Copyright (c) 2017 Sebastian Müller <mail@sbstjn.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Serverless GeoIP

Use AWS Lambda and [MaxMind GeoLite](http://dev.maxmind.com/geoip/geoip2/geolite2/) to query for locations of IP addresses. You can invoke the function or use API Gateway to send an HTTP request with the IP address to lookup.

## Install

```bash
$ > git clone git@github.com:sbstjn/serverless-geoip.git
$ > cd serverless-geoip
$ > yarn install
```

## Configure

Download the [GeoLite2 City](http://dev.maxmind.com/geoip/geoip2/geolite2/) database and store the file inside the `data` folder.

## Deploy

```bash
$ > yarn deploy


endpoints:
GET - https://randomid.execute-api.us-east-1.amazonaws.com/dev/lookup
```

## Usage

### Invoke

```bash
$ > sls invoke -f lookup --data '{ "ip": "8.8.8.8" }'

{
"continent": {
"code": "NA",
"geoname_id": 6255149,
"names": {
"de": "Nordamerika",
"en": "North America",
"es": "Norteamérica",

```
### HTTP Request
```bash
$ > curl https://randomid.execute-api.us-east-1.amazonaws.com/dev/lookup?ip=8.8.8.8

{"continent":{"code":"NA","geoname_id":6255149,"names":{"de":"Nordamerika","en":"North America", …
```
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "serverless-geoip",
"version": "1.0.0",
"description": "Use MaxMind GeoLite2 database with AWS Lambda and serverless",
"main": "index.js",
"scripts": {
"deploy": "sls deploy"
},
"keywords": [
"serverless",
"geoip"
],
"author": "Sebastian Müller <mail@sbstjn.com>",
"license": "MIT",
"dependencies": {
"mmdb-reader": "^1.1.0"
},
"devDependencies": {
"serverless": "^1.24.0"
}
}
34 changes: 34 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
service: serverless-geoip

provider:
name: aws
runtime: nodejs6.10
memorySize: 128
timeout: 120
region: us-east-1

package:
exclude:
- .gitignore
- CODE_OF_CONDUCT.md
- dist/**
- LICENSE.md
- README.md
- yarn.lock
- package.json
- package-lock.json

functions:
lookup:
handler: src/lookup.run
timeout: 120
memorySize: 128

http:
handler: src/http.run
memorySize: 128
timeout: 120
events:
- http:
method: GET
path: /lookup
27 changes: 27 additions & 0 deletions src/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const Reader = require('mmdb-reader')
const lookup = require('./lookup')

const res = data => ({
statusCode: 200,
body: JSON.stringify(data)
})

const run = (event, ctx, callback) => {
console.log(ctx)

if (!event || !event.queryStringParameters || !event.queryStringParameters.ip) {
callback(
new Error('Invalid event.queryStringParameters.ip')
)
} else {
const req = { ip: event.queryStringParameters.ip }

lookup.run(
req,
ctx,
(error, data) => callback(null, res(data))
)
}
}

module.exports = { run }
18 changes: 18 additions & 0 deletions src/lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Reader = require('mmdb-reader')
const file = __dirname + '/../data/GeoLite2-City.mmdb'

const run = (event, ctx, callback) => {
console.log(ctx)

if (!event || !event.ip) {
callback(
new Error('Invalid event data, requires IP address')
)
} else {
Reader.open(file,
(error, reader) => callback(error, !error && reader.lookup(event.ip))
)
}
}

module.exports = { run }
Loading

0 comments on commit 67ad4b2

Please sign in to comment.