-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathapigatewayv1.ts
More file actions
1663 lines (1589 loc) · 44.5 KB
/
Copy pathapigatewayv1.ts
File metadata and controls
1663 lines (1589 loc) · 44.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
ComponentResourceOptions,
Output,
all,
interpolate,
output,
} from "@pulumi/pulumi";
import {
Component,
outputId,
Prettify,
Transform,
transform,
} from "../component";
import { Link } from "../link";
import type { Input } from "../input";
import { FunctionArgs, FunctionArn } from "./function";
import { hashStringToPrettyString, physicalName, logicalName } from "../naming";
import { VisibleError } from "../error";
import { RETENTION } from "./logging";
import { ApiGatewayV1LambdaRoute } from "./apigatewayv1-lambda-route";
import { ApiGatewayV1Authorizer } from "./apigatewayv1-authorizer";
import { setupApiGatewayAccount } from "./helpers/apigateway-account";
import { apigateway, cloudwatch, getRegionOutput } from "@pulumi/aws";
import { Dns } from "../dns";
import { dns as awsDns } from "./dns";
import { DnsValidatedCertificate } from "./dns-validated-certificate";
import { ApiGatewayV1IntegrationRoute } from "./apigatewayv1-integration-route";
import { ApiGatewayV1UsagePlan } from "./apigatewayv1-usage-plan";
import { useProvider } from "./helpers/provider";
export interface ApiGatewayV1DomainArgs {
/**
* Use an existing API Gateway domain name.
*
* By default, a new API Gateway domain name is created. If you'd like to use an existing
* domain name, set the `nameId` to the ID of the domain name and **do not** pass in `name`.
*
* @example
* ```js
* {
* domain: {
* nameId: "example.com"
* }
* }
* ```
*/
nameId?: Input<string>;
/**
* The custom domain you want to use.
*
* @example
* ```js
* {
* domain: {
* name: "example.com"
* }
* }
* ```
*
* Can also include subdomains based on the current stage.
*
* ```js
* {
* domain: {
* name: `${$app.stage}.example.com`
* }
* }
* ```
*/
name: Input<string>;
/**
* The base mapping for the custom domain. This adds a suffix to the URL of the API.
*
* @example
*
* Given the following base path and domain name.
*
* ```js
* {
* domain: {
* name: "api.example.com",
* path: "v1"
* }
* }
* ```
*
* The full URL of the API will be `https://api.example.com/v1/`.
*
* :::note
* There's an extra trailing slash when a base path is set.
* :::
*
* By default there is no base path, so if the `name` is `api.example.com`, the full URL will be `https://api.example.com`.
*/
path?: Input<string>;
/**
* The ARN of an ACM (AWS Certificate Manager) certificate that proves ownership of the
* domain. By default, a certificate is created and validated automatically.
*
* :::tip
* You need to pass in a `cert` for domains that are not hosted on supported `dns` providers.
* :::
*
* To manually set up a domain on an unsupported provider, you'll need to:
*
* 1. [Validate that you own the domain](https://docs.aws.amazon.com/acm/latest/userguide/domain-ownership-validation.html) by creating an ACM certificate. You can either validate it by setting a DNS record or by verifying an email sent to the domain owner.
* 2. Once validated, set the certificate ARN as the `cert` and set `dns` to `false`.
* 3. Add the DNS records in your provider to point to the API Gateway URL.
*
* @example
* ```js
* {
* domain: {
* name: "example.com",
* dns: false,
* cert: "arn:aws:acm:us-east-1:112233445566:certificate/3a958790-8878-4cdc-a396-06d95064cf63"
* }
* }
* ```
*/
cert?: Input<string>;
/**
* The DNS provider to use for the domain. Defaults to the AWS.
*
* Takes an adapter that can create the DNS records on the provider. This can automate
* validating the domain and setting up the DNS routing.
*
* Supports Route 53, Cloudflare, and Vercel adapters. For other providers, you'll need
* to set `dns` to `false` and pass in a certificate validating ownership via `cert`.
*
* @default `sst.aws.dns`
*
* @example
*
* Specify the hosted zone ID for the Route 53 domain.
*
* ```js
* {
* domain: {
* name: "example.com",
* dns: sst.aws.dns({
* zone: "Z2FDTNDATAQYW2"
* })
* }
* }
* ```
*
* Use a domain hosted on Cloudflare, needs the Cloudflare provider.
*
* ```js
* {
* domain: {
* name: "example.com",
* dns: sst.cloudflare.dns()
* }
* }
* ```
*
* Use a domain hosted on Vercel, needs the Vercel provider.
*
* ```js
* {
* domain: {
* name: "example.com",
* dns: sst.vercel.dns()
* }
* }
* ```
*/
dns?: Input<false | (Dns & {})>;
}
export interface ApiGatewayV1Args {
/**
* Set a custom domain for your REST API.
*
* Automatically manages domains hosted on AWS Route 53, Cloudflare, and Vercel. For other
* providers, you'll need to pass in a `cert` that validates domain ownership and add the
* DNS records.
*
* :::tip
* Built-in support for AWS Route 53, Cloudflare, and Vercel. And manual setup for other
* providers.
* :::
*
* @example
*
* By default this assumes the domain is hosted on Route 53.
*
* ```js
* {
* domain: "example.com"
* }
* ```
*
* For domains hosted on Cloudflare.
*
* ```js
* {
* domain: {
* name: "example.com",
* dns: sst.cloudflare.dns()
* }
* }
* ```
*/
domain?: Input<string | Prettify<ApiGatewayV1DomainArgs>>;
/**
* Configure the type of API Gateway REST API endpoint.
*
* - `edge`: The default; it creates a CloudFront distribution for the API.
* Useful for cases where requests are geographically distributed.
* - `regional`: Endpoints are deployed in specific AWS regions and are
* intended to be accessed directly by clients within or near that region.
* - `private`: Endpoints allows access to the API only from within a specified
* Amazon VPC (Virtual Private Cloud) using VPC endpoints. These do not expose
* the API to the public internet.
*
* Learn more about the [different types of endpoints](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-api-endpoint-types.html).
*
* @default `{type: "edge"}`
* @example
*
* For example, to create a regional endpoint.
* ```js
* {
* endpoint: {
* type: "regional"
* }
* }
* ```
*
* And to create a private endpoint.
* ```js
* {
* endpoint: {
* type: "private",
* vpcEndpointIds: ["vpce-0dccab6fb1e828f36"]
* }
* }
* ```
*/
endpoint?: Input<{
/**
* The type of the API Gateway REST API endpoint.
*/
type: "edge" | "regional" | "private";
/**
* The VPC endpoint IDs for the `private` endpoint.
*/
vpcEndpointIds?: Input<Input<string>[]>;
}>;
/**
* Enable the CORS or Cross-origin resource sharing for your API.
* @default `true`
* @example
* Disable CORS.
* ```js
* {
* cors: false
* }
* ```
*/
cors?: Input<boolean>;
/**
* Configure the [API Gateway logs](https://docs.aws.amazon.com/apigateway/latest/developerguide/view-cloudwatch-log-events-in-cloudwatch-console.html) in CloudWatch. By default, access logs are enabled and retained for 1 month.
* @default `{retention: "1 month"}`
* @example
* ```js
* {
* accessLog: {
* retention: "forever"
* }
* }
* ```
*/
accessLog?: Input<{
/**
* The duration the API Gateway logs are retained in CloudWatch.
* @default `1 month`
*/
retention?: Input<keyof typeof RETENTION>;
}>;
/**
* [Transform](/docs/components#transform) how this component creates its underlying
* resources.
*/
transform?: {
/**
* Transform the API Gateway REST API resource.
*/
api?: Transform<apigateway.RestApiArgs>;
/**
* Transform the API Gateway REST API stage resource.
*/
stage?: Transform<apigateway.StageArgs>;
/**
* Transform the API Gateway REST API deployment resource.
*/
deployment?: Transform<apigateway.DeploymentArgs>;
/**
* Transform the CloudWatch LogGroup resource used for access logs.
*/
accessLog?: Transform<cloudwatch.LogGroupArgs>;
/**
* Transform the API Gateway REST API domain name resource.
*/
domainName?: Transform<apigateway.DomainNameArgs>;
/**
* Transform the routes. This is called for every route that is added.
*
* :::note
* This is applied right before the resource is created.
* :::
*
* You can use this to set any default props for all the routes and their handler function.
* Like the other transforms, you can either pass in an object or a callback.
*
* @example
*
* Here we are setting a default memory of `2048 MB` for our routes.
*
* ```js
* {
* transform: {
* route: {
* handler: (args, opts) => {
* // Set the default if it's not set by the route
* args.memory ??= "2048 MB";
* }
* }
* }
* }
* ```
*
* Defaulting to IAM auth for all our routes.
*
* ```js
* {
* transform: {
* route: {
* args: (props) => {
* // Set the default if it's not set by the route
* props.auth ??= { iam: true };
* }
* }
* }
* }
* ```
*/
route?: {
/**
* Transform the handler function of the route.
*/
handler?: Transform<FunctionArgs>;
/**
* Transform the arguments for the route.
*/
args?: Transform<ApiGatewayV1RouteArgs>;
};
};
}
export interface ApiGatewayV1AuthorizerArgs {
/**
* The name of the authorizer.
* @example
* ```js
* {
* name: "myAuthorizer"
* }
* ```
*/
name: string;
/**
* The Lambda token authorizer function. Takes the handler path or the function args.
* @example
* ```js
* {
* tokenFunction: "src/authorizer.index"
* }
* ```
*/
tokenFunction?: Input<string | FunctionArgs>;
/**
* The Lambda request authorizer function. Takes the handler path or the function args.
* @example
* ```js
* {
* requestFunction: "src/authorizer.index"
* }
* ```
*/
requestFunction?: Input<string | FunctionArgs>;
/**
* A list of user pools used as the authorizer.
* @example
* ```js
* {
* name: "myAuthorizer",
* userPools: [userPool.arn]
* }
* ```
*
* Where `userPool` is:
*
* ```js
* const userPool = new aws.cognito.UserPool();
* ```
*/
userPools?: Input<Input<string>[]>;
/**
* Time to live for cached authorizer results in seconds.
* @default `300`
* @example
* ```js
* {
* ttl: 30
* }
* ```
*/
ttl?: Input<number>;
/**
* Specifies where to extract the authorization token from the request.
* @default `"method.request.header.Authorization"`
* @example
* ```js
* {
* identitySource: "method.request.header.AccessToken"
* }
* ```
*/
identitySource?: Input<string>;
/**
* [Transform](/docs/components#transform) how this component creates its underlying
* resources.
*/
transform?: {
/**
* Transform the API Gateway authorizer resource.
*/
authorizer?: Transform<apigateway.AuthorizerArgs>;
};
}
export interface ApiGatewayV1UsagePlanArgs {
/**
* Configure rate limits to protect your API from being overwhelmed by too many
* requests at once.
*
* @example
* ```js
* {
* throttle: {
* rate: 100,
* burst: 200
* }
* }
* ```
*/
throttle?: Input<{
/**
* The maximum number of requests permitted in a short-term spike beyond the
* rate limit.
*/
burst?: Input<number>;
/**
* The steady-state maximum number of requests allowed per second.
*/
rate?: Input<number>;
}>;
/**
* Configure a cap on the total number of requests allowed within a specified time
* period.
* @example
* ```js
* {
* quota: {
* limit: 1000,
* period: "month",
* offset: 0
* }
* }
* ```
*/
quota?: Input<{
/**
* The maximum number of requests that can be made in the specified period of
* time.
*/
limit: Input<number>;
/**
* The time period for which the quota applies.
*/
period: Input<"day" | "week" | "month">;
/**
* The number of days into the period when the quota counter is reset.
*
* For example, this resets the quota at the beginning of each month.
*
* ```js
* {
* period: "month",
* offset: 0
* }
* ```
*/
offset?: Input<number>;
}>;
}
export interface ApiGatewayV1ApiKeyArgs {
/**
* The value of the API key. If not provided, it will be generated automatically.
* @example
* ```js
* {
* value: "d41d8cd98f00b204e9800998ecf8427e"
* }
* ```
*/
value?: Input<string>;
}
export interface ApiGatewayV1RouteArgs {
/**
* Enable auth for your REST API. By default, auth is disabled.
* @default `false`
* @example
* ```js
* {
* auth: {
* iam: true
* }
* }
* ```
*/
auth?: Input<
| false
| {
/**
* Enable IAM authorization for a given API route.
*
* When IAM auth is enabled, clients need to use Signature Version 4 to sign their requests with their AWS credentials.
*/
iam?: Input<boolean>;
/**
* Enable custom Lambda authorization for a given API route. Pass in the authorizer ID.
* @example
* ```js
* {
* auth: {
* custom: myAuthorizer.id
* }
* }
* ```
*
* Where `myAuthorizer` is:
*
* ```js
* const userPool = new aws.cognito.UserPool();
* const myAuthorizer = api.addAuthorizer({
* name: "MyAuthorizer",
* userPools: [userPool.arn]
* });
* ```
*/
custom?: Input<string>;
/**
* Enable Cognito User Pool authorization for a given API route.
*
* @example
* You can configure JWT auth.
*
* ```js
* {
* auth: {
* cognito: {
* authorizer: myAuthorizer.id,
* scopes: ["read:profile", "write:profile"]
* }
* }
* }
* ```
*
* Where `myAuthorizer` is:
*
* ```js
* const userPool = new aws.cognito.UserPool();
*
* const myAuthorizer = api.addAuthorizer({
* name: "MyAuthorizer",
* userPools: [userPool.arn]
* });
* ```
*/
cognito?: Input<{
/**
* Authorizer ID of the Cognito User Pool authorizer.
*/
authorizer: Input<string>;
/**
* Defines the permissions or access levels that the authorization token grants.
*/
scopes?: Input<Input<string>[]>;
}>;
}
>;
/**
* Specify if an API key is required for the route. By default, an API key is not
* required.
* @default `false`
* @example
* ```js
* {
* apiKey: true
* }
* ```
*/
apiKey?: Input<boolean>;
/**
* [Transform](/docs/components#transform) how this component creates its underlying
* resources.
*/
transform?: {
/**
* Transform the API Gateway REST API method resource.
*/
method?: Transform<apigateway.MethodArgs>;
/**
* Transform the API Gateway REST API integration resource.
*/
integration?: Transform<apigateway.IntegrationArgs>;
};
}
export interface ApiGatewayV1IntegrationArgs {
/**
* The type of the API Gateway REST API integration.
*/
type: Input<"aws" | "aws-proxy" | "mock" | "http" | "http-proxy">;
/**
* The URI of the API Gateway REST API integration.
*/
uri?: Input<string>;
/**
* The credentials to use to call the AWS service.
*/
credentials?: Input<string>;
/**
* The HTTP method to use to call the integration.
*/
integrationHttpMethod?: Input<
"GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "ANY" | "PATCH"
>;
/**
* Map of request query string parameters and headers that should be passed to the backend responder.
*/
requestParameters?: Input<Record<string, Input<string>>>;
/**
* Map of the integration's request templates.
*/
requestTemplates?: Input<Record<string, Input<string>>>;
/**
* The passthrough behavior to use to call the integration.
*
* Required if `requestTemplates` is set.
*/
passthroughBehavior?: Input<"when-no-match" | "never" | "when-no-templates">;
}
/**
* The `ApiGatewayV1` component lets you add an [Amazon API Gateway REST API](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-rest-api.html) to your app.
*
* @example
*
* #### Create the API
*
* ```ts title="sst.config.ts"
* const api = new sst.aws.ApiGatewayV1("MyApi");
* ```
*
* #### Add routes
*
* ```ts title="sst.config.ts"
* api.route("GET /", "src/get.handler");
* api.route("POST /", "src/post.handler");
*
* api.deploy();
* ```
*
* :::note
* You need to call `deploy` after you've added all your routes.
* :::
*
* #### Configure the routes
*
* ```ts title="sst.config.ts"
* api.route("GET /", "src/get.handler", {
* auth: { iam: true }
* });
* ```
*
* #### Configure the route handler
*
* You can configure the Lambda function that'll handle the route.
*
* ```ts title="sst.config.ts"
* api.route("POST /", {
* handler: "src/post.handler",
* memory: "2048 MB"
* });
* ```
*
* #### Default props for all routes
*
* You can use a `transform` to set some default props for all your routes. For
* example, instead of setting the `memory` for each route.
*
* ```ts title="sst.config.ts"
* api.route("GET /", { handler: "src/get.handler", memory: "2048 MB" });
* api.route("POST /", { handler: "src/post.handler", memory: "2048 MB" });
* ```
*
* You can set it through the `transform`.
*
* ```ts title="sst.config.ts" {6}
* const api = new sst.aws.ApiGatewayV1("MyApi", {
* transform: {
* route: {
* handler: (args, opts) => {
* // Set the default if it's not set by the route
* args.memory ??= "2048 MB";
* }
* }
* }
* });
*
* api.route("GET /", "src/get.handler");
* api.route("POST /", "src/post.handler");
* ```
*
* With this we set the `memory` if it's not overridden by the route.
*/
export class ApiGatewayV1 extends Component implements Link.Linkable {
private constructorName: string;
private constructorArgs: ApiGatewayV1Args;
private constructorOpts: ComponentResourceOptions;
private api: apigateway.RestApi;
private apigDomain?: Output<apigateway.DomainName>;
private apiMapping?: Output<apigateway.BasePathMapping>;
private region: Output<string>;
private resources: Record<string, Output<string>> = {};
private routes: (ApiGatewayV1LambdaRoute | ApiGatewayV1IntegrationRoute)[] =
[];
private stage?: apigateway.Stage;
private logGroup?: cloudwatch.LogGroup;
private endpointType: Output<"EDGE" | "REGIONAL" | "PRIVATE">;
private deployed: boolean = false;
constructor(
name: string,
args: ApiGatewayV1Args = {},
opts: ComponentResourceOptions = {},
) {
super(__pulumiType, name, args, opts);
const parent = this;
const region = normalizeRegion();
const endpoint = normalizeEndpoint();
const apigAccount = setupApiGatewayAccount(name, opts);
const api = createApi();
this.resources["/"] = api.rootResourceId;
this.constructorName = name;
this.constructorArgs = args;
this.constructorOpts = opts;
this.api = api;
this.region = region;
this.endpointType = endpoint.types;
function normalizeRegion() {
return getRegionOutput(undefined, { parent }).region;
}
function normalizeEndpoint() {
return output(args.endpoint).apply((endpoint) => {
if (!endpoint) return { types: "EDGE" as const };
if (endpoint.type === "private" && !endpoint.vpcEndpointIds)
throw new VisibleError(
"Please provide the VPC endpoint IDs for the private endpoint.",
);
return endpoint.type === "regional"
? { types: "REGIONAL" as const }
: endpoint.type === "private"
? {
types: "PRIVATE" as const,
vpcEndpointIds: endpoint.vpcEndpointIds,
}
: { types: "EDGE" as const };
});
}
function createApi() {
return new apigateway.RestApi(
...transform(
args.transform?.api,
`${name}Api`,
{
endpointConfiguration: endpoint,
},
{ parent, dependsOn: apigAccount },
),
);
}
}
/**
* The URL of the API.
*/
public get url() {
return this.apigDomain && this.apiMapping
? all([this.apigDomain.domainName, this.apiMapping.basePath]).apply(
([domain, key]) =>
key ? `https://${domain}/${key}/` : `https://${domain}`,
)
: interpolate`https://${this.api.id}.execute-api.${this.region}.amazonaws.com/${$app.stage}/`;
}
/**
* The underlying [resources](/docs/components/#nodes) this component creates.
*/
public get nodes() {
const self = this;
return {
/**
* The Amazon API Gateway REST API
*/
api: this.api,
/**
* The Amazon API Gateway REST API stage
*/
stage: this.stage,
/**
* The CloudWatch LogGroup for the access logs.
*/
logGroup: this.logGroup,
/**
* The API Gateway REST API domain name.
*/
get domainName() {
if (!self.deployed)
throw new VisibleError(
`"nodes.domainName" is not available before the "${self.constructorName}" API is deployed.`,
);
if (!self.apigDomain)
throw new VisibleError(
`"nodes.domainName" is not available when domain is not configured for the "${self.constructorName}" API.`,
);
return self.apigDomain;
},
};
}
/**
* Add a route to the API Gateway REST API. The route is a combination of an HTTP method and a path, `{METHOD} /{path}`.
*
* A method could be one of `GET`, `POST`, `PUT`, `DELETE`, `PATCH`, `HEAD`, `OPTIONS`, or `ANY`. Here `ANY` matches any HTTP method.
*
* The path can be a combination of
* - Literal segments, `/notes`, `/notes/new`, etc.
* - Parameter segments, `/notes/{noteId}`, `/notes/{noteId}/attachments/{attachmentId}`, etc.
* - Greedy segments, `/{proxy+}`, `/notes/{proxy+}`, etc. The `{proxy+}` segment is a greedy segment that matches all child paths. It needs to be at the end of the path.
*
* :::tip
* The `{proxy+}` is a greedy segment, it matches all its child paths.
* :::
*
* When a request comes in, the API Gateway will look for the most specific match.
*
* :::note
* You cannot have duplicate routes.
* :::
*
* @param route The path for the route.
* @param handler The function that'll be invoked.
* @param args Configure the route.
*
* @example
* Add a simple route.
*
* ```js title="sst.config.ts"
* api.route("GET /", "src/get.handler");
* ```
*
* Match any HTTP method.
*
* ```js title="sst.config.ts"
* api.route("ANY /", "src/route.handler");
* ```
*
* Add a default or fallback route. Here for every request other than `GET /hi`,
* the `default.handler` function will be invoked.
*
* ```js title="sst.config.ts"
* api.route("GET /hi", "src/get.handler");
*
* api.route("ANY /", "src/default.handler");
* api.route("ANY /{proxy+}", "src/default.handler");
* ```
*
* The `/{proxy+}` matches any path that starts with `/`, so if you want a
* fallback route for the root `/` path, you need to add a `ANY /` route as well.
*
* Add a parameterized route.
*
* ```js title="sst.config.ts"
* api.route("GET /notes/{id}", "src/get.handler");
* ```
*
* Add a greedy route.
*
* ```js title="sst.config.ts"
* api.route("GET /notes/{proxy+}", "src/greedy.handler");
* ```
*
* Enable auth for a route.
*
* ```js title="sst.config.ts"
* api.route("GET /", "src/get.handler")
* api.route("POST /", "src/post.handler", {
* auth: {
* iam: true
* }
* });
* ```
*
* Customize the route handler.
*
* ```js title="sst.config.ts"
* api.route("GET /", {
* handler: "src/get.handler",
* memory: "2048 MB"
* });
* ```
*
* Or pass in the ARN of an existing Lambda function.
*
* ```js title="sst.config.ts"
* api.route("GET /", "arn:aws:lambda:us-east-1:123456789012:function:my-function");
* ```
*/
public route(
route: string,
handler: Input<string | FunctionArgs | FunctionArn>,
args: ApiGatewayV1RouteArgs = {},
) {
const { method, path } = this.parseRoute(route);
this.createResource(path);
const transformed = transform(
this.constructorArgs.transform?.route?.args,
this.buildRouteId(method, path),
args,
{ provider: this.constructorOpts.provider },
);
const apigRoute = new ApiGatewayV1LambdaRoute(
transformed[0],
{
api: {
name: this.constructorName,
id: this.api.id,
executionArn: this.api.executionArn,
},
method,
path,
resourceId: this.resources[path],
handler,
handlerTransform: this.constructorArgs.transform?.route?.handler,
...transformed[1],
},
transformed[2],
);
this.routes.push(apigRoute);
return apigRoute;
}
/**
* Add a custom integration to the API Gateway REST API. [Learn more about
* integrations](https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-integration-settings.html).
*
* @param route The path for the route.
* @param integration The integration configuration.