Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from aws_cdk import (
  3. core as cdk_core,
  4. aws_s3 as cdk_s3,
  5. aws_s3_deployment as cdk_s3_deploy
  6. )
  7.  
  8.  
  9. class FrontendStack(cdk_core.Stack):
  10. def __init__(self, app, id, **kwargs):
  11. super().__init__(app, id, **kwargs)
  12. self.app_prefix = getattr(app, "prefix", "app")
  13. self.www_index = "index.html"
  14.  
  15.  
  16. def set_frontend_bucket(self):
  17. # Create frontend bucket
  18. frontend_bucket_name = "{}-www".format(self.app_prefix.lower())
  19. self.frontend_bucket = cdk_s3.Bucket(
  20. scope=self, id="frontend_bucket",
  21. block_public_access=block_public_access,
  22. website_index_document=self.www_index,
  23. removal_policy=cdk_core.RemovalPolicy.DESTROY
  24. )
  25.  
  26.  
  27. class DeployFrontendStack(cdk_core.Stack):
  28. def __init__(self, app, id, **kwargs):
  29. super().__init__(app, id, **kwargs)
  30. self.app_prefix = getattr(app, "prefix", "app")
  31.  
  32. def upload_code(self, bucket):
  33. source = cdk_s3_deploy.Source.asset("./frontend")
  34. cdk_s3_deploy.BucketDeployment(
  35. scope=self,
  36. id="{}_frontend_deployment".format(self.app_prefix),
  37. destination_bucket=bucket,
  38. source=source
  39. )
  40.  
  41.  
  42. # Main app
  43. app = cdk_core.App()
  44. app.prefix = app.node.try_get_context("app_prefix")
  45. if app.prefix is None:
  46. app.prefix = "App"
  47.  
  48. # Frontend bucket and CloudFront distribution
  49. FrontendStack = FrontendStack(app, "{}-Frontend".format(app.prefix))
  50. FrontendStack.set_frontend_bucket()
  51.  
  52. # Upload static website to S3
  53. DeployFrontendStack = DeployFrontendStack(app, "{}-DeployFrontend".format(
  54. app.prefix))
  55. DeployFrontendStack.upload_code(FrontendStack.frontend_bucket)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement