Skip to content

Commit 4e7f672

Browse files
committed
feat: scope API Gateway and Lambda origin to CloudFront domain
1 parent 03b673c commit 4e7f672

File tree

12 files changed

+272
-281
lines changed

12 files changed

+272
-281
lines changed

docs/src/content/docs/en/guides/fastapi.mdx

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -490,26 +490,7 @@ This sets up:
490490
5. X-Ray tracing configuration
491491
6. CloudWatch metrics namespace
492492

493-
:::note
494-
If your solution includes a website you can configure its CloudFront distribution as the only permitted CORS origin in the API gateway / API AWS Lambda integrations for HTTP / REST APIs.
495-
You will need to create the API and then call the API `grantCorsFrom` method with the created website.
496-
497-
```ts
498-
import { MyApi, MyWebsite } from ':my-scope/common-constructs';
499-
500-
export class ExampleStack extends Stack {
501-
constructor(scope: Construct, id: string) {
502-
const api = new MyApi(this, 'MyApi', {
503-
integrations: MyApi.defaultIntegrations(this).build(),
504-
});
505-
const website = new MyWebsite(this, 'MyWebsite');
506-
api.grantCorsFrom(website);
507-
}
508-
}
509-
```
510-
511-
The `MyWebsite` construct can be generated using the <Link path="/guides/react-website">`ts#react-website` generator</Link>
512-
:::
493+
<Snippet name="api/cors-configuration-cdk-note" />
513494

514495
:::note
515496
If you selected to use `Cognito` authentication, you will need to supply the `identity` property to the API construct:
@@ -563,27 +544,7 @@ This sets up:
563544
5. X-Ray tracing configuration
564545
6. CORS configuration
565546

566-
:::note
567-
If your solution includes a Terraform website module then you can use its CloudFront domain name to restrict CORS.
568-
Given a CloudFront domain name `<domain_name>`, within the Terraform module for deploying your API add
569-
- a `cors_allow_origins` property, set to `["http://localhost:4200", "http://localhost:4300", "https://<domain name>"]`, for HTTP APIs. This restricts the API gateway CORS to this distribution and local host.
570-
- an `ALLOWED_ORIGINS` environment variable, set to `"https://<domain_name>"`, for REST APIs. This sets the CloudFront distribution as the only permitted CORS origin (other than local host) in AWS Lambda integrations.
571-
572-
```hcl {4,7}
573-
module "my_api" {
574-
source = "../../common/terraform/src/app/apis/my-api"
575-
576-
cors_allow_origins = ["http://localhost:4200", "http://localhost:4300", "https://<domain name>"] // Only required for HTTP API
577-
578-
env = {
579-
ALLOWED_ORIGINS = "https://<domain name>" // Only required for REST API
580-
ENVIRONMENT = var.environment
581-
LOG_LEVEL = "INFO"
582-
}
583-
}
584-
```
585-
The `MyWebsite` construct can be generated using the <Link path="/guides/react-website">`ts#react-website` generator</Link>
586-
:::
547+
<Snippet name="api/cors-configuration-terraform-note" />
587548

588549
:::note
589550
If you selected to use `Cognito` authentication, you will need to supply the Cognito configuration:

