-
Notifications
You must be signed in to change notification settings - Fork 7
feat: static s3 website construct proposal #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| ## Construct StaticS3Website | ||
|
|
||
| This construct creates an S3 bucket and upload any content into it. | ||
| It is a composition of the following CDK constructs: | ||
| - [AWS S3 Bucket lib](https://github.com/aws/aws-cdk/tree/main/packages/aws-cdk-lib/aws-s3) | ||
| - [AWS S3 Bucket deployment lib](https://github.com/aws/aws-cdk/tree/main/packages/aws-cdk-lib/aws-s3-deployment) | ||
|
|
||
| Why to use it over the CDK constructs? | ||
|
||
| - It has good defaults. e.g., enforcing ssl; restricted access; auto deleting bucket on deleting the stack. | ||
| - easier interfaces. i.e. less configuration | ||
| - Unified construct | ||
|
|
||
| ### Usage | ||
|
|
||
| #### Simple S3 static website | ||
|
|
||
| ```ts | ||
| // This will create the bucket `my-website-bucket` and upload the files in your local `dist` folder into it | ||
| new StaticS3Website(stack, 'my-website', { | ||
| bucketName: 'my-website-bucket', | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do the bucket name defaults to the id of the construct or it's a required prop?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have it as a required prop, so it be more explicit when building/reading the construct. |
||
| deployments: [{ | ||
| sources: ['./dist'], | ||
| }], | ||
| }); | ||
| ``` | ||
|
|
||
| #### Complex S3 static website | ||
|
|
||
| ```ts | ||
| const staticWebsiteConfig: StaticS3WebsiteProps = { | ||
| bucketName: 'my-website-bucket', | ||
| removalPolicy: RemovalPolicy.RETAIN, | ||
| blockPublicAccess: new BlockPublicAccess({ | ||
| blockPublicAcls: true, | ||
| ignorePublicAcls: true, | ||
| }), | ||
| deployments: [ | ||
| { | ||
| sources: [Source.asset('./dist', { exclude: ['*.html'] })], | ||
| cacheControl: [CacheControl.fromString('public,max-age=31536000,immutable')], | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: this will make the GET of the files on S3 to have response header "cache-control" set? If so, pls add this comment to the example
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will add the cache control metadata to the objects that will be uploaded by the sources selection. |
||
| }, | ||
| { | ||
| sources: [ | ||
| Source.asset('./dist', { | ||
| exclude: ['*', '!*.html'], | ||
| ignoreMode: IgnoreMode.GLOB, | ||
| }), | ||
| ], | ||
| cacheControl: [CacheControl.fromString('public,max-age=0,must-revalidate')], | ||
| }, | ||
| { | ||
| sources: [Source.asset('../assets')], | ||
| destinationKey: 'assets', | ||
| cacheControl: [CacheControl.fromString('public,max-age=31536000,immutable')], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const ALLOWED_IP_RANGE = ['10.0.0.0', '172.16.0.0']; | ||
|
|
||
| // creates the bucket and deploy the code | ||
| const staticWebsite = new StaticS3Website(stack, 'my-complex-website', staticWebsiteConfig); | ||
|
|
||
| // Creates an IAM policy | ||
| const ipLimitPolicy = new PolicyStatement({ | ||
| actions: ['s3:GetObject', 's3:List*'], | ||
| resources: [staticWebsite.bucket.arnForObjects('*')], | ||
| principals: [new AnyPrincipal()], | ||
| }); | ||
|
|
||
| // Adds condition to the policy | ||
| ipLimitPolicy.addCondition('IpAddress', { | ||
| 'aws:SourceIp': ALLOWED_IP_RANGE, | ||
| }); | ||
|
|
||
| // Attaches the policy to the deployment bucket | ||
| staticWebsite.bucket.addToResourcePolicy(ipLimitPolicy) | ||
| ``` | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Praise: The "complex" example is very nice! |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Todo: change to "This construct creates a S3 bucket and uploads the contents of a local folder to it"