1
- import * as cdk from '@aws-cdk/core' ;
2
- import * as lambda from '@aws-cdk/aws-lambda' ;
3
- import * as apigateway from '@aws-cdk/aws-apigateway' ;
4
- import * as cloudfront from '@aws-cdk/aws-cloudfront' ;
1
+ import * as cdk from "@aws-cdk/core" ;
2
+ import * as lambda from "@aws-cdk/aws-lambda" ;
3
+ import * as apigatewayv2 from "@aws-cdk/aws-apigatewayv2" ;
4
+ import * as apigatewayv2Integrations from "@aws-cdk/aws-apigatewayv2-integrations" ;
5
+ import * as cloudfront from "@aws-cdk/aws-cloudfront" ;
5
6
import * as path from "path" ;
6
7
7
8
export class CdkStack extends cdk . Stack {
@@ -13,34 +14,43 @@ export class CdkStack extends cdk.Stack {
13
14
runtime : lambda . Runtime . NODEJS_12_X ,
14
15
handler : "build/lambda.handler" ,
15
16
code : lambda . Code . fromAsset ( path . join ( __dirname , "../../" ) , {
16
- exclude : [ "cdk" ]
17
- } )
17
+ exclude : [ "cdk" ] ,
18
+ } ) ,
18
19
} ) ;
19
20
20
- // ApiGW
21
- const apigw = new apigateway . LambdaRestApi ( this , "MyApi" , {
21
+ // ApiGWv2
22
+ const httpApiIntegration = new apigatewayv2Integrations . LambdaProxyIntegration ( {
22
23
handler : fn ,
23
- proxy : true
24
+ } ) ;
25
+
26
+ const httpApi = new apigatewayv2 . HttpApi ( this , "MyApi" ) ;
27
+ httpApi . addRoutes ( {
28
+ path : "/" ,
29
+ methods : [ apigatewayv2 . HttpMethod . ANY ] ,
30
+ integration : httpApiIntegration ,
24
31
} ) ;
25
32
26
33
// CF
27
34
const feCf = new cloudfront . CloudFrontWebDistribution ( this , "MyCf" , {
28
35
defaultRootObject : "/" ,
29
- originConfigs : [ {
30
- customOriginSource : {
31
- domainName : `${ apigw . restApiId } .execute-api.${ this . region } .${ this . urlSuffix } ` ,
36
+ originConfigs : [
37
+ {
38
+ customOriginSource : {
39
+ domainName : `${ httpApi . httpApiId } .execute-api.${ this . region } .${ this . urlSuffix } ` ,
40
+ } ,
41
+ behaviors : [
42
+ {
43
+ isDefaultBehavior : true ,
44
+ } ,
45
+ ] ,
32
46
} ,
33
- originPath : '/' + apigw . deploymentStage . stageName ,
34
- behaviors : [ {
35
- isDefaultBehavior : true ,
36
- } ]
37
- } ] ,
47
+ ] ,
38
48
enableIpV6 : true ,
39
49
} ) ;
40
50
41
51
// Output
42
52
new cdk . CfnOutput ( this , "myOut" , {
43
- value : feCf . domainName
53
+ value : feCf . distributionDomainName ,
44
54
} ) ;
45
55
}
46
56
}
0 commit comments