Skip to main content

CDK Construct for a private frontend hosting S3 bucket

Project description

CDK Private S3 Hosting Construct

This is a CDK construct that creates a private S3 bucket and an Application Load Balancer (ALB) with a listener rule that forwards requests to the S3 bucket.

You can use this construct for a enterprise use case where you want to host a static website in a private network.

Original idea is from this blog post.

View on Construct Hub Open in Visual Studio Code npm version Build Status Release Status License npm downloads

Architecture

Architecture

Installation

You can install the package via npm:

npm install cdk-private-s3-hosting

Usage

To create a private S3 bucket and an ALB with a listener rule that forwards requests to the S3 bucket, you can use the following code:

import { PrivateS3Hosting } from 'cdk-private-s3-hosting';

const privateS3Hosting = new PrivateS3Hosting(this, 'PrivateS3Hosting', {
  domainName: 'cryer-nao-domain.com',
});

After you deploy the stack, you can access the S3 bucket using the ALB's DNS name from the VPC where the stack is deployed.

For example, if you put the hoge.txt file in the S3 bucket, you can access it using the following command:

curl http://cryer-nao-domain.com/hoge.txt

Deploy the frontend assets

You can deploy the frontend assets to the S3 bucket like below:

import { PrivateS3Hosting } from 'cdk-private-s3-hosting';
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';

const privateS3Hosting = new PrivateS3Hosting(this, 'PrivateS3Hosting', {
  domainName: 'cryer-nao-domain.com',
});

new s3deploy.BucketDeployment(this, 'DeployWebsite', {
  sources: [s3deploy.Source.asset('./website-dist')],
  destinationBucket: websiteBucket,
  bucket: privateS3Hosting.bucket,
});

After deploying the stack, you can access the website using the domainName you specified from the VPC.

curl http://cryer-nao-domain.com

Note: All access to the path pattern */ will be redirected to /index.html. Therefore, it will function correctly even when the path is set on the frontend and the page is reloaded.

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_private_s3_hosting-0.0.1.tar.gz (56.6 kB view details)

Uploaded Source

Built Distribution

cdk_private_s3_hosting-0.0.1-py3-none-any.whl (55.5 kB view details)

Uploaded Python 3

File details

Details for the file cdk_private_s3_hosting-0.0.1.tar.gz.

File metadata

File hashes

Hashes for cdk_private_s3_hosting-0.0.1.tar.gz
Algorithm Hash digest
SHA256 336379236f540cd8043f4f21fd07f23a88d8bf671e1ad8dc79ab99735af1153f
MD5 f17f866f4064135f5fff9157e5d0a7ea
BLAKE2b-256 8223a1c27bd1d84f442cc93385949d85d5b88904dc27d4410c1b5dccc55c9abf

See more details on using hashes here.

File details

Details for the file cdk_private_s3_hosting-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cdk_private_s3_hosting-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 63138bbcee0c4d79f0404253f06b378dcf13d40a900ff29abc3c1d9a1da67683
MD5 1127347f70b515a03a96de2c3de15264
BLAKE2b-256 abbe92148142439f6f3d9767e225fefa523a7c2dd307fe58c708e81a88a8b593

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