Cdk component that creates a static website using S3, configures CloudFront (CDN) and maps a custom domain via Route53 (DNS)
Project description
@cloudcomponents/cdk-static-website
Cdk component that creates a static website using S3, configures CloudFront (CDN) and maps a custom domain via Route53 (DNS)
Install
TypeScript/JavaScript:
npm i @cloudcomponents/cdk-static-website
Python:
pip install cloudcomponents.cdk-static-website
How to use
Example 1: With an existing certificate
import { Construct, RemovalPolicy, Stack, StackProps } from '@aws-cdk/core';
import { StringParameter } from '@aws-cdk/aws-ssm';
import { SecurityPolicyProtocol } from '@aws-cdk/aws-cloudfront';
import { StaticWebsite } from '@cloudcomponents/cdk-static-website';
import { HttpHeaders } from '@cloudcomponents/cdk-lambda-at-edge-pattern';
export class StaticWebsiteStack extends Stack {
constructor(scope: Construct, id: string, props: StackProps) {
super(scope, id, props);
const certificateArn = StringParameter.valueFromLookup(
this,
'/certificate/cloudcomponents.org',
);
const website = new StaticWebsite(this, 'StaticWebsite', {
bucketConfiguration: {
removalPolicy: RemovalPolicy.DESTROY,
},
aliasConfiguration: {
domainName: 'cloudcomponents.org',
names: ['www.cloudcomponents.org', 'cloudcomponents.org'],
acmCertRef: certificateArn,
},
});
// A us-east-1 stack is generated under the hood
const httpHeaders = new HttpHeaders(this, 'HttpHeaders', {
httpHeaders: {
'Content-Security-Policy':
"default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; object-src 'none'; connect-src 'self'",
'Strict-Transport-Security':
'max-age=31536000; includeSubdomains; preload',
'Referrer-Policy': 'same-origin',
'X-XSS-Protection': '1; mode=block',
'X-Frame-Options': 'DENY',
'X-Content-Type-Options': 'nosniff',
'Cache-Control': 'no-cache',
},
});
website.addLambdaFunctionAssociation(httpHeaders);
}
}
Example 2: Cloudfront URL with existing sources and up to date Securitypolicy
import { Construct, RemovalPolicy, Stack, StackProps } from '@aws-cdk/core';
import { StringParameter } from '@aws-cdk/aws-ssm';
import { StaticWebsite } from '@cloudcomponents/cdk-static-website';
import { SecurityPolicyProtocol } from '@aws-cdk/aws-cloudfront';
export class StaticWebsiteWithExistingSourcesAndSecurityPolicyStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const certificateArn = StringParameter.valueFromLookup(
this,
'/certificate/cloudcomponents.org',
);
new StaticWebsite(this, 'StaticWebsite', {
bucketConfiguration: {
source: '../path/to/your/static/webpage',
removalPolicy: RemovalPolicy.DESTROY,
},
aliasConfiguration: {
domainName: 'cloudcomponents.org',
names: ['www.cloudcomponents.org', 'cloudcomponents.org'],
acmCertRef: certificateArn,
securityPolicy: SecurityPolicyProtocol.TLS_V1_2_2018
},
});
}
}
Lambda@Edge function
website.addLambdaFunctionAssociation({
eventType: LambdaEdgeEventType.ORIGIN_REQUEST,
lambdaFunction: Version.fromVersionArn(
stack,
'LambdaEdge',
'arn:aws:lambda:us-east-1:123456789012:function:my-function:1',
),
});
API Reference
See API.md.
Example
See more complete examples.
License
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
Close
Hashes for cloudcomponents.cdk-static-website-1.49.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8bdf9a2cbb2d1d83a9ff802d712c587acf1822d24899988b96818e7ac38a458 |
|
MD5 | 4f2aa37e0bd67eed69ad7884c047197f |
|
BLAKE2b-256 | 79266ff6d91f7d8fcaf8420730b4b075714931307a3384245765cdde275d41c1 |
Close
Hashes for cloudcomponents.cdk_static_website-1.49.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5c3a52839536312d06b2c1c4882a7cb100872b3ee36a29f9bdd6fe5ccd3cc72 |
|
MD5 | 3c19e0c75de107e0fe7f465bcfcc97a6 |
|
BLAKE2b-256 | 7b4b1b1461fffbfe3c87f2d1f71d668342a9f1ecbbd2cc4cc33b660a6ff871da |