Skip to main content

An AWS CDK construct to create a CloudFront-powered HTTP endpoint delivering requestor's geolocation details.

Project description

CloudfrontGeoLocator

GitHub npm version Maven Central NuGet latest version PyPi version Go Reference

The CloudfrontGeoLocator is an AWS CDK construct that automates the setup of a CloudFront distribution with an Origin Request Lambda function, enabling applications to determine the geolocation of their users based on incoming HTTP requests. It's an ideal solution for developers looking to enhance their cloud applications with geolocation awareness without delving into the complexities of AWS configurations.

Table of Contents

Installation

To install CloudfrontGeoLocator construct library using npm, run the following command:

npm i cdk-cloudfront-geo-locator

Usage

To initialize the CloudfrontGeoLocator construct you can use the following code:

import cdk = require('aws-cdk-lib');
import { Construct } from 'constructs';
import { CloudfrontGeoLocator } from 'cdk-cloudfront-geo-locator';

// stack initialization with default props
const geoLocator = new CloudfrontGeoLocator(this, 'GeoLocator');

// stack initialization with custom props
const geoLocatorWithProps = new CloudfrontGeoLocator(
  this,
  'GeoLocatorWithProps',
  {
    s3BucketName: 'cloudfront-origin-bucket-name', // optional
    lambdaFunctionName: 'cloudfront-origin-request-edge-lambda-function-name', // optional
    cloudfrontCachePolicyName: 'cloudfront-cache-policy-name', // optional
    cloudfrontOriginRequestPolicyName: 'cloudfront-origin-request-policy-name', // optional
    cloudfrontPriceClass: cloudfront.PriceClass.PRICE_CLASS_100, // optional
  }
);

// stack initialization with custom domain
const customDomainGeoLocator = new CloudfrontGeoLocator(
  this,
  'CustomDomainGeoLocator',
  {
    customDomainName: 'example.com',
    customDomainCertificateArn:
      'arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012',
  }
);

// exported properties
console.log(geoLocator.s3BucketArn); // The ARN of the S3 bucket.
console.log(geoLocator.lambdaFunctionArn); // The ARN of the Lambda function.
console.log(geoLocator.lambdaFunctionVersion); // The current version of the Lambda function.
console.log(geoLocator.distributionId); // The ID of the CloudFront distribution.
console.log(geoLocator.distributionDomainName); // The domain name of the CloudFront distribution.
console.log(geoLocator.cloudfrontCachePolicyId); // The ID of the CloudFront cache policy.
console.log(geoLocator.cloudfrontOriginRequestPolicyId); // The ID of the CloudFront origin request policy.

Documentation

To initialize the CloudfrontGeoLocator construct you can use the following props:

/**
 * Properties for the CloudfrontGeoLocator construct.
 */
export interface CloudfrontGeoLocatorProps extends ResourceProps {
  /**
   * A unique name to identify the s3 bucket.
   *
   * @default - cloudfront-geo-locator-origin
   */
  readonly s3BucketName?: string;

  /**
   * A unique name to identify the lambda function.
   *
   * @default - cloudfront-geo-locator
   */
  readonly lambdaFunctionName?: string;

  /**
   * A unique name to identify the cloudfront cache policy.
   *
   * @default - CloudfrontGeoLocatorCachePolicy
   */
  readonly cloudfrontCachePolicyName?: string;

  /**
   * A unique name to identify the cloudfront origin request policy.
   *
   * @default - CloudfrontGeoLocatorOriginRequestPolicy
   */
  readonly cloudfrontOriginRequestPolicyName?: string;

  /**
   * The price class for the CloudFront distribution.
   *
   * @default - PRICE_CLASS_100
   */
  readonly cloudfrontPriceClass?: cloudfront.PriceClass;

  /**
   * The domain name for the CloudFront distribution.
   *
   * @default - undefined
   */
  readonly customDomainName?: string;

  /**
   * The ARN of the certificate.
   *
   * @default - undefined
   */
  readonly customDomainCertificateArn?: string;
}

Geolocation Endpoint Response

The geolocation endpoint returns a JSON object with the following properties:

{
  "country": "TR",
  "countryName": "Türkiye",
  "countryRegion": "35",
  "countryRegionName": "Izmir",
  "city": "Izmir"
}

Contributing

We welcome contributions! Please review code of conduct and contributing guide so that you can understand what actions will and will not be tolerated.

Pull Request Guidelines

  • The main branch is just a snapshot of the latest stable release. All development should be done in development branches. Do not submit PRs against the main branch.

  • Work in the src folder and DO NOT checkin dist in the commits.

  • It's OK to have multiple small commits as you work on the PR

  • If adding a new feature add accompanying test case.

  • If fixing bug,

    • Add accompanying test case if applicable.
    • Provide a detailed description of the bug in the PR.
    • If you are resolving an opened issue add issue number in your PR title.

License

CloudfrontGeoLocator is MIT licensed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk-cloudfront-geo-locator-1.0.0.tar.gz (126.0 kB view details)

Uploaded Source

Built Distribution

cdk_cloudfront_geo_locator-1.0.0-py3-none-any.whl (124.5 kB view details)

Uploaded Python 3

File details

Details for the file cdk-cloudfront-geo-locator-1.0.0.tar.gz.

File metadata

File hashes

Hashes for cdk-cloudfront-geo-locator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3df47d52f367df491d536c7b4a3af83082c90dd216c88b9f39c0ff87c123394f
MD5 20163b9c2b4174f5358c3a6ecd9568f1
BLAKE2b-256 a7f0522503e831c9f05ed7c96011096a9b13a3b15ff0ed1f1d2a19e44d3c7f7d

See more details on using hashes here.

File details

Details for the file cdk_cloudfront_geo_locator-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cdk_cloudfront_geo_locator-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e344485411a948694ea1f0d9203ad3d3ad51783b7738b31bafb22592224a31af
MD5 6d166e4efcc22d5d114b66eb35946c45
BLAKE2b-256 015a36a12270fa487338004f6aaea11645cb0b28c54eaf8f6e392afee5efcc41

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page