Skip to content
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

Task 8 #77

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ dist-ssr
*.njsproj
*.sln
*.sw?

cdk
cdk.out
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# React-shop-cloudfront

## Task 2

- Link to S3-website: http://aws-2023-q4-course.s3-website-eu-west-1.amazonaws.com
- Link to CloudFront: https://d6yi9zf2tot78.cloudfront.net

- Link to APP that was created using CDK and another bucket: https://d37k2ipkdi7gkj.cloudfront.net
- Steps:
- install cdk-lib
- create cdk folder
- enter cdk folder
- init cdk with 'cdk init app --language=typescript'
- add scripts to cdk.json
- add cdk.ts file
- run 'cdk bootstrap'
- run 'cdk deploy'

# Technologies
This is frontend starter project for nodejs-aws mentoring program. It uses the following technologies:

- [Vite](https://vitejs.dev/) as a project bundler
Expand Down
8 changes: 8 additions & 0 deletions cdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.js
!jest.config.js
*.d.ts
node_modules

# CDK asset staging directory
.cdk.staging
cdk.out
6 changes: 6 additions & 0 deletions cdk/.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
14 changes: 14 additions & 0 deletions cdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Welcome to your CDK TypeScript project

This is a blank project for CDK development with TypeScript.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

## Useful commands

* `npm run build` compile typescript to js
* `npm run watch` watch for changes and compile
* `npm run test` perform the jest unit tests
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk synth` emits the synthesized CloudFormation template
21 changes: 21 additions & 0 deletions cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { CdkStack } from '../lib/cdk-stack';

const app = new cdk.App();
new CdkStack(app, 'CdkStack', {
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */

/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },

/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },

/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});
7 changes: 7 additions & 0 deletions cdk/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"app": "npx ts-node -P tsconfig.json --prefer-ts-exts cdk.ts",
"watch": {
"include": ["cdk.ts"]
},
"context": {}
}
49 changes: 49 additions & 0 deletions cdk/cdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import * as origins from 'aws-cdk-lib/aws-cloudfront-origins';

const app = new cdk.App();

const stack = new cdk.Stack(app, "AwsCourseAlexStack", {
env: { region: 'eu-west-1' }
})

const bucket = new s3.Bucket(stack, "AwsCourseAlexBucket", {
bucketName: "rs-aws-course-alex-bucket"
})

const originAccessIdentity = new cloudfront.OriginAccessIdentity(stack, "AwsCourseAlexOAI", {
comment: bucket.bucketName
})

bucket.grantRead(originAccessIdentity)

const clfront = new cloudfront.Distribution(stack, "AwsCourseAlexDistribution", {
defaultBehavior: {
origin: new origins.S3Origin(bucket, {
originAccessIdentity
}),
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
},
defaultRootObject: "index.html",
errorResponses: [
{
httpStatus: 404,
responseHttpStatus: 200,
responsePagePath: "/index.html",
},
],
})

new s3deploy.BucketDeployment(stack, "DeployAwsCourseAlex", {
destinationBucket: bucket,
sources: [s3deploy.Source.asset("../dist")],
distribution: clfront,
distributionPaths: ["/*"],
})

new cdk.CfnOutput(stack, "Domain URL", {
value: clfront.distributionDomainName
})
8 changes: 8 additions & 0 deletions cdk/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
};
16 changes: 16 additions & 0 deletions cdk/lib/cdk-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
// import * as sqs from 'aws-cdk-lib/aws-sqs';

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

// The code that defines your stack goes here

// example resource
// const queue = new sqs.Queue(this, 'CdkQueue', {
// visibilityTimeout: cdk.Duration.seconds(300)
// });
}
}
Loading