StaticSiteBase
The StaticSiteBase
construct is a foundational AWS Cloud Development Kit (CDK) construct for creating and managing static websites hosted on AWS. It facilitates the deployment of website assets to an Amazon S3 bucket and configures a CloudFront distribution for content delivery.
Features
- S3 Bucket Deployment: Automatically provisions an S3 bucket for hosting the website's static assets.
- CloudFront Distribution: Creates a CloudFront distribution to deliver the website content with low latency and high transfer speeds.
- Custom Domain Configuration: Allows for custom domain configuration with an ACM (AWS Certificate Manager) certificate for HTTPS.
Properties
- certificateArn (optional): The ARN (Amazon Resource Name) of an ACM certificate. This must be in
us-east-1
- domainNames (optional): An array of domain names for the website.
- sourceAsset: The source asset to be deployed to the S3 bucket.
Usage
Below is an example of how to use the StaticSiteBase
construct:
import { StaticSiteBase, StaticSiteProps } from '@devkit-io/constructs';
import { Source } from 'aws-cdk-lib/aws-s3-deployment';
import { App, Stack } from 'aws-cdk-lib';
const app = new App();
const stack = new Stack(app, 'StaticWebsiteStack');
const siteProps: StaticSiteProps = {
certificateArn: 'arn:aws:acm:region:account:certificate/id',
domainNames: ['example.com', 'www.example.com'],
sourceAsset: Source.asset('./source'),
};
new StaticSiteBase(stack, 'MyWebsite', siteProps);
In this example, a new StaticSiteBase
construct is created with custom domain configuration and a source asset directory specified for deployment.
Error Handling
An error will be thrown if either certificateArn
or domainNames
is provided without the other, as both properties are required for custom domain configuration.
AWS Resources
- S3 Bucket: Hosts the website's static assets with public access blocked.
- CloudFront Distribution: Configured with the S3 bucket as its origin source for content delivery.
- ACM Certificate (optional): Utilized for HTTPS when custom domains are configured.
- Origin Access Identity: Used to grant the CloudFront distribution read access to the S3 bucket.