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.
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.
[cloudshell-user@ip-10-0-31-170 ~]$ curl http://cryer-nao-domain.com/ -L
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<script type="module" crossorigin src="/assets/index-f40OySzR.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-DiwrgTda.css">
</head>
<body>
<div id="root"></div>
</body>
</html>
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.
Setup DNS
This construct creates Route53 hosted zone and an A record for the domain name you specified by default.
If you want to use your own DNS settings(e.g. using a corporate DNS server),
you can disable the Route53 hosted zone creation by setting the enablePrivateDns
property to false
.
import { PrivateS3Hosting } from 'cdk-private-s3-hosting';
const privateS3Hosting = new PrivateS3Hosting(this, 'PrivateS3Hosting', {
domainName: 'cryer-nao-domain.com',
enablePrivateDns: false,
});
TLS Certificate
If you want to use HTTPS, you need to create a TLS certificate in ACM and pass it to the certificate
property.
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
import { PrivateS3Hosting } from 'cdk-private-s3-hosting';
declare const certificate: acm.ICertificate;
const privateS3Hosting = new PrivateS3Hosting(this, 'PrivateS3Hosting', {
domainName: 'cryer-nao-domain.com',
certificate,
});
Of course, specified domain name (domainName
and subDomain
) must be the same as the domain name of the certificate.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cdk_private_s3_hosting-0.0.5.tar.gz
.
File metadata
- Download URL: cdk_private_s3_hosting-0.0.5.tar.gz
- Upload date:
- Size: 60.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91381fa9dda5f0da5cd1cfd91a6747f277b1304e95089601a72bc4518042deb7 |
|
MD5 | ab7bd0ae38227102f5715d2d608b2af7 |
|
BLAKE2b-256 | fc59ee710ad0dc01f2143e767af684fb1f0b89a35495af396a0379548a1ccc76 |
File details
Details for the file cdk_private_s3_hosting-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: cdk_private_s3_hosting-0.0.5-py3-none-any.whl
- Upload date:
- Size: 59.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9202336b81644ef7478d0a01198d4739d17925e54d03173ce2cb2aea8d026e91 |
|
MD5 | 12b12f34d934f9cd1aee8781295259a0 |
|
BLAKE2b-256 | 2c775a694605468c60ad7b85f8869d30cb0e767aa8f0dafe717815edf0c86afd |