Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ coverage
dist
dist-ssr
*.local
*.d.ts

# Editor directories and files
.vscode/*
Expand All @@ -24,3 +25,7 @@ dist-ssr
*.njsproj
*.sln
*.sw?

# CDK asset staging directory
.cdk.staging
cdk.out
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.ts
!*.d.ts

# CDK asset staging directory
.cdk.staging
cdk.out
Empty file added Agent
Empty file.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [CloudFront](https://d2b96icd4snjtl.cloudfront.net/)

## [S3 Bucket](http://reactshopfrontend-alexmlogicalbucketidb65779d4-px3z7nuunokf.s3-website.eu-central-1.amazonaws.com/)

---

# React-shop-cloudfront

This is frontend starter project for nodejs-aws mentoring program. It uses the following technologies:
Expand Down
13 changes: 13 additions & 0 deletions bin/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { FrontendStack } from '../lib/frontend-stack';

const app = new cdk.App();
new FrontendStack(app, 'ReactShopFrontend', {
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION || 'eu-central-1'
},
description: 'React Shop Frontend Infrastructure'
});
19 changes: 19 additions & 0 deletions cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"app": "npx tsx bin/app.ts",
"watch": {
"include": [
"**"
],
"exclude": [
"README.md",
"cdk*.json",
"**/*.d.ts",
"**/*.js",
"tsconfig.json",
"package*.json",
"yarn.lock",
"node_modules",
"test"
]
}
}
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/src/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My shop</title>
<title>4 shopping</title>
</head>
<body>
<div id="app"></div>
Expand Down
57 changes: 57 additions & 0 deletions lib/frontend-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import * as origins from 'aws-cdk-lib/aws-cloudfront-origins';
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
import { RemovalPolicy } from 'aws-cdk-lib';

export class FrontendStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);

// Create an S3 bucket for the website
const websiteBucket = new s3.Bucket(this, 'alexm-logical-bucket-id', {
autoDeleteObjects: true,
removalPolicy: RemovalPolicy.DESTROY,
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL
});

// Create a CloudFront distribution
const distribution = new cloudfront.Distribution(this, 'alexm-distribution-id', {
defaultBehavior: {
origin: origins.S3BucketOrigin.withOriginAccessControl(websiteBucket),
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED,
compress: true,
},
defaultRootObject: 'index.html',
errorResponses: [
{
httpStatus: 404,
responseHttpStatus: 200,
responsePagePath: '/index.html',
},
{
httpStatus: 403,
responseHttpStatus: 200,
responsePagePath: '/index.html',
}
]
});

// Deploy the React application to S3
new s3deploy.BucketDeployment(this, 'alexm-deployment-id', {
sources: [s3deploy.Source.asset('./dist')],
destinationBucket: websiteBucket,
distribution,
distributionPaths: ['/*'],
prune: true
});

// Output the CloudFront URL
new cdk.CfnOutput(this, 'CloudFrontURL', {
value: `https://${distribution.distributionDomainName}`,
description: 'URL for website',
});
}
}
Loading