@codedrifters/constructs 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +526 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1 +1,526 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @codedrifters/constructs
|
|
2
|
+
|
|
3
|
+
A collection of reusable AWS CDK constructs frequently used in CodeDrifters projects. This package provides pre-configured, secure, and production-ready constructs for common AWS infrastructure patterns.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [What are AWS CDK Constructs?](#what-are-aws-cdk-constructs)
|
|
8
|
+
- [Installation](#installation)
|
|
9
|
+
- [Constructs](#constructs)
|
|
10
|
+
- [S3 Constructs](#s3-constructs)
|
|
11
|
+
- [PrivateBucket](#privatebucket)
|
|
12
|
+
- [Static Hosting Constructs](#static-hosting-constructs)
|
|
13
|
+
- [StaticHosting](#statichosting)
|
|
14
|
+
- [StaticContent](#staticcontent)
|
|
15
|
+
- [Utility Functions](#utility-functions)
|
|
16
|
+
- [Git Utilities](#git-utilities)
|
|
17
|
+
- [String Utilities](#string-utilities)
|
|
18
|
+
- [Complete Example](#complete-example)
|
|
19
|
+
- [API Reference](#api-reference)
|
|
20
|
+
- [Further Documentation](#further-documentation)
|
|
21
|
+
|
|
22
|
+
## What are AWS CDK Constructs?
|
|
23
|
+
|
|
24
|
+
AWS CDK (Cloud Development Kit) constructs are reusable cloud components that encapsulate AWS resources and their configuration. Think of them as building blocks that combine multiple AWS services into higher-level abstractions.
|
|
25
|
+
|
|
26
|
+
For example, instead of manually configuring an S3 bucket, CloudFront distribution, Route53 records, and SSL certificates separately, a construct can combine all of these into a single "StaticHosting" construct that you can use with just a few lines of code.
|
|
27
|
+
|
|
28
|
+
**Key Benefits:**
|
|
29
|
+
- **Reusability**: Write once, use everywhere
|
|
30
|
+
- **Consistency**: Enforce best practices and security defaults
|
|
31
|
+
- **Simplicity**: Complex infrastructure becomes simple API calls
|
|
32
|
+
- **Type Safety**: Full TypeScript support with IntelliSense
|
|
33
|
+
|
|
34
|
+
For more information about AWS CDK constructs, see the [AWS CDK Developer Guide](https://docs.aws.amazon.com/cdk/v2/guide/home.html).
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
Add the package to your CDK project using Configulator/Projen configuration. This ensures consistent dependency management across your project.
|
|
39
|
+
|
|
40
|
+
### In a Monorepo (Recommended)
|
|
41
|
+
|
|
42
|
+
If you're using `@codedrifters/configulator` in a monorepo, add the package as a dependency in your sub-project configuration:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { TypeScriptProject } from '@codedrifters/configulator';
|
|
46
|
+
import { MonorepoProject } from '@codedrifters/configulator';
|
|
47
|
+
|
|
48
|
+
const myCdkProject = new TypeScriptProject({
|
|
49
|
+
name: 'my-cdk-project',
|
|
50
|
+
packageName: '@myorg/my-cdk-project',
|
|
51
|
+
outdir: 'packages/my-cdk-project',
|
|
52
|
+
parent: root, // Your MonorepoProject instance
|
|
53
|
+
|
|
54
|
+
deps: [
|
|
55
|
+
'@codedrifters/constructs',
|
|
56
|
+
],
|
|
57
|
+
|
|
58
|
+
devDeps: [
|
|
59
|
+
'aws-cdk-lib@catalog:', // Catalog versions are pre-configured
|
|
60
|
+
'constructs@catalog:',
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### In a Standalone CDK Project
|
|
66
|
+
|
|
67
|
+
If you're using AWS CDK directly (not via Configulator), add the package to your `deps` array:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { awscdk } from 'projen';
|
|
71
|
+
|
|
72
|
+
const project = new awscdk.AwsCdkTypeScriptApp({
|
|
73
|
+
name: 'my-cdk-app',
|
|
74
|
+
|
|
75
|
+
deps: [
|
|
76
|
+
'@codedrifters/constructs',
|
|
77
|
+
],
|
|
78
|
+
|
|
79
|
+
devDeps: [
|
|
80
|
+
'aws-cdk-lib',
|
|
81
|
+
'constructs',
|
|
82
|
+
],
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### After Adding to Configuration
|
|
87
|
+
|
|
88
|
+
After updating your projenrc configuration file, run:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
npx projen
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
This will update your `package.json` and install the dependencies.
|
|
95
|
+
|
|
96
|
+
### Peer Dependencies
|
|
97
|
+
|
|
98
|
+
This package requires the following peer dependencies:
|
|
99
|
+
|
|
100
|
+
- `aws-cdk-lib` - AWS CDK construct library
|
|
101
|
+
- `constructs` - Core constructs library
|
|
102
|
+
|
|
103
|
+
These should be added as dev dependencies in your project configuration. If you're using `@codedrifters/configulator`, the catalog versions (`@catalog:`) are automatically configured in the root `MonorepoProject`.
|
|
104
|
+
|
|
105
|
+
## Constructs
|
|
106
|
+
|
|
107
|
+
### S3 Constructs
|
|
108
|
+
|
|
109
|
+
#### PrivateBucket
|
|
110
|
+
|
|
111
|
+
A secure S3 bucket with sensible security defaults. This construct extends AWS CDK's `Bucket` construct with security best practices applied by default.
|
|
112
|
+
|
|
113
|
+
**Security Defaults:**
|
|
114
|
+
- Public access is blocked (`BlockPublicAccess.BLOCK_ALL`)
|
|
115
|
+
- SSL/TLS is enforced for all requests
|
|
116
|
+
- Bucket owner enforced object ownership
|
|
117
|
+
- Configurable removal policy (defaults to `RETAIN`)
|
|
118
|
+
|
|
119
|
+
**Basic Usage:**
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import { PrivateBucket } from '@codedrifters/constructs';
|
|
123
|
+
import { Stack } from 'aws-cdk-lib';
|
|
124
|
+
import { Construct } from 'constructs';
|
|
125
|
+
|
|
126
|
+
const stack = new Stack(app, 'MyStack');
|
|
127
|
+
|
|
128
|
+
// Create a private bucket with default security settings
|
|
129
|
+
const bucket = new PrivateBucket(stack, 'MyPrivateBucket');
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**With Custom Properties:**
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { RemovalPolicy } from 'aws-cdk-lib';
|
|
136
|
+
import { PrivateBucket } from '@codedrifters/constructs';
|
|
137
|
+
|
|
138
|
+
const bucket = new PrivateBucket(stack, 'MyPrivateBucket', {
|
|
139
|
+
removalPolicy: RemovalPolicy.DESTROY,
|
|
140
|
+
autoDeleteObjects: true,
|
|
141
|
+
versioned: true,
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
**Note:** The security settings (public access blocked, SSL enforced, etc.) cannot be overridden - they are always applied to ensure bucket security.
|
|
146
|
+
|
|
147
|
+
### Static Hosting Constructs
|
|
148
|
+
|
|
149
|
+
#### StaticHosting
|
|
150
|
+
|
|
151
|
+
A complete static website hosting solution that creates and configures:
|
|
152
|
+
|
|
153
|
+
- **S3 Bucket**: Private bucket for storing static files (using `PrivateBucket`)
|
|
154
|
+
- **CloudFront Distribution**: Global CDN for fast content delivery
|
|
155
|
+
- **SSL Certificate**: Wildcard certificate via AWS Certificate Manager
|
|
156
|
+
- **Route53 Records**: DNS entries for the domain and wildcard subdomains
|
|
157
|
+
- **Lambda@Edge Function**: Viewer request handler for path rewriting
|
|
158
|
+
- **SSM Parameters**: Stores bucket ARN, distribution domain, and distribution ID for later use
|
|
159
|
+
|
|
160
|
+
**Features:**
|
|
161
|
+
- Automatic wildcard SSL certificate generation
|
|
162
|
+
- Support for custom domains with automatic DNS configuration
|
|
163
|
+
- Lambda@Edge function for intelligent path rewriting
|
|
164
|
+
- Conservative caching policy (60s default TTL)
|
|
165
|
+
- Stores configuration in SSM Parameter Store for use by `StaticContent`
|
|
166
|
+
|
|
167
|
+
**Basic Usage (CloudFront Domain Only):**
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { StaticHosting } from '@codedrifters/constructs';
|
|
171
|
+
import { Stack } from 'aws-cdk-lib';
|
|
172
|
+
|
|
173
|
+
const hostingStack = new Stack(app, 'HostingStack', { env });
|
|
174
|
+
|
|
175
|
+
const hosting = new StaticHosting(hostingStack, 'static-hosting', {
|
|
176
|
+
description: 'My static website',
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
**With Custom Domain:**
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { StaticHosting } from '@codedrifters/constructs';
|
|
184
|
+
import { Stack, RemovalPolicy } from 'aws-cdk-lib';
|
|
185
|
+
|
|
186
|
+
const hostingStack = new Stack(app, 'HostingStack', { env });
|
|
187
|
+
|
|
188
|
+
const hosting = new StaticHosting(hostingStack, 'static-hosting', {
|
|
189
|
+
description: 'My static website',
|
|
190
|
+
staticDomainProps: {
|
|
191
|
+
baseDomain: 'example.com',
|
|
192
|
+
hostedZoneAttributes: {
|
|
193
|
+
hostedZoneId: 'Z1234567890ABC',
|
|
194
|
+
zoneName: 'example.com',
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
privateBucketProps: {
|
|
198
|
+
removalPolicy: RemovalPolicy.DESTROY,
|
|
199
|
+
autoDeleteObjects: true,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
**Properties:**
|
|
205
|
+
|
|
206
|
+
| Property | Type | Default | Description |
|
|
207
|
+
|----------|------|---------|-------------|
|
|
208
|
+
| `description` | `string` | `undefined` | Short description for traceability |
|
|
209
|
+
| `staticDomainProps` | `StaticDomainProps` | `undefined` | Domain configuration (optional) |
|
|
210
|
+
| `bucketArnParamName` | `string` | `"/STATIC_WEBSITE/BUCKET_ARN"` | SSM parameter name for bucket ARN |
|
|
211
|
+
| `distributionDomainParamName` | `string` | `"/STATIC_WEBSITE/DISTRIBUTION_DOMAIN"` | SSM parameter name for distribution domain |
|
|
212
|
+
| `distributionIDParamName` | `string` | `"/STATIC_WEBSITE/DISTRIBUTION_ID"` | SSM parameter name for distribution ID |
|
|
213
|
+
| `privateBucketProps` | `PrivateBucketProps` | `undefined` | Props to pass to the S3 bucket |
|
|
214
|
+
|
|
215
|
+
**StaticDomainProps:**
|
|
216
|
+
|
|
217
|
+
| Property | Type | Description |
|
|
218
|
+
|----------|------|-------------|
|
|
219
|
+
| `baseDomain` | `string` | The base domain (e.g., `example.com`) |
|
|
220
|
+
| `hostedZoneAttributes` | `HostedZoneAttributes` | Hosted zone ID and zone name |
|
|
221
|
+
|
|
222
|
+
**Outputs:**
|
|
223
|
+
|
|
224
|
+
The construct exposes:
|
|
225
|
+
- `fullDomain: string` - The full domain name (custom domain if provided, otherwise CloudFront domain)
|
|
226
|
+
|
|
227
|
+
**SSM Parameters Created:**
|
|
228
|
+
|
|
229
|
+
The construct automatically creates SSM parameters that can be referenced by `StaticContent`:
|
|
230
|
+
- Bucket ARN (default: `/STATIC_WEBSITE/BUCKET_ARN`)
|
|
231
|
+
- CloudFront Distribution Domain (default: `/STATIC_WEBSITE/DISTRIBUTION_DOMAIN`)
|
|
232
|
+
- CloudFront Distribution ID (default: `/STATIC_WEBSITE/DISTRIBUTION_ID`)
|
|
233
|
+
|
|
234
|
+
#### StaticContent
|
|
235
|
+
|
|
236
|
+
Deploys static content from a local directory to an S3 bucket. This construct is designed to work with `StaticHosting` and supports branch-based deployment paths for PR and feature branch previews.
|
|
237
|
+
|
|
238
|
+
**Features:**
|
|
239
|
+
- Deploys files from a local directory to S3
|
|
240
|
+
- Supports branch-based path prefixes (e.g., `feature-123.example.com/`)
|
|
241
|
+
- Automatically retrieves bucket ARN from SSM Parameter Store
|
|
242
|
+
- Configurable destination directory within the bucket
|
|
243
|
+
|
|
244
|
+
**How Branch-Based Deployment Works:**
|
|
245
|
+
|
|
246
|
+
The construct uses the current git branch name to create unique deployment paths. This allows multiple branches to deploy to the same bucket without conflicts:
|
|
247
|
+
|
|
248
|
+
```
|
|
249
|
+
S3 Bucket Structure:
|
|
250
|
+
├── example.com/ → Production/main branch
|
|
251
|
+
├── feature-123.example.com/ → Feature branch
|
|
252
|
+
├── pr-456.example.com/ → Pull request
|
|
253
|
+
└── stage.example.com/ → Staging branch
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
**Basic Usage:**
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
import { StaticContent, StaticHosting } from '@codedrifters/constructs';
|
|
260
|
+
import { Stack } from 'aws-cdk-lib';
|
|
261
|
+
|
|
262
|
+
// First, create the hosting infrastructure
|
|
263
|
+
const hostingStack = new Stack(app, 'HostingStack', { env });
|
|
264
|
+
const hosting = new StaticHosting(hostingStack, 'static-hosting', {
|
|
265
|
+
staticDomainProps: {
|
|
266
|
+
baseDomain: 'example.com',
|
|
267
|
+
hostedZoneAttributes: {
|
|
268
|
+
hostedZoneId: 'Z1234567890ABC',
|
|
269
|
+
zoneName: 'example.com',
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
// Then, deploy content in a separate stack
|
|
275
|
+
const contentStack = new Stack(app, 'ContentStack', { env });
|
|
276
|
+
contentStack.node.addDependency(hostingStack);
|
|
277
|
+
|
|
278
|
+
new StaticContent(contentStack, 'static-content', {
|
|
279
|
+
contentSourceDirectory: 'dist', // Local directory with built files
|
|
280
|
+
contentDestinationDirectory: '/', // Deploy to root of bucket path
|
|
281
|
+
fullDomain: hosting.fullDomain, // Use domain from StaticHosting
|
|
282
|
+
});
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
**With Custom Subdomain:**
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
new StaticContent(contentStack, 'static-content', {
|
|
289
|
+
contentSourceDirectory: 'dist',
|
|
290
|
+
contentDestinationDirectory: '/',
|
|
291
|
+
fullDomain: hosting.fullDomain,
|
|
292
|
+
subDomain: 'staging', // Override git branch detection
|
|
293
|
+
bucketArnParamName: '/STATIC_WEBSITE/BUCKET_ARN', // Custom param name
|
|
294
|
+
});
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
**Properties:**
|
|
298
|
+
|
|
299
|
+
| Property | Type | Default | Description |
|
|
300
|
+
|----------|------|---------|-------------|
|
|
301
|
+
| `contentSourceDirectory` | `string` | **Required** | Absolute path to directory containing files to deploy |
|
|
302
|
+
| `contentDestinationDirectory` | `string` | **Required** | Directory within bucket to place content (should start with `/`) |
|
|
303
|
+
| `fullDomain` | `string` | **Required** | Full domain name (from `StaticHosting.fullDomain`) |
|
|
304
|
+
| `subDomain` | `string` | Git branch name | Subdomain prefix (defaults to current git branch) |
|
|
305
|
+
| `bucketArnParamName` | `string` | `"/STATIC_WEBSITE/BUCKET_ARN"` | SSM parameter name for bucket ARN |
|
|
306
|
+
|
|
307
|
+
**Path Construction:**
|
|
308
|
+
|
|
309
|
+
The construct creates a unique path prefix using: `{subDomain}.{fullDomain}`
|
|
310
|
+
|
|
311
|
+
For example:
|
|
312
|
+
- Branch: `feature-123`, Domain: `example.com` → Path: `feature-123.example.com/`
|
|
313
|
+
- Branch: `main`, Domain: `example.com` → Path: `main.example.com/` (or just `example.com/` if subdomain is empty)
|
|
314
|
+
|
|
315
|
+
### Utility Functions
|
|
316
|
+
|
|
317
|
+
#### Git Utilities
|
|
318
|
+
|
|
319
|
+
Helper functions for working with Git in CDK deployments.
|
|
320
|
+
|
|
321
|
+
**`findGitBranch()`**
|
|
322
|
+
|
|
323
|
+
Returns the current git branch name.
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
import { findGitBranch } from '@codedrifters/constructs/lib/git-utils';
|
|
327
|
+
|
|
328
|
+
const branch = findGitBranch();
|
|
329
|
+
// Returns: "feature/1234" or "main" etc.
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
**`findGitRepoName()`**
|
|
333
|
+
|
|
334
|
+
Returns the repository name. Works in both local environments and GitHub Actions.
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
import { findGitRepoName } from '@codedrifters/constructs/lib/git-utils';
|
|
338
|
+
|
|
339
|
+
const repoName = findGitRepoName();
|
|
340
|
+
// Returns: "codedrifters/packages" or similar
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
In GitHub Actions, this uses the `GITHUB_REPOSITORY` environment variable. Locally, it extracts the name from the git remote URL.
|
|
344
|
+
|
|
345
|
+
#### String Utilities
|
|
346
|
+
|
|
347
|
+
Helper functions for string manipulation.
|
|
348
|
+
|
|
349
|
+
**`hashString(inString: string, trimLength?: number)`**
|
|
350
|
+
|
|
351
|
+
Creates a SHA-256 hash of a string, optionally trimmed to a specific length.
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
import { hashString } from '@codedrifters/constructs/lib/string-utils';
|
|
355
|
+
|
|
356
|
+
const hash = hashString('my-string');
|
|
357
|
+
// Returns: Full SHA-256 hash
|
|
358
|
+
|
|
359
|
+
const shortHash = hashString('my-string', 8);
|
|
360
|
+
// Returns: First 8 characters of hash
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
**`trimStringLength(inputString: string, maxLength: number)`**
|
|
364
|
+
|
|
365
|
+
Truncates a string to a maximum length if it exceeds it.
|
|
366
|
+
|
|
367
|
+
```typescript
|
|
368
|
+
import { trimStringLength } from '@codedrifters/constructs/lib/string-utils';
|
|
369
|
+
|
|
370
|
+
const short = trimStringLength('very long string', 10);
|
|
371
|
+
// Returns: "very long " (truncated to 10 chars)
|
|
372
|
+
|
|
373
|
+
const unchanged = trimStringLength('short', 10);
|
|
374
|
+
// Returns: "short" (no change needed)
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
## Complete Example
|
|
378
|
+
|
|
379
|
+
Here's a complete example showing how to use `StaticHosting` and `StaticContent` together:
|
|
380
|
+
|
|
381
|
+
```typescript
|
|
382
|
+
import { StaticContent, StaticHosting } from '@codedrifters/constructs';
|
|
383
|
+
import { App, RemovalPolicy, Stack } from 'aws-cdk-lib';
|
|
384
|
+
|
|
385
|
+
const app = new App();
|
|
386
|
+
|
|
387
|
+
const env = {
|
|
388
|
+
account: '123456789012',
|
|
389
|
+
region: 'us-east-1',
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
const baseDomain = 'example.com';
|
|
393
|
+
|
|
394
|
+
/*******************************************************************************
|
|
395
|
+
*
|
|
396
|
+
* Step 1: Create the hosting infrastructure
|
|
397
|
+
*
|
|
398
|
+
* This creates the S3 bucket, CloudFront distribution, SSL certificate,
|
|
399
|
+
* and DNS records.
|
|
400
|
+
*
|
|
401
|
+
******************************************************************************/
|
|
402
|
+
|
|
403
|
+
const hostingStack = new Stack(
|
|
404
|
+
app,
|
|
405
|
+
`static-hosting-dev-${env.account}-${env.region}`,
|
|
406
|
+
{ env }
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
const hosting = new StaticHosting(hostingStack, 'static-hosting', {
|
|
410
|
+
description: 'My static website',
|
|
411
|
+
privateBucketProps: {
|
|
412
|
+
removalPolicy: RemovalPolicy.DESTROY,
|
|
413
|
+
autoDeleteObjects: true,
|
|
414
|
+
},
|
|
415
|
+
staticDomainProps: {
|
|
416
|
+
baseDomain,
|
|
417
|
+
hostedZoneAttributes: {
|
|
418
|
+
hostedZoneId: 'Z1234567890ABC',
|
|
419
|
+
zoneName: baseDomain,
|
|
420
|
+
},
|
|
421
|
+
},
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
/*******************************************************************************
|
|
425
|
+
*
|
|
426
|
+
* Step 2: Deploy static content
|
|
427
|
+
*
|
|
428
|
+
* This deploys files from a local directory to the S3 bucket created above.
|
|
429
|
+
* The content stack must depend on the hosting stack to ensure the bucket
|
|
430
|
+
* exists before deployment.
|
|
431
|
+
*
|
|
432
|
+
******************************************************************************/
|
|
433
|
+
|
|
434
|
+
const contentStack = new Stack(
|
|
435
|
+
app,
|
|
436
|
+
`static-content-dev-${env.account}-${env.region}`,
|
|
437
|
+
{ env }
|
|
438
|
+
);
|
|
439
|
+
|
|
440
|
+
// Ensure hosting stack is created first
|
|
441
|
+
contentStack.node.addDependency(hostingStack);
|
|
442
|
+
|
|
443
|
+
new StaticContent(contentStack, 'static-content', {
|
|
444
|
+
contentSourceDirectory: 'src/website', // Path to your built website files
|
|
445
|
+
contentDestinationDirectory: '/', // Deploy to root
|
|
446
|
+
fullDomain: hosting.fullDomain, // Use the domain from StaticHosting
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
app.synth();
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
**Deployment Flow:**
|
|
453
|
+
|
|
454
|
+
1. Deploy the hosting stack first: `cdk deploy static-hosting-dev-*`
|
|
455
|
+
2. This creates the S3 bucket, CloudFront distribution, SSL certificate, and DNS records
|
|
456
|
+
3. Deploy the content stack: `cdk deploy static-content-dev-*`
|
|
457
|
+
4. This uploads your static files to the S3 bucket
|
|
458
|
+
5. Your website is now live at your domain!
|
|
459
|
+
|
|
460
|
+
**Branch-Based Deployments:**
|
|
461
|
+
|
|
462
|
+
When deploying from different git branches, the `StaticContent` construct automatically uses the branch name as a subdomain prefix. This allows you to have:
|
|
463
|
+
- `main` branch → `example.com`
|
|
464
|
+
- `feature-123` branch → `feature-123.example.com`
|
|
465
|
+
- `pr-456` branch → `pr-456.example.com`
|
|
466
|
+
|
|
467
|
+
All using the same S3 bucket and CloudFront distribution!
|
|
468
|
+
|
|
469
|
+
## API Reference
|
|
470
|
+
|
|
471
|
+
### Exports
|
|
472
|
+
|
|
473
|
+
The package exports the following:
|
|
474
|
+
|
|
475
|
+
**Constructs:**
|
|
476
|
+
- `PrivateBucket` - Secure S3 bucket
|
|
477
|
+
- `StaticHosting` - Complete static hosting solution
|
|
478
|
+
- `StaticContent` - Static content deployment
|
|
479
|
+
|
|
480
|
+
**Utilities:**
|
|
481
|
+
- `findGitBranch()` - Get current git branch
|
|
482
|
+
- `findGitRepoName()` - Get repository name
|
|
483
|
+
- `hashString()` - Hash a string
|
|
484
|
+
- `trimStringLength()` - Truncate a string
|
|
485
|
+
|
|
486
|
+
### Type Definitions
|
|
487
|
+
|
|
488
|
+
**PrivateBucketProps**
|
|
489
|
+
- Extends `BucketProps` from `aws-cdk-lib/aws-s3`
|
|
490
|
+
- All standard S3 bucket properties are supported
|
|
491
|
+
- Security settings are enforced and cannot be overridden
|
|
492
|
+
|
|
493
|
+
**StaticHostingProps**
|
|
494
|
+
- Extends `StackProps` from `aws-cdk-lib`
|
|
495
|
+
- See [StaticHosting](#statichosting) section for full property list
|
|
496
|
+
|
|
497
|
+
**StaticContentProps**
|
|
498
|
+
- See [StaticContent](#staticcontent) section for full property list
|
|
499
|
+
|
|
500
|
+
**StaticDomainProps**
|
|
501
|
+
- `baseDomain: string` - Base domain name
|
|
502
|
+
- `hostedZoneAttributes: HostedZoneAttributes` - Route53 hosted zone attributes
|
|
503
|
+
|
|
504
|
+
## Further Documentation
|
|
505
|
+
|
|
506
|
+
### AWS CDK Resources
|
|
507
|
+
|
|
508
|
+
- [AWS CDK Developer Guide](https://docs.aws.amazon.com/cdk/v2/guide/home.html) - Comprehensive guide to AWS CDK
|
|
509
|
+
- [AWS CDK API Reference](https://docs.aws.amazon.com/cdk/api/v2/) - Complete API documentation
|
|
510
|
+
- [AWS CDK Construct Library](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-construct-library.html) - All available constructs
|
|
511
|
+
|
|
512
|
+
### AWS Service Documentation
|
|
513
|
+
|
|
514
|
+
- [Amazon S3 Documentation](https://docs.aws.amazon.com/s3/) - S3 service documentation
|
|
515
|
+
- [Amazon CloudFront Documentation](https://docs.aws.amazon.com/cloudfront/) - CloudFront CDN documentation
|
|
516
|
+
- [AWS Certificate Manager](https://docs.aws.amazon.com/acm/) - SSL/TLS certificate management
|
|
517
|
+
- [Amazon Route53](https://docs.aws.amazon.com/route53/) - DNS service documentation
|
|
518
|
+
|
|
519
|
+
### Package Information
|
|
520
|
+
|
|
521
|
+
- [NPM Package](https://www.npmjs.com/package/@codedrifters/constructs) - View on NPM
|
|
522
|
+
- [GitHub Repository](https://github.com/codedrifters/packages) - Source code
|
|
523
|
+
|
|
524
|
+
---
|
|
525
|
+
|
|
526
|
+
**Note:** This package is designed for use with AWS CDK v2. Make sure you're using compatible versions of `aws-cdk-lib` and `constructs`.
|
package/package.json
CHANGED