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

Use Lambda URL as CloudFront origin #368

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 19 additions & 6 deletions src/constructs/aws/ServerSideWebsite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const SCHEMA = {
properties: {
type: { const: "server-side-website" },
apiGateway: { enum: ["http", "rest"] },
functionName: { type: "string" },
assets: {
type: "object",
additionalProperties: { type: "string" },
Expand Down Expand Up @@ -115,11 +116,23 @@ export class ServerSideWebsite extends AwsConstruct {
})();
const backendCachePolicy = CachePolicy.CACHING_DISABLED;

const apiId =
configuration.apiGateway === "rest"
? this.provider.naming.getRestApiLogicalId()
: this.provider.naming.getHttpApiLogicalId();
const apiGatewayDomain = Fn.join(".", [Fn.ref(apiId), `execute-api.${this.provider.region}.amazonaws.com`]);
// Resolve domain to use as cloudfront origin
const originDomain = (() => {
if (configuration.functionName !== void 0) {
kevincerro marked this conversation as resolved.
Show resolved Hide resolved
// Use Lambda Url
const lambdaUrlId = this.provider.naming.getLambdaFunctionUrlLogicalId(configuration.functionName);

return Fn.select(2, Fn.split("/", Fn.getAtt(lambdaUrlId, "FunctionUrl").toString()));
kevincerro marked this conversation as resolved.
Show resolved Hide resolved
} else {
// Use API Gateway
const apiId =
configuration.apiGateway === "rest"
? this.provider.naming.getRestApiLogicalId()
: this.provider.naming.getHttpApiLogicalId();

return Fn.join(".", [Fn.ref(apiId), `execute-api.${this.provider.region}.amazonaws.com`]);
kevincerro marked this conversation as resolved.
Show resolved Hide resolved
}
})();

// Cast the domains to an array
this.domains = configuration.domain !== undefined ? flatten([configuration.domain]) : undefined;
Expand All @@ -132,7 +145,7 @@ export class ServerSideWebsite extends AwsConstruct {
comment: `${provider.stackName} ${id} website CDN`,
defaultBehavior: {
// Origins are where CloudFront fetches content
origin: new HttpOrigin(apiGatewayDomain, {
origin: new HttpOrigin(originDomain, {
// API Gateway only supports HTTPS
protocolPolicy: OriginProtocolPolicy.HTTPS_ONLY,
}),
Expand Down
1 change: 1 addition & 0 deletions src/providers/AwsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class AwsProvider implements ProviderInterface {
public naming: {
getStackName: () => string;
getLambdaLogicalId: (functionName: string) => string;
getLambdaFunctionUrlLogicalId: (functionName: string) => string;
getRestApiLogicalId: () => string;
getHttpApiLogicalId: () => string;
};
Expand Down
1 change: 1 addition & 0 deletions src/types/serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Provider = {
naming: {
getStackName: () => string;
getLambdaLogicalId: (functionName: string) => string;
getLambdaFunctionUrlLogicalId: (functionName: string) => string;
getRestApiLogicalId: () => string;
getHttpApiLogicalId: () => string;
getCompiledTemplateFileName: () => string;
Expand Down