From f1977ff6fe1be1d477e50899652f47b17880e922 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 19 Oct 2020 08:22:36 +0200 Subject: [PATCH] add support for API Gateway V2 --- README.md | 7 +++++++ index.js | 19 ++++++++++++++++++- resources.yml | 7 ------- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9734c03..982b895 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ plugins: custom: apiCloudFront: + httpApi: true domain: my-custom-domain.com certificate: arn:aws:acm:us-east-1:000000000000:certificate/00000000-1111-2222-3333-444444444444 waf: 00000000-0000-0000-0000-000000000000 @@ -62,6 +63,12 @@ custom: ### Notes +* `httpApi` can be *false* (default) or *true*. If you are using ApiGatewayV2 for your lambdas this setting must be applied + +``` +httpApi: true +``` + * `domain` can be list, so if you want to add more domains, instead string you list multiple ones: ``` diff --git a/index.js b/index.js index f010acf..cff0122 100644 --- a/index.js +++ b/index.js @@ -85,12 +85,28 @@ class ServerlessApiCloudFrontPlugin { prepareDomain(distributionConfig) { const domain = this.getConfig('domain', null); + const httpApi = this.getConfig("httpApi", false); if (domain !== null) { distributionConfig.Aliases = Array.isArray(domain) ? domain : [ domain ]; } else { delete distributionConfig.Aliases; } + distributionConfig.Origins[0].DomainName = { + "Fn::Join": [ + "", + [ + { + Ref: httpApi && "HttpApi" || "ApiGatewayRestApi" + }, + ".execute-api.", + { + "Ref": "AWS::Region" + }, + ".amazonaws.com" + ] + ] + } } preparePriceClass(distributionConfig) { @@ -99,7 +115,8 @@ class ServerlessApiCloudFrontPlugin { } prepareOrigins(distributionConfig) { - distributionConfig.Origins[0].OriginPath = `/${this.options.stage}`; + const httpApi = this.getConfig("httpApi", false); + distributionConfig.Origins[0].OriginPath = httpApi ? "" : `/${this.options.stage}`; } prepareCookies(distributionConfig) { diff --git a/resources.yml b/resources.yml index fab031c..d6894ca 100644 --- a/resources.yml +++ b/resources.yml @@ -6,13 +6,6 @@ Resources: DistributionConfig: Origins: - Id: ApiGateway - DomainName: - Fn::Join: - - "" - - - Ref: ApiGatewayRestApi - - ".execute-api." - - Ref: AWS::Region - - ".amazonaws.com" CustomOriginConfig: HTTPPort: '80' HTTPSPort: '443'