Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
dns: use ALIAS for apex records for Vercel domains
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang committed Aug 11, 2024
1 parent 603751b commit 55524f5
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 236 deletions.
30 changes: 9 additions & 21 deletions platform/src/components/aws/apigateway-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,27 +381,15 @@ export class ApiGatewayWebSocket extends Component implements Link.Linkable {
domain.dns.apply((dns) => {
if (!dns) return;

if (dns.provider === "aws") {
dns.createAliasRecords(
name,
{
name: domain.name,
aliasName: apigDomain.domainNameConfiguration.targetDomainName,
aliasZone: apigDomain.domainNameConfiguration.hostedZoneId,
},
{ parent },
);
} else {
dns.createRecord(
name,
{
type: "CNAME",
name: domain.name,
value: apigDomain.domainNameConfiguration.targetDomainName,
},
{ parent },
);
}
dns.createAlias(
name,
{
name: domain.name,
aliasName: apigDomain.domainNameConfiguration.targetDomainName,
aliasZone: apigDomain.domainNameConfiguration.hostedZoneId,
},
{ parent },
);
});
}

Expand Down
30 changes: 9 additions & 21 deletions platform/src/components/aws/apigatewayv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,27 +739,15 @@ export class ApiGatewayV2 extends Component implements Link.Linkable {
domain.dns.apply((dns) => {
if (!dns) return;

if (dns.provider === "aws") {
dns.createAliasRecords(
name,
{
name: domain.name,
aliasName: apigDomain.domainNameConfiguration.targetDomainName,
aliasZone: apigDomain.domainNameConfiguration.hostedZoneId,
},
{ parent },
);
} else {
dns.createRecord(
name,
{
type: "CNAME",
name: domain.name,
value: apigDomain.domainNameConfiguration.targetDomainName,
},
{ parent },
);
}
dns.createAlias(
name,
{
name: domain.name,
aliasName: apigDomain.domainNameConfiguration.targetDomainName,
aliasZone: apigDomain.domainNameConfiguration.hostedZoneId,
},
{ parent },
);
});
}

