[aws-cdk-cloudfront-s3] Simple AWS CDK Construct library to create CloudFront with S3 using OAI

Today I publish a new npm library for AWS CDK. We can easy to create website using CloudFront / S3 with using […]

広告ここから
広告ここまで

目次

    Today I publish a new npm library for AWS CDK. We can easy to create website using CloudFront / S3 with using OAI (Origin Access Identity)

    Why use OAI?

    If we can publish the s3 bucket object, we just turn on the “S3 Static website hosting” options. That’s all!

    But, if we provide private content or required to be private the s3 bucket, we can not use the option.

    We have to enable the Origin Access Identity option between CloudFront and S3. And if we use this way, we have to add Lambda@edge to the CloudFront distribution to handle a slash ended URL request like /blog/.

    What is this?

    The construct library will create these resources.

    • CloudFront distribution
    • S3 bucket
    • OAI
    • Lambda@edge

    We can easy to create a website environment with private s3 bucket.

    Usage

    Most simply example is this.

    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

    We can import ACM certification to set a 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']
    });

    Try it out!

    We can easy to try the libs using the example.

    $ git clone [email protected]:wpkyoto/aws-cdk-cloudfront-s3.git
    $ cd aws-cdk-cloudfront-s3
    $ npm install
    $ cd example
    $ npm install
    $ npm run build && npm run cdk deploy

    And the stack template YAML can see here.

    広告ここから
    広告ここまで

    Random posts

    Home
    Search
    Bookmark