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

feat(sdk): adding cloud.Website to awscdk #4267

Merged
merged 1 commit into from
Sep 24, 2023
Merged
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
5 changes: 5 additions & 0 deletions libs/wingsdk/src/target-awscdk/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Secret } from "./secret";
import { TestRunner } from "./test-runner";
import { CdkTokens } from "./tokens";
import { Topic } from "./topic";
import { Website } from "./website";

import {
BUCKET_FQN,
Expand All @@ -24,6 +25,7 @@ import {
SECRET_FQN,
TOPIC_FQN,
SCHEDULE_FQN,
WEBSITE_FQN,
} from "../cloud";
import {
App as CoreApp,
Expand Down Expand Up @@ -180,6 +182,9 @@ export class App extends CoreApp {

case ON_DEPLOY_FQN:
return new OnDeploy(scope, id, args[0], args[1]);

case WEBSITE_FQN:
return new Website(scope, id, args[0]);
}
return undefined;
}
Expand Down
26 changes: 17 additions & 9 deletions libs/wingsdk/src/target-awscdk/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,7 @@ export class Bucket extends cloud.Bucket {

this.public = props.public ?? false;

const isTestEnvironment = App.of(scope).isTestEnvironment;

this.bucket = new S3Bucket(this, "Default", {
encryption: BucketEncryption.S3_MANAGED,
blockPublicAccess: this.public ? undefined : BlockPublicAccess.BLOCK_ALL,
publicReadAccess: this.public ? true : false,
removalPolicy: RemovalPolicy.DESTROY,
autoDeleteObjects: isTestEnvironment ? true : false,
});
this.bucket = createEncryptedBucket(this, this.public);
}

public addObject(key: string, body: string): void {
Expand Down Expand Up @@ -217,3 +209,19 @@ export class Bucket extends cloud.Bucket {
return `BUCKET_NAME_${this.node.addr.slice(-8)}`;
}
}

export function createEncryptedBucket(
scope: Construct,
isPublic: boolean,
name: string = "Default"
): S3Bucket {
const isTestEnvironment = App.of(scope).isTestEnvironment;

return new S3Bucket(scope, name, {
encryption: BucketEncryption.S3_MANAGED,
blockPublicAccess: isPublic ? undefined : BlockPublicAccess.BLOCK_ALL,
publicReadAccess: isPublic ? true : false,
removalPolicy: RemovalPolicy.DESTROY,
autoDeleteObjects: isTestEnvironment ? true : false,
});
}
94 changes: 94 additions & 0 deletions libs/wingsdk/src/target-awscdk/website.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { posix, sep } from "path";
import { Distribution, OriginAccessIdentity } from "aws-cdk-lib/aws-cloudfront";
import { S3Origin } from "aws-cdk-lib/aws-cloudfront-origins";
import { CanonicalUserPrincipal, PolicyStatement } from "aws-cdk-lib/aws-iam";
import { Bucket as S3Bucket } from "aws-cdk-lib/aws-s3";
import { BucketDeployment, Source } from "aws-cdk-lib/aws-s3-deployment";
import { Construct } from "constructs";
import { createEncryptedBucket } from "./bucket";
import { core } from "..";
import * as cloud from "../cloud";
import { Json } from "../std";

const INDEX_FILE = "index.html";

/**
* AWS implementation of `cloud.Website`.
*
* @inflight `@winglang/sdk.cloud.IWebsiteClient`
*/
export class Website extends cloud.Website {
private readonly bucket: S3Bucket;
private readonly _url: string;

constructor(scope: Construct, id: string, props: cloud.WebsiteProps) {
super(scope, id, props);

this.bucket = createEncryptedBucket(this, false, "WebsiteBucket");

new BucketDeployment(this, "BucketWebsiteConfiguration", {
destinationBucket: this.bucket,
sources: [Source.asset(this.path)],
});

const cloudFrontOAI = new OriginAccessIdentity(this, "CloudFrontOAI");

this.bucket.addToResourcePolicy(
new PolicyStatement({
actions: ["s3:GetObject"],
resources: [this.bucket.arnForObjects("*")],
principals: [
new CanonicalUserPrincipal(
cloudFrontOAI.cloudFrontOriginAccessIdentityS3CanonicalUserId
),
],
})
);

// create a cloudFront distribution
const distribution = new Distribution(this, "Distribution", {
defaultBehavior: {
origin: new S3Origin(this.bucket, {
originAccessIdentity: cloudFrontOAI,
}),
},
domainNames: this._domain ? [this._domain] : undefined,
defaultRootObject: INDEX_FILE,
});

this._url = `https://${distribution.domainName}`;
}

public get url(): string {
return this._url;
}

public addJson(path: string, data: Json): string {
if (!path.endsWith(".json")) {
throw new Error(
`key must have a .json suffix. (current: "${path.split(".").pop()}")`
);
}

new BucketDeployment(this, `S3Object-${path}`, {
destinationBucket: this.bucket,
sources: [Source.data(this.formatPath(path), JSON.stringify(data))],
});

return `${this.url}/${path}`;
}

private formatPath(path: string): string {
return path.split(sep).join(posix.sep);
}

/** @internal */
public _toInflight(): string {
return core.InflightClient.for(
__dirname.replace("target-awscdk", "shared-aws"),
__filename,
"WebsiteClient",
[]
);
}
}
Loading
Loading