@rayova/cdk-cloudfront-rules
TypeScript icon, indicating that this package has built-in type declarations

0.0.0 • Public • Published

CDK CloudFront Rules

This CDK construct produces a CloudFront Function (not to be confused with Lambda @ Edge) that simplifies CloudFront-based URL rewriting and redirects.

Example

// Create a CloudFrontRules construct
const cloudFrontRules = new CloudFrontRules(scope, 'CloudFrontRules', {
  rules: [
    // Rewrite URIs matching /rewrite-* to /* using a capture group. (Think
    // Apache/.htaccess RewriteRule)
    Rule.rewriteRule({
      pattern: '^/rewrite-(.*)',
      location: '/$1',
    }),

    // Redirect /redirect-* to https://www.example.com/*
    Rule.rewriteRule({
      pattern: '^/redirect-(.*)',
      patternFlags: 'i', // case insensitive
      location: 'https://www.example.com/$1',
      redirectType: RedirectType.TEMPORARY,
    }),

    // Note: Rules are applied in the order they're provided. Rewrite rules
    // are applied while there has not yet been a redirect or matching
    // rewrite with the 'last' option.
    //
    // In this example, were you to visit a URI like
    // /rewrite-redirect-foobar, the first rule will rewrite you to
    // /redirect-foobar and then the second rule will match,
    // redirecting you to https://www.example.com/foobar
  ],
});

// Create your CloudFront distribution.
const distribution = new cloudfront.Distribution(scope, 'Distribution', {
  defaultBehavior: {
    origin: new cloudfront_origins.HttpOrigin('www.example.com', {
      protocolPolicy: cloudfront.OriginProtocolPolicy.HTTPS_ONLY,
    }),

    // Associate the produced function with VIEWER_REQUEST events.
    functionAssociations: [{
      eventType: cloudfront.FunctionEventType.VIEWER_REQUEST,
      function: cloudFrontRules.function,
    }],
  },
});

/@rayova/cdk-cloudfront-rules/

    Package Sidebar

    Install

    npm i @rayova/cdk-cloudfront-rules

    Weekly Downloads

    0

    Version

    0.0.0

    License

    Apache-2.0

    Unpacked Size

    81.8 kB

    Total Files

    13

    Last publish

    Collaborators

    • misterjoshua
    • ekellendonk