Expand Down
30 changes: 9 additions & 21 deletions platform/src/components/aws/app-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,27 +601,15 @@ export class AppSync extends Component implements Link.Linkable {
domain.apply((domain) => {
if (!domain.dns) return;

if (domain.dns.provider === "aws") {
domain.dns.createAliasRecords(
name,
{
name: domain.name,
aliasName: domainName.appsyncDomainName,
aliasZone: domainName.hostedZoneId,
},
{ parent },
);
} else {
domain.dns.createRecord(
name,
{
type: "CNAME",
name: domain.name,
value: domainName.appsyncDomainName,
},
{ parent },
);
}
domain.dns.createAlias(
name,
{
name: domain.name,
aliasName: domainName.appsyncDomainName,
aliasZone: domainName.hostedZoneId,
},
{ parent },
);
});
}
}
Expand Down
154 changes: 71 additions & 83 deletions platform/src/components/aws/cdn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,59 +257,59 @@ export interface CdnArgs {
invalidation?: Input<
| boolean
| {
/**
* Configure if `sst deploy` should wait for the CloudFront cache invalidation to finish.
*
* :::tip
* For non-prod environments it might make sense to pass in `false`.
* :::
*
* Waiting for this process to finish ensures that new content will be available after the deploy finishes. However, this process can sometimes take more than 5 mins.
* @default `false`
* @example
* ```js
* {
* invalidation: {
* wait: true
* }
* }
* ```
*/
wait?: Input<boolean>;
/**
* The invalidation token is used to determine if the cache should be invalidated. If the
* token is the same as the previous deployment, the cache will not be invalidated.
*
* @default A unique value is auto-generated on each deploy
* @example
* ```js
* {
* invalidation: {
* token: "foo123"
* }
* }
* ```
*/
token?: Input<string>;
/**
* Specify an array of glob pattern of paths to invalidate.
*
* :::note
* Each glob pattern counts as a single invalidation. However, invalidating `/*` counts as a single invalidation as well.
* :::
* @default `["/*"]`
* @example
* Invalidate the `index.html` and all files under the `products/` route. This counts as two invalidations.
* ```js
* {
* invalidation: {
* paths: ["/index.html", "/products/*"]
* }
* }
* ```
*/
paths?: Input<Input<string>[]>;
}
/**
* Configure if `sst deploy` should wait for the CloudFront cache invalidation to finish.
*
* :::tip
* For non-prod environments it might make sense to pass in `false`.
* :::
*
* Waiting for this process to finish ensures that new content will be available after the deploy finishes. However, this process can sometimes take more than 5 mins.
* @default `false`
* @example
* ```js
* {
* invalidation: {
* wait: true
* }
* }
* ```
*/
wait?: Input<boolean>;
/**
* The invalidation token is used to determine if the cache should be invalidated. If the
* token is the same as the previous deployment, the cache will not be invalidated.
*
* @default A unique value is auto-generated on each deploy
* @example
* ```js
* {
* invalidation: {
* token: "foo123"
* }
* }
* ```
*/
token?: Input<string>;
/**
* Specify an array of glob pattern of paths to invalidate.
*
* :::note
* Each glob pattern counts as a single invalidation. However, invalidating `/*` counts as a single invalidation as well.
* :::
* @default `["/*"]`
* @example
* Invalidate the `index.html` and all files under the `products/` route. This counts as two invalidations.
* ```js
* {
* invalidation: {
* paths: ["/index.html", "/products/*"]
* }
* }
* ```
*/
paths?: Input<Input<string>[]>;
}
>;
/**
* [Transform](/docs/components#transform) how this component creates its underlying resources.
Expand Down Expand Up @@ -448,19 +448,19 @@ export class Cdn extends Component {
},
aliases: domain
? output(domain).apply((domain) => [
domain.name,
...domain.aliases,
])
domain.name,
...domain.aliases,
])
: [],
viewerCertificate: certificateArn
? {
acmCertificateArn: certificateArn,
sslSupportMethod: "sni-only",
minimumProtocolVersion: "TLSv1.2_2021",
}
acmCertificateArn: certificateArn,
sslSupportMethod: "sni-only",
minimumProtocolVersion: "TLSv1.2_2021",
}
: {
cloudfrontDefaultCertificate: true,
},
cloudfrontDefaultCertificate: true,
},
waitForDeployment: false,
},
{ parent },
Expand Down Expand Up @@ -504,27 +504,15 @@ export class Cdn extends Component {
if (!domain.dns) return;

for (const recordName of [domain.name, ...domain.aliases]) {
if (domain.dns.provider === "aws") {
domain.dns.createAliasRecords(
name,
{
name: recordName,
aliasName: distribution.domainName,
aliasZone: distribution.hostedZoneId,
},
{ parent },
);
} else {
domain.dns.createRecord(
name,
{
type: "CNAME",
name: recordName,
value: distribution.domainName,
},
{ parent },
);
}
domain.dns.createAlias(
name,
{
name: recordName,
aliasName: distribution.domainName,
aliasZone: distribution.hostedZoneId,
},
{ parent },
);
}
});
}
Expand Down
52 changes: 26 additions & 26 deletions platform/src/components/aws/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,42 +93,18 @@ export interface DnsArgs {
export function dns(args: DnsArgs = {}) {
return {
provider: "aws",
createAlias,
createRecord,
createAliasRecords,
} satisfies Dns;

/**
* Creates a DNS record in the hosted zone.
*
* @param namePrefix The prefix to use for the resource names.
* @param record The DNS record to create.
* @param opts The component resource options.
*/
function createRecord(
namePrefix: string,
record: Record,
opts: ComponentResourceOptions,
) {
return _createRecord(
namePrefix,
{
type: record.type,
name: record.name,
ttl: 60,
records: [record.value],
},
opts,
);
}

/**
* Creates alias records in the hosted zone.
*
* @param namePrefix The prefix to use for the resource names.
* @param record The alias record to create.
* @param opts The component resource options.
*/
function createAliasRecords(
function createAlias(
namePrefix: string,
record: AliasRecord,
opts: ComponentResourceOptions,
Expand All @@ -152,6 +128,30 @@ export function dns(args: DnsArgs = {}) {
);
}

/**
* Creates a DNS record in the hosted zone.
*
* @param namePrefix The prefix to use for the resource names.
* @param record The DNS record to create.
* @param opts The component resource options.
*/
function createRecord(
namePrefix: string,
record: Record,
opts: ComponentResourceOptions,
) {
return _createRecord(
namePrefix,
{
type: record.type,
name: record.name,
ttl: 60,
records: [record.value],
},
opts,
);
}

function _createRecord(
namePrefix: string,
partial: Omit<route53.RecordArgs, "zoneId">,
Expand Down
Loading

0 comments on commit 55524f5

Please sign in to comment.