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
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from aws_cdk.core import Construct, RemovalPolicy, Stack, StackProps
from aws_cdk.aws_ssm import StringParameter
from aws_cdk.aws_cloudfront import SecurityPolicyProtocol
from cloudcomponents.cdk_static_website import StaticWebsite
from cloudcomponents.cdk_lambda_at_edge_pattern import HttpHeaders
class StaticWebsiteStack(Stack):
def __init__(self, scope, id, *, description=None, env=None, stackName=None, tags=None, synthesizer=None, terminationProtection=None, analyticsReporting=None):
super().__init__(scope, id, description=description, env=env, stackName=stackName, tags=tags, synthesizer=synthesizer, terminationProtection=terminationProtection, analyticsReporting=analyticsReporting)
certificate_arn = StringParameter.value_from_lookup(self, "/certificate/cloudcomponents.org")
website = StaticWebsite(self, "StaticWebsite",
bucket_configuration=WebsiteBucketProps(
removal_policy=RemovalPolicy.DESTROY
),
alias_configuration=AliasProps(
domain_name="cloudcomponents.org",
names=["www.cloudcomponents.org", "cloudcomponents.org"],
acm_cert_ref=certificate_arn
)
)
# A us-east-1 stack is generated under the hood
http_headers = HttpHeaders(self, "HttpHeaders",
http_headers={
"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.add_lambda_function_association(http_headers)
Example 2: Cloudfront URL with existing sources and up to date Securitypolicy
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from aws_cdk.core import Construct, RemovalPolicy, Stack, StackProps
from aws_cdk.aws_ssm import StringParameter
from cloudcomponents.cdk_static_website import StaticWebsite
from aws_cdk.aws_cloudfront import SecurityPolicyProtocol
class StaticWebsiteWithExistingSourcesAndSecurityPolicyStack(Stack):
def __init__(self, scope, id, *, description=None, env=None, stackName=None, tags=None, synthesizer=None, terminationProtection=None, analyticsReporting=None):
super().__init__(scope, id, description=description, env=env, stackName=stackName, tags=tags, synthesizer=synthesizer, terminationProtection=terminationProtection, analyticsReporting=analyticsReporting)
certificate_arn = StringParameter.value_from_lookup(self, "/certificate/cloudcomponents.org")
StaticWebsite(self, "StaticWebsite",
bucket_configuration=WebsiteBucketProps(
source="../path/to/your/static/webpage",
removal_policy=RemovalPolicy.DESTROY
),
alias_configuration=AliasProps(
domain_name="cloudcomponents.org",
names=["www.cloudcomponents.org", "cloudcomponents.org"],
acm_cert_ref=certificate_arn,
security_policy=SecurityPolicyProtocol.TLS_V1_2_2018
)
)
Lambda@Edge function
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
website.add_lambda_function_association(
event_type=LambdaEdgeEventType.ORIGIN_REQUEST,
lambda_function=Version.from_version_arn(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.15.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 750450d823fa2be1a1d48496576918919c7a9429109f5e0a87ad47154d1c2011 |
|
MD5 | d2231203a4495f9bb134ba34692d77a7 |
|
BLAKE2b-256 | c2532536a286985fa7257d94b5b67da6e3e032082ed19f3164d56c57b75a74b9 |
Close
Hashes for cloudcomponents.cdk_static_website-1.15.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7d55558e74e32daf8ea4c8e6261740c0807d1c53b097ac1aa03aff239b6b2e5 |
|
MD5 | 63a1af8d6e432ca7c4fad2d2bbf6e60f |
|
BLAKE2b-256 | 0108e6173c34f7db2e048b5c635bece95963deb6c2e4fc47bbb3c266516b91a5 |