Skip to content

Commit

Permalink
feat: expose CloudFront distribution as property (#18)
Browse files Browse the repository at this point in the history
- Add public readonly distribution property to Hosting construct

- Expose CloudFront distribution from HostingInfrastructure

- Update documentation with new property
  • Loading branch information
cornelcroi committed Oct 24, 2024
1 parent 7794c19 commit a828dd6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/hosting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class Hosting extends Construct {
/**
* The S3 Bucket used for hosting the content.
*/
public readonly hostingBucket: s3.Bucket;
public readonly hostingBucket: s3.IBucket;

/**
* The CloudFront Function used for URI manipulation.
Expand Down
23 changes: 12 additions & 11 deletions lib/hosting_infrastructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ interface IConfigProps {


export class HostingInfrastructure extends Construct {

public readonly hostingBucket: IBucket;
public readonly distribution: cloudfront.Distribution;



Expand Down Expand Up @@ -77,7 +79,7 @@ export class HostingInfrastructure extends Construct {
},
]);
*/
const hostingBucket = new s3.Bucket(this, "HostingBucket", {
this.hostingBucket = new s3.Bucket(this, "HostingBucket", {
versioned: false,
...(s3Logs ? { serverAccessLogsBucket: s3Logs } : {}),
enforceSSL: true,
Expand All @@ -90,9 +92,8 @@ export class HostingInfrastructure extends Construct {
}),
});

this.hostingBucket = hostingBucket;

const s3origin = new origins.S3Origin(hostingBucket);
const s3origin = new origins.S3Origin(this.hostingBucket);

const myResponseHeadersPolicy = new cloudfront.ResponseHeadersPolicy(
this,
Expand Down Expand Up @@ -246,7 +247,7 @@ export class HostingInfrastructure extends Construct {



const distribution = new cloudfront.Distribution(this, "Distribution", {
this.distribution = new cloudfront.Distribution(this, "Distribution", {
comment: "Static hosting - " + Aws.STACK_NAME,
defaultRootObject: "index.html",
httpVersion: cloudfront.HttpVersion.HTTP2_AND_3,
Expand Down Expand Up @@ -279,7 +280,7 @@ export class HostingInfrastructure extends Construct {
: {}),
});

NagSuppressions.addResourceSuppressions(distribution, [
NagSuppressions.addResourceSuppressions(this.distribution, [
{
id: "AwsSolutions-CFR4",
reason:
Expand All @@ -290,7 +291,7 @@ export class HostingInfrastructure extends Construct {
//OAC is not implemented in CDK so tweaking is required:eplace OAI par OAC
//https://github.com/aws/aws-cdk/issues/21771

const cfnDistribution = distribution.node.defaultChild as CfnDistribution;
const cfnDistribution = this.distribution.node.defaultChild as CfnDistribution;
cfnDistribution.addOverride(
"Properties.DistributionConfig.Origins.0.S3OriginConfig.OriginAccessIdentity",
""
Expand All @@ -300,7 +301,7 @@ export class HostingInfrastructure extends Construct {
oac.getAtt("Id")
);

const comS3PolicyOverride = hostingBucket.node.findChild("Policy").node
const comS3PolicyOverride = this.hostingBucket.node.findChild("Policy").node
.defaultChild as CfnBucketPolicy;
const statement = comS3PolicyOverride.policyDocument.statements[1];
if (statement["_principal"] && statement["_principal"].CanonicalUser) {
Expand All @@ -318,30 +319,30 @@ export class HostingInfrastructure extends Construct {
service: "cloudfront",
region: "",
resource: "distribution",
resourceName: distribution.distributionId,
resourceName: this.distribution.distributionId,
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
}),
},
}
);

const s3OriginNode = distribution.node
const s3OriginNode = this.distribution.node
.findAll()
//.filter((child) => child.node.id === "S3Origin");
.filter((child) => child.node.id === "S3Origin");
s3OriginNode[0].node.tryRemoveChild("Resource");
//End of tweaking for OAC is not implemented in CDK so tweaking is required

new CfnOutput(this, "DomainName", {
value: "https://" + distribution.domainName,
value: "https://" + this.distribution.domainName,
});

const stackName = calculateMainStackName(params.hostingConfiguration);


new ssm.StringParameter(this, 'SSMConnectionRegion', {
parameterName: '/' + stackName + '/' + SSM_DOMAIN_STR,
stringValue: distribution.domainName,
stringValue: this.distribution.domainName,
});


Expand Down

0 comments on commit a828dd6

Please sign in to comment.