-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
388: Add UI stack and hook up CORS origin (#819)
- Loading branch information
1 parent
94688aa
commit 7bd3b5b
Showing
5 changed files
with
123 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './resourceNamingUtils'; | ||
export { ApiStack } from './api-stack'; | ||
export { AuthStack } from './auth-stack'; | ||
export { UiStack } from './ui-stack'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { | ||
AllowedMethods, | ||
CacheCookieBehavior, | ||
CachePolicy, | ||
Distribution, | ||
OriginAccessIdentity, | ||
SecurityPolicyProtocol, | ||
ViewerProtocolPolicy, | ||
} from 'aws-cdk-lib/aws-cloudfront'; | ||
import { S3Origin } from 'aws-cdk-lib/aws-cloudfront-origins'; | ||
import { | ||
CfnOutput, | ||
Duration, | ||
RemovalPolicy, | ||
Stack, | ||
StackProps, | ||
} from 'aws-cdk-lib/core'; | ||
import * as iam from 'aws-cdk-lib/aws-iam'; | ||
import { | ||
BlockPublicAccess, | ||
Bucket, | ||
BucketEncryption, | ||
} from 'aws-cdk-lib/aws-s3'; | ||
import { Construct } from 'constructs'; | ||
|
||
import { resourceName } from './resourceNamingUtils'; | ||
|
||
export class UiStack extends Stack { | ||
public readonly cloudfrontUrl: string; | ||
|
||
constructor(scope: Construct, id: string, props: StackProps) { | ||
super(scope, id, props); | ||
|
||
const generateResourceName = resourceName(scope); | ||
|
||
// allow s3 to be secured | ||
const cloudfrontOAI = new OriginAccessIdentity( | ||
this, | ||
generateResourceName('cloudfront-OAI') | ||
); | ||
|
||
//HostBucket | ||
const bucketName = generateResourceName('host-bucket'); | ||
const hostBucket = new Bucket(this, bucketName, { | ||
bucketName, | ||
blockPublicAccess: BlockPublicAccess.BLOCK_ALL, | ||
encryption: BucketEncryption.S3_MANAGED, | ||
versioned: false, | ||
removalPolicy: RemovalPolicy.DESTROY, | ||
autoDeleteObjects: true, | ||
}); | ||
hostBucket.addToResourcePolicy( | ||
new iam.PolicyStatement({ | ||
actions: ['s3:GetObject'], | ||
resources: [hostBucket.arnForObjects('*')], | ||
principals: [ | ||
new iam.CanonicalUserPrincipal( | ||
cloudfrontOAI.cloudFrontOriginAccessIdentityS3CanonicalUserId | ||
), | ||
], | ||
}) | ||
); | ||
|
||
//CloudFront | ||
const cachePolicyName = generateResourceName('site-cache-policy'); | ||
const cloudFront = new Distribution( | ||
this, | ||
generateResourceName('site-distribution'), | ||
{ | ||
defaultRootObject: 'index.html', | ||
minimumProtocolVersion: SecurityPolicyProtocol.TLS_V1_2_2021, | ||
errorResponses: [ | ||
{ | ||
httpStatus: 404, | ||
responseHttpStatus: 200, | ||
responsePagePath: '/index.html', | ||
ttl: Duration.seconds(30), | ||
}, | ||
], | ||
defaultBehavior: { | ||
origin: new S3Origin(hostBucket, { | ||
originAccessIdentity: cloudfrontOAI, | ||
}), | ||
cachePolicy: new CachePolicy(this, cachePolicyName, { | ||
cachePolicyName, | ||
cookieBehavior: CacheCookieBehavior.allowList( | ||
'prompt-injection.sid' | ||
), | ||
}), | ||
compress: true, | ||
allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS, | ||
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS, | ||
}, | ||
} | ||
); | ||
this.cloudfrontUrl = `https://${cloudFront.domainName}`; | ||
|
||
new CfnOutput(this, 'WebURL', { | ||
value: this.cloudfrontUrl, | ||
}); | ||
} | ||
} |