web_ui.yaml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. AWSTemplateFormatVersion: "2010-09-09"
  2. Description: Amazon S3 Find and Forget Web UI
  3. Parameters:
  4. AccessLogsBucket:
  5. Type: String
  6. Default: ""
  7. CreateCloudFrontDistribution:
  8. Type: String
  9. DeployWebUI:
  10. Type: String
  11. ResourcePrefix:
  12. Type: String
  13. Conditions:
  14. WithAccessLogs: !Not [!Equals [!Ref AccessLogsBucket, ""]]
  15. ShouldDeployWebUI: !Equals [!Ref DeployWebUI, "true"]
  16. WithCloudFront: !And
  17. - !Equals [!Ref CreateCloudFrontDistribution, "true"]
  18. - !Condition ShouldDeployWebUI
  19. Resources:
  20. WebUIBucket:
  21. Type: AWS::S3::Bucket
  22. Properties:
  23. VersioningConfiguration:
  24. Status: Enabled
  25. BucketEncryption:
  26. ServerSideEncryptionConfiguration:
  27. - ServerSideEncryptionByDefault:
  28. SSEAlgorithm: AES256
  29. CorsConfiguration:
  30. CorsRules:
  31. - AllowedHeaders: ["*"]
  32. AllowedMethods: [GET]
  33. AllowedOrigins: ["*"]
  34. Id: !Sub ${ResourcePrefix}CorsRule
  35. MaxAge: 3600
  36. LoggingConfiguration: !If
  37. - WithAccessLogs
  38. - DestinationBucketName: !Ref AccessLogsBucket
  39. LogFilePrefix: !Sub ${ResourcePrefix}/
  40. - !Ref AWS::NoValue
  41. WebUIBucketPolicy:
  42. Type: AWS::S3::BucketPolicy
  43. Condition: ShouldDeployWebUI
  44. Properties:
  45. Bucket: !Ref WebUIBucket
  46. PolicyDocument:
  47. Statement:
  48. - Sid: HttpsOnly
  49. Action: '*'
  50. Effect: Deny
  51. Resource:
  52. - !Sub arn:${AWS::Partition}:s3:::${WebUIBucket}
  53. - !Sub arn:${AWS::Partition}:s3:::${WebUIBucket}/*
  54. Principal: '*'
  55. Condition:
  56. Bool:
  57. 'aws:SecureTransport': 'false'
  58. - !If
  59. - WithCloudFront
  60. - Sid: CloudFrontOriginOnly
  61. Action: s3:GetObject
  62. Effect: Allow
  63. Resource: !Sub arn:${AWS::Partition}:s3:::${WebUIBucket}/*
  64. Principal:
  65. CanonicalUser: !GetAtt CloudFrontOriginAccessIdentity.S3CanonicalUserId
  66. - !Ref AWS::NoValue
  67. CloudFrontOriginAccessIdentity:
  68. Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
  69. Condition: WithCloudFront
  70. Properties:
  71. CloudFrontOriginAccessIdentityConfig:
  72. Comment: !Ref WebUIBucket
  73. CloudFrontDistribution:
  74. Type: AWS::CloudFront::Distribution
  75. Condition: WithCloudFront
  76. Properties:
  77. DistributionConfig:
  78. Origins:
  79. - DomainName: !GetAtt WebUIBucket.RegionalDomainName
  80. Id: !Sub ${ResourcePrefix}-myS3Origin
  81. S3OriginConfig:
  82. OriginAccessIdentity: !Sub origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}
  83. Enabled: true
  84. HttpVersion: http2
  85. Comment: The Distribution for Amazon S3 Find and Forget
  86. DefaultRootObject: index.html
  87. DefaultCacheBehavior:
  88. AllowedMethods:
  89. - HEAD
  90. - GET
  91. - OPTIONS
  92. TargetOriginId: !Sub ${ResourcePrefix}-myS3Origin
  93. ForwardedValues:
  94. QueryString: false
  95. Cookies:
  96. Forward: none
  97. ViewerProtocolPolicy: redirect-to-https
  98. PriceClass: PriceClass_All
  99. ViewerCertificate:
  100. CloudFrontDefaultCertificate: true
  101. Logging: !If
  102. - WithAccessLogs
  103. - Bucket: !Sub ${AccessLogsBucket}.s3.${AWS::URLSuffix}
  104. IncludeCookies: false
  105. Prefix: !Sub ${ResourcePrefix}/
  106. - !Ref AWS::NoValue
  107. Outputs:
  108. CloudFrontDistribution:
  109. Value: !If
  110. - WithCloudFront
  111. - !Ref CloudFrontDistribution
  112. - "none"
  113. Origin:
  114. Value: !If
  115. - WithCloudFront
  116. - !Sub "https://${CloudFrontDistribution.DomainName}"
  117. - !Sub "https://${WebUIBucket.RegionalDomainName}"
  118. Description: Web UI Origin
  119. Url:
  120. Value: !If
  121. - WithCloudFront
  122. - !Sub "https://${CloudFrontDistribution.DomainName}"
  123. - !Sub "https://${WebUIBucket.RegionalDomainName}/index.html"
  124. Description: Web UI Url
  125. WebUIBucket:
  126. Value: !Ref WebUIBucket
  127. Description: Web UI S3 Bucket