|
| 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'; |
| 5 | +import * as path from "path"; |
| 6 | +import { CfnOutput } from '@aws-cdk/core'; |
| 7 | + |
| 8 | +export class CdkStack extends cdk.Stack { |
| 9 | + constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { |
| 10 | + super(scope, id, props); |
| 11 | + |
| 12 | + // lambda |
| 13 | + const fn = new lambda.Function(this, "MyLambda", { |
| 14 | + runtime: lambda.Runtime.NODEJS_12_X, |
| 15 | + handler: "build/lambda.handler", |
| 16 | + code: lambda.Code.fromAsset(path.join(__dirname, "../cdk-api.zip")) |
| 17 | + }); |
| 18 | + |
| 19 | + // ApiGW |
| 20 | + const apigw = new apigateway.LambdaRestApi(this, "MyApi", { |
| 21 | + handler: fn, |
| 22 | + proxy: true |
| 23 | + }); |
| 24 | + |
| 25 | + // CF |
| 26 | + const feCf = new cloudfront.CloudFrontWebDistribution(this, "MyCf", { |
| 27 | + defaultRootObject: "/", |
| 28 | + originConfigs: [{ |
| 29 | + customOriginSource: { |
| 30 | + domainName: `${apigw.restApiId}.execute-api.${this.region}.${this.urlSuffix}`, |
| 31 | + }, |
| 32 | + originPath: '/' + apigw.deploymentStage.stageName, |
| 33 | + behaviors: [{ |
| 34 | + isDefaultBehavior: true, |
| 35 | + }] |
| 36 | + }], |
| 37 | + enableIpV6: true, |
| 38 | + }); |
| 39 | + |
| 40 | + new CfnOutput(this, "myOut", { |
| 41 | + value: feCf.domainName |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
0 commit comments