Skip to content

A bundle of serverless functions designed to allow user geolocation and actions based on that detection.

License

Notifications You must be signed in to change notification settings

alfonsogober/serverless-geolocation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serverless Geolocation

A bundle of serverless functions designed to allow user geolocation and actions based on that detection.

For more info, see Geo-fencing Users With Lambda @ Edge and CloudFront

Quick Start

Install

git clone [email protected]:alfonsogoberjr/serverless-geolocation.git
cd serverless-geolocation && npm install

Deploy

serverless deploy --stage dev

After a few minutes, you should see

Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (1.66 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
....................
Serverless: Stack update finished...
Service Information
service: geolocation
stage: dev
region: us-east-1
stack: geolocation-dev
api keys:
  dev-geolocation-apikey: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
endpoints:
  GET - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
functions:
  edgeRedirect: geolocation-dev-edgeRedirect
  countryLookup: geolocation-dev-countryLookup

Save the apikey and the GET URL somewhere; you’ll need it to access your lookup endpoint.

You should be able to curl your new endpoint now:

curl -H "X-Api-Key:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/

You should see

{"iso_code":"US","is_in_european_union":false}

Functions

edgeRedirect

Lambda @ Edge function that redirects users to countrycode.yourdomain.com

For example, a user in Shanghai will be redirected to cn.yourdomain.com, and a user in Amsterdam will be redirected to nl.yourdomain.com.

countryLookup

Standard Lambda-Proxy API-Gateway integration that performs a lookup using MaxMind's GeoIP data (open-source).

serverless invoke local -f countryLookup --data '{ "requestContext": { "identity": { "sourceIp": "185.104.185.86" } } }' --stage dev

License

Creative Commons Attribution-ShareAlike 4.0 International License

About

A bundle of serverless functions designed to allow user geolocation and actions based on that detection.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published