|
| 1 | +import * as s3 from "@aws-cdk/aws-s3"; |
| 2 | +import * as iam from "@aws-cdk/aws-iam"; |
| 3 | +import * as cdk from "@aws-cdk/core"; |
| 4 | +import * as cloudFront from "@aws-cdk/aws-cloudfront"; |
| 5 | +import * as deploy from "@aws-cdk/aws-s3-deployment"; |
| 6 | + |
| 7 | +const APP_PREFIX = "bw-nodejs-aws-shop-react"; |
| 8 | + |
| 9 | +const app = new cdk.App(); |
| 10 | + |
| 11 | +const stack = new cdk.Stack(app, `${APP_PREFIX}-stack`, { |
| 12 | + description: `This stack includes resources needed to deploy ${APP_PREFIX} application`, |
| 13 | +}); |
| 14 | + |
| 15 | +const bucket = new s3.Bucket(stack, `${APP_PREFIX}-bucket`, { |
| 16 | + bucketName: "bw-nodejs-aws-shop-react-cdk", |
| 17 | + websiteIndexDocument: "index.html", |
| 18 | + publicReadAccess: false, |
| 19 | + blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL, |
| 20 | +}); |
| 21 | + |
| 22 | +const oai = new cloudFront.OriginAccessIdentity(stack, `${APP_PREFIX}-oai`); |
| 23 | + |
| 24 | +bucket.addToResourcePolicy( |
| 25 | + new iam.PolicyStatement({ |
| 26 | + actions: ["s3:GetObject"], |
| 27 | + resources: [bucket.arnForObjects("*")], |
| 28 | + principals: [ |
| 29 | + new iam.CanonicalUserPrincipal( |
| 30 | + oai.cloudFrontOriginAccessIdentityS3CanonicalUserId |
| 31 | + ), |
| 32 | + ], |
| 33 | + }) |
| 34 | +); |
| 35 | + |
| 36 | +const distribution = new cloudFront.CloudFrontWebDistribution( |
| 37 | + stack, |
| 38 | + `${APP_PREFIX}-distribution`, |
| 39 | + { |
| 40 | + comment: `${APP_PREFIX} distribution`, |
| 41 | + originConfigs: [ |
| 42 | + { |
| 43 | + s3OriginSource: { |
| 44 | + s3BucketSource: bucket, |
| 45 | + originAccessIdentity: oai, |
| 46 | + }, |
| 47 | + behaviors: [ |
| 48 | + { |
| 49 | + isDefaultBehavior: true, |
| 50 | + }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + ], |
| 54 | + } |
| 55 | +); |
| 56 | + |
| 57 | +new deploy.BucketDeployment(stack, `${APP_PREFIX}-deployment`, { |
| 58 | + sources: [deploy.Source.asset("./dist")], |
| 59 | + destinationBucket: bucket, |
| 60 | + distribution, |
| 61 | + distributionPaths: ["/index.html", "/assets/*"], |
| 62 | +}); |
| 63 | + |
| 64 | +app.synth(); |
0 commit comments