-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added distributions, websites and cnames
- Loading branch information
1 parent
c58c375
commit 03cbcd6
Showing
5 changed files
with
133 additions
and
27 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,28 +1,106 @@ | ||
import { DistributionProperties, Distribution } from '../../aws/cloudfront/Distribution'; | ||
import { RecordSet } from '../../aws/route53/RecordSet'; | ||
import {Bucket} from "../../aws/s3/Bucket"; | ||
import {BucketPolicy} from "../../aws/s3/BucketPolicy"; | ||
import { AWSResourcesFor } from "../aws"; | ||
import { Policy } from '../iam/PolicyDocument'; | ||
|
||
import {join, Value} from "../Value"; | ||
|
||
export type WebsiteExpects = AWSResourcesFor<'s3' | 'cloudfront' | 'route53'> | ||
export class Website { | ||
|
||
bucket: Bucket; | ||
bucketPolicy: BucketPolicy; | ||
|
||
private constructor(bucket: Bucket, bucketPolicy: BucketPolicy) { | ||
this.bucket = bucket; | ||
this.bucketPolicy = bucketPolicy; | ||
|
||
private constructor( | ||
private aws: WebsiteExpects, | ||
public bucket: Bucket, | ||
public bucketPolicy: BucketPolicy, | ||
public distribution?: Distribution, | ||
public aRecord?: RecordSet, | ||
) { | ||
} | ||
|
||
withARecord(domain: Value<string>, hostedZoneId: Value<string>) { | ||
if(!this.distribution) throw new Error('You must call withDistribution to create A record'); | ||
this.aRecord = this.aws.route53.recordSet({ | ||
name: domain, | ||
type: 'A', | ||
hostedZoneId, | ||
aliasTarget: { | ||
dNSName: this.distribution!.attributes.DomainName(), | ||
hostedZoneId: this.aws.cloudFrontHostedZoneId | ||
} | ||
}); | ||
} | ||
|
||
withDistribution( | ||
originId: Value<string>, | ||
certificateArn: Value<string>, | ||
aliases: Value<Value<string>[]>, | ||
rootObject: Value<string> = 'index.html', | ||
httpVersion: DistributionProperties['distributionConfig']['httpVersion'] = 'http2', | ||
edgeLambdaArn?: Value<string> | ||
): this { | ||
this.distribution = this.aws.cloudfront.distribution({ | ||
distributionConfig: { | ||
aliases, | ||
customErrorResponses: [{ errorCode: 404, responseCode: 200, responsePagePath: '/' }], | ||
defaultCacheBehavior: { | ||
cachePolicyId: '658327ea-f89d-4fab-a63d-7e88639e58f6', | ||
compress: false, | ||
...(edgeLambdaArn ? { lambdaFunctionAssociations: [{ | ||
eventType: 'viewer-request', | ||
lambdaFunctionARN: edgeLambdaArn, | ||
}] } : {}), | ||
responseHeadersPolicyId: '67f7725c-6f97-4210-82d7-5512b31e9d03', | ||
targetOriginId: originId, | ||
viewerProtocolPolicy: 'redirect-to-https' | ||
}, | ||
defaultRootObject: rootObject, | ||
enabled: true, | ||
httpVersion: httpVersion, | ||
iPV6Enabled: true, | ||
origins: [ | ||
{ | ||
id: originId, | ||
customOriginConfig: { | ||
originProtocolPolicy: 'http-only', | ||
originSSLProtocols: [ 'TLSv1.2' ], | ||
}, | ||
domainName: this.aws.functions.select(2, this.aws.functions.split('/', this.bucket.attributes.WebsiteURL())), | ||
} | ||
], | ||
viewerCertificate: { | ||
acmCertificateArn: certificateArn, | ||
minimumProtocolVersion: 'TLSv1.2_2021', | ||
sslSupportMethod: 'sni-only' | ||
} | ||
} | ||
}); | ||
return this; | ||
} | ||
|
||
static create(aws: AWSResourcesFor<'s3'>, indexDocument: Value<string> = 'index.html', errorDocument: Value<string> = indexDocument): Website { | ||
static create( | ||
aws: WebsiteExpects, | ||
bucketName: Value<string>, | ||
indexDocument: Value<string> = 'index.html', | ||
errorDocument: Value<string> = indexDocument | ||
): Website { | ||
const bucket = aws.s3.bucket({ | ||
accessControl: 'PublicRead', | ||
websiteConfiguration: { indexDocument, errorDocument } | ||
bucketName: bucketName, | ||
websiteConfiguration: { indexDocument, errorDocument }, | ||
publicAccessBlockConfiguration: { | ||
blockPublicAcls: false, | ||
blockPublicPolicy: false, | ||
ignorePublicAcls: false, | ||
restrictPublicBuckets: false | ||
} | ||
}); | ||
|
||
const policy = aws.s3.bucketPolicy({ | ||
bucket: bucket, | ||
policyDocument: { statement: [{action: 's3:GetObject', effect: 'Allow', resource: [join('arn:aws:s3:::', bucket, '/*')], principal: '*'}] } | ||
policyDocument: Policy.allow('s3:GetObject', [join(bucket.attributes.Arn(), '/*')], { principal: { AWS: '*' } }).document | ||
}); | ||
return new Website(bucket, policy); | ||
|
||
return new Website(aws, bucket, policy); | ||
} | ||
} |
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