Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ Creates an AWS APIGateway from definitions in an Openapi document based on Zod s

- Check [Construct OpenapiGatewayLambda](./docs/openapigateway-lambda.md) for more details

## Construct StaticS3Website

Creates an S3 bucket and upload the given folder into it

- Check [Construct StaticS3Website](./docs/static-s3-website.md) for more details

## WSO2

Expand Down
78 changes: 78 additions & 0 deletions docs/static-s3-website.md
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.
Copy link
Owner

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"

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?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussion: I would remove this paragraph as there isn't another construct with a similar behavior, so this is not "better", it's simply a new composition.

- 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',
Copy link
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.
But having it as default is indeed an option.

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')],
Copy link
Owner

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

@MarcioMeier MarcioMeier May 8, 2024

Choose a reason for hiding this comment

The 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.
In short, yes, it will add the cache-control headers to the files

},
{
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)
```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praise: The "complex" example is very nice!