docs/src/content/docs/en/guides/trpc.mdx

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -425,26 +425,7 @@ export class ExampleStack extends Stack {
425425

426426
This sets up your API infrastructure, including an AWS API Gateway REST or HTTP API, AWS Lambda functions for business logic, and authentication based on your chosen `auth` method.
427427

428-
:::note
429-
If your solution includes a website you can configure its CloudFront distribution as the only permitted CORS origin in the API gateway / API AWS Lambda integrations for HTTP / REST APIs.
430-
You will need to create the API and then call the API `grantCorsFrom` method with the created website.
431-
432-
```ts
433-
import { MyApi, MyWebsite } from ':my-scope/common-constructs';
434-
435-
export class ExampleStack extends Stack {
436-
constructor(scope: Construct, id: string) {
437-
const api = new MyApi(this, 'MyApi', {
438-
integrations: MyApi.defaultIntegrations(this).build(),
439-
});
440-
const website = new MyWebsite(this, 'MyWebsite');
441-
api.grantCorsFrom(website);
442-
}
443-
}
444-
```
445-
446-
The `MyWebsite` construct can be generated using the <Link path="/guides/react-website">`ts#react-website` generator</Link>
447-
:::
428+
<Snippet name="api/cors-configuration-cdk-note" />
448429

449430
:::note
450431
If you selected to use `Cognito` authentication, you will need to supply the `identity` property to the API construct:
@@ -498,27 +479,7 @@ This sets up:
498479
5. X-Ray tracing configuration
499480
6. CORS configuration
500481

501-
:::note
502-
If your solution includes a Terraform website module then you can use its CloudFront domain name to restrict CORS.
503-
Given a CloudFront domain name `<domain_name>`, within the Terraform module for deploying your API add
504-
- a `cors_allow_origins` property, set to `["http://localhost:4200", "http://localhost:4300", "https://<domain name>"]`, for HTTP APIs. This restricts the API gateway CORS to this distribution and local host.
505-
- an `ALLOWED_ORIGINS` environment variable, set to `"https://<domain_name>"`, for REST APIs. This sets the CloudFront distribution as the only permitted CORS origin (other than local host) in AWS Lambda integrations.
506-
507-
```hcl {4,7}
508-
module "my_api" {
509-
source = "../../common/terraform/src/app/apis/my-api"
510-
511-
cors_allow_origins = ["http://localhost:4200", "http://localhost:4300", "https://<domain name>"] // Only required for HTTP API
512-
513-
env = {
514-
ALLOWED_ORIGINS = "https://<domain name>" // Only required for REST API
515-
ENVIRONMENT = var.environment
516-
LOG_LEVEL = "INFO"
517-
}
518-
}
519-
```
520-
The `MyWebsite` construct can be generated using the <Link path="/guides/react-website">`ts#react-website` generator</Link>
521-
:::
482+
<Snippet name="api/cors-configuration-terraform-note" />
522483

523484
:::note
524485
If you selected to use `Cognito` authentication, you will need to supply the Cognito configuration:

docs/src/content/docs/en/guides/ts-smithy-api.mdx

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -405,26 +405,7 @@ This sets up:
405405
4. CloudWatch log group
406406
5. X-Ray tracing configuration
407407

408-
:::note
409-
If your solution includes a website you can configure its CloudFront distribution as the only permitted CORS origin in the API gateway / API AWS Lambda integrations for HTTP / REST APIs.
410-
You will need to create the API and then call the API `grantCorsFrom` method with the created website.
411-
412-
```ts
413-
import { MyApi, MyWebsite } from ':my-scope/common-constructs';
414-
415-
export class ExampleStack extends Stack {
416-
constructor(scope: Construct, id: string) {
417-
const api = new MyApi(this, 'MyApi', {
418-
integrations: MyApi.defaultIntegrations(this).build(),
419-
});
420-
const website = new MyWebsite(this, 'MyWebsite');
421-
api.grantCorsFrom(website);
422-
}
423-
}
424-
```
425-
426-
The `MyWebsite` construct can be generated using the <Link path="/guides/react-website">`ts#react-website` generator</Link>
427-
:::
408+
<Snippet name="api/cors-configuration-cdk-note" />
428409

429410
:::note
430411
If you selected `Cognito` authentication, you will need to supply the `identity` property to the API construct:
@@ -478,27 +459,7 @@ This sets up:
478459
5. X-Ray tracing configuration
479460
6. CORS configuration
480461

481-
:::note
482-
If your solution includes a Terraform website module then you can use its CloudFront domain name to restrict CORS.
483-
Given a CloudFront domain name `<domain_name>`, within the Terraform module for deploying your API add
484-
- a `cors_allow_origins` property, set to `["http://localhost:4200", "http://localhost:4300", "https://<domain name>"]`, for HTTP APIs. This restricts the API gateway CORS to this distribution and local host.
485-
- an `ALLOWED_ORIGINS` environment variable, set to `"https://<domain_name>"`, for REST APIs. This sets the CloudFront distribution as the only permitted CORS origin (other than local host) in AWS Lambda integrations.
486-
487-
```hcl {4,7}
488-
module "my_api" {
489-
source = "../../common/terraform/src/app/apis/my-api"
490-
491-
cors_allow_origins = ["http://localhost:4200", "http://localhost:4300", "https://<domain name>"] // Only required for HTTP API
492-
493-
env = {
494-
ALLOWED_ORIGINS = "https://<domain name>" // Only required for REST API
495-
ENVIRONMENT = var.environment
496-
LOG_LEVEL = "INFO"
497-
}
498-
}
499-
```
500-
The `MyWebsite` construct can be generated using the <Link path="/guides/react-website">`ts#react-website` generator</Link>
501-
:::
462+
<Snippet name="api/cors-configuration-terraform-note" />
502463

503464
:::note
504465
If you selected `Cognito` authentication, you will need to supply the Cognito configuration:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: CORS configuration CDK
3+
---
4+
import Link from '@components/link.astro';
5+
6+
:::note
7+
If your solution includes a website you can configure its CloudFront distribution as the only permitted CORS origin in the API gateway / API AWS Lambda integrations for HTTP / REST APIs. Note that this restriction is not applied to preflight OPTIONS for REST APIs.
8+
You will need to create the API and then call the API `restrictCorsTo` method with the created website.
9+
10+
```ts
11+
import { MyApi, MyWebsite } from ':my-scope/common-constructs';
12+
13+
export class ExampleStack extends Stack {
14+
constructor(scope: Construct, id: string) {
15+
const api = new MyApi(this, 'MyApi', {
16+
integrations: MyApi.defaultIntegrations(this).build(),
17+
});
18+
const website = new MyWebsite(this, 'MyWebsite');
19+
api.restrictCorsTo(website);
20+
}
21+
}
22+
```
23+
24+
The `MyWebsite` construct can be generated using the <Link path="/guides/react-website">`ts#react-website` generator</Link>
25+
:::
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: CORS configuration Terraform
3+
---
4+
import Link from '@components/link.astro';
5+
6+
:::note
7+
If your solution includes a Terraform website module then you can use its CloudFront domain name to restrict CORS.
8+
Given a CloudFront domain name `<domain_name>`, within the Terraform module for deploying your API add
9+
- a `cors_allow_origins` property, set to `["http://localhost:4200", "http://localhost:4300", "https://<domain name>"]`, for HTTP APIs. This restricts the API gateway CORS to this distribution and local host.
10+
- an `ALLOWED_ORIGINS` environment variable, set to `"https://<domain_name>"`, for REST APIs. This sets the CloudFront distribution as the only permitted CORS origin (other than local host) in AWS Lambda integrations. Note that this restriction is not applied to preflight OPTIONS.
11+
12+
```hcl {4,7}
13+
module "my_api" {
14+
source = "../../common/terraform/src/app/apis/my-api"
15+
16+
cors_allow_origins = ["http://localhost:4200", "http://localhost:4300", "https://<domain name>"] // Only required for HTTP API
17+
18+
env = {
19+
ALLOWED_ORIGINS = "https://<domain name>" // Only required for REST API
20+
ENVIRONMENT = var.environment
21+
LOG_LEVEL = "INFO"
22+
}
23+
}
24+
```
25+
The `MyWebsite` construct can be generated using the <Link path="/guides/react-website">`ts#react-website` generator</Link>
26+
:::

packages/nx-plugin/src/py/fast-api/__snapshots__/generator.spec.ts.snap

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -436,21 +436,22 @@ export class TestApi<
436436
}
437437
438438
/**
439-
* Grants CORS to this API from the CloudFront distribution domain
439+
* Restricts CORS to the website CloudFront distribution domains
440440
*
441-
* Configures the CloudFront distribution domain as the only permitted CORS origin
441+
* Configures the CloudFront distribution domains as the only permitted CORS origins
442442
* (other than local host with default ports) in the API gateway
443-
* The CORS origin is not configured within the AWS Lambda integrations since
443+
* The CORS origins are not configured within the AWS Lambda integrations since
444444
* the associated header is controlled by API Gateway v2
445445
*
446446
* @param cloudFrontDistribution - The CloudFront distribution to grant CORS from
447447
*/
448-
public grantCorsFrom({
449-
cloudFrontDistribution,
450-
}: {
451-
cloudFrontDistribution: Distribution;
452-
}) {
453-
const allowedOrigin = \`https://\${cloudFrontDistribution.distributionDomainName}\`;
448+
public restrictCorsTo(
449+
...websites: { cloudFrontDistribution: Distribution }[]
450+
) {
451+
const allowedOrigins = websites.map(
452+
({ cloudFrontDistribution }) =>
453+
\`https://\${cloudFrontDistribution.distributionDomainName}\`,
454+
);
454455
455456
const cfnApi = this.api.node.defaultChild;
456457
if (!(cfnApi instanceof CfnApi)) {
@@ -463,7 +464,7 @@ export class TestApi<
463464
allowOrigins: [
464465
'http://localhost:4200',
465466
'http://localhost:4300',
466-
allowedOrigin,
467+
...allowedOrigins,
467468
],
468469
allowMethods: [CorsHttpMethod.ANY],
469470
allowHeaders: [
@@ -980,24 +981,29 @@ export class TestApi<
980981
}
981982
982983
/**
983-
* Grants CORS to this API from the CloudFront distribution domain
984+
* Restricts CORS to the website CloudFront distribution domains
984985
*
985-
* Configures the CloudFront distribution domain as the only permitted CORS origin
986+
* Configures the CloudFront distribution domains as the only permitted CORS origins
986987
* (other than local host) in the AWS Lambda integrations
987988
*
988-
* @param cloudFrontDistribution - The CloudFront distribution to grant CORS from
989+
* Note that this restriction is not applied to preflight OPTIONS
990+
*
991+
* @param websites - The CloudFront distribution to grant CORS from
989992
*/
990-
public grantCorsFrom({
991-
cloudFrontDistribution,
992-
}: {
993-
cloudFrontDistribution: Distribution;
994-
}) {
995-
const allowedOrigin = \`https://\${cloudFrontDistribution.distributionDomainName}\`;
993+
public restrictCorsTo(
994+
...websites: { cloudFrontDistribution: Distribution }[]
995+
) {
996+
const allowedOrigins = websites
997+
.map(
998+
({ cloudFrontDistribution }) =>
999+
\`https://\${cloudFrontDistribution.distributionDomainName}\`,
1000+
)
1001+
.join(',');
9961002
9971003
// Set ALLOWED_ORIGINS environment variable for all Lambda integrations
9981004
Object.values(this.integrations).forEach((integration) => {
9991005
if ('handler' in integration && integration.handler instanceof Function) {
1000-
integration.handler.addEnvironment('ALLOWED_ORIGINS', allowedOrigin);
1006+
integration.handler.addEnvironment('ALLOWED_ORIGINS', allowedOrigins);
10011007
}
10021008
});
10031009
}

0 commit comments

Comments
 (0)