Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement