Skip to content

Instantly share code, notes, and snippets.

@bgdnlp
Created August 8, 2019 12:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgdnlp/e3e8b703baa52cfde6ae5ceb1d537678 to your computer and use it in GitHub Desktop.
Save bgdnlp/e3e8b703baa52cfde6ae5ceb1d537678 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from aws_cdk import (
core as cdk_core,
aws_s3 as cdk_s3,
aws_s3_deployment as cdk_s3_deploy
)
class FrontendStack(cdk_core.Stack):
def __init__(self, app, id, **kwargs):
super().__init__(app, id, **kwargs)
self.app_prefix = getattr(app, "prefix", "app")
self.www_index = "index.html"
def set_frontend_bucket(self):
# Create frontend bucket
frontend_bucket_name = "{}-www".format(self.app_prefix.lower())
self.frontend_bucket = cdk_s3.Bucket(
scope=self, id="frontend_bucket",
block_public_access=block_public_access,
website_index_document=self.www_index,
removal_policy=cdk_core.RemovalPolicy.DESTROY
)
class DeployFrontendStack(cdk_core.Stack):
def __init__(self, app, id, **kwargs):
super().__init__(app, id, **kwargs)
self.app_prefix = getattr(app, "prefix", "app")
def upload_code(self, bucket):
source = cdk_s3_deploy.Source.asset("./frontend")
cdk_s3_deploy.BucketDeployment(
scope=self,
id="{}_frontend_deployment".format(self.app_prefix),
destination_bucket=bucket,
source=source
)
# Main app
app = cdk_core.App()
app.prefix = app.node.try_get_context("app_prefix")
if app.prefix is None:
app.prefix = "App"
# Frontend bucket and CloudFront distribution
FrontendStack = FrontendStack(app, "{}-Frontend".format(app.prefix))
FrontendStack.set_frontend_bucket()
# Upload static website to S3
DeployFrontendStack = DeployFrontendStack(app, "{}-DeployFrontend".format(
app.prefix))
DeployFrontendStack.upload_code(FrontendStack.frontend_bucket)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment