@wpkyoto/aws-cdk-cloudfront-s3
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

CloudFront S3 with Origin Access Identity

AWS CDK version

Now using v1.47.0.

Install

$ npm install -S @wpkyoto/aws-cdk-cloudfront-s3

Usage

import { expect as expectCDK, haveResource, SynthUtils } from '@aws-cdk/assert';
import * as cdk from '@aws-cdk/core';
import { Bucket } from '@aws-cdk/aws-s3';
import { CloudfrontS3 } from '@wpkyoto/aws-cdk-cloudfront-s3';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'TestStack');
new CloudfrontS3(stack, 'MyTestConstruct', {
  name: 'example',
});

Use own S3 bucket

import { expect as expectCDK, haveResource, SynthUtils } from '@aws-cdk/assert';
import * as cdk from '@aws-cdk/core';
import { Bucket } from '@aws-cdk/aws-s3';
import { CloudfrontS3 } from '@wpkyoto/aws-cdk-cloudfront-s3';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'TestStack');
new CloudfrontS3(stack, 'MyTestConstruct', {
  name: 'example',
  s3Bucket: Bucket.fromBucketName(stack, 'Dummy', 'dummy'),
});

Custom Domain

import { expect as expectCDK, haveResource, SynthUtils } from '@aws-cdk/assert';
import * as cdk from '@aws-cdk/core';
import { Bucket } from '@aws-cdk/aws-s3';
import { CloudfrontS3 } from '@wpkyoto/aws-cdk-cloudfront-s3';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'TestStack');
new CloudfrontS3(stack, 'MyTestConstruct', {
  name: 'example',
  acmCertificationARN: 'YOUR_ACM_ARN',
  domains: ['example.com']
});

Own Lambda@edge function

import { expect as expectCDK, haveResource, SynthUtils } from '@aws-cdk/assert';
import * as cdk from '@aws-cdk/core';
import { Bucket } from '@aws-cdk/aws-s3';
import { CloudfrontS3 } from '@wpkyoto/aws-cdk-cloudfront-s3';
import { Runtime } from '@aws-cdk/aws-lambda';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import {createHash} from 'crypto'
import { readFileSync } from 'fs';
import { join } from 'path';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'TestStack');
new CloudfrontS3(stack, 'MyTestConstruct', {
  name: 'example',
  edgeFunctions: {
    "origin-response": new NodejsFunction(this, 'OriginRequestHandler', {
        handler: 'handler',
        runtime: Runtime.NODEJS_12_X,
        functionName: `ExampleEdgeFunction`,
        entry: join(__dirname, '/lambda/originResponse.ts'),
      }).addVersion(
        createHash('sha256').update(
          readFileSync(
            join(__dirname, '/lambda/originResponse.ts'),
            'utf-8'
          )
        ).digest('hex')
      )
  }
});

Readme

Keywords

none

Package Sidebar

Install

npm i @wpkyoto/aws-cdk-cloudfront-s3

Weekly Downloads

0

Version

1.1.0

License

Apache-2.0

Unpacked Size

36.7 kB

Total Files

9

Last publish

Collaborators

  • hideokamoto