CloudFront distribution in front of your http api gateway
npm install serverless-with-cloudfront




> A try to extend the project: https://github.com/Droplr/serverless-api-cloudfront
Automatically properly configured AWS CloudFront distribution that routes traffic to follow resource.
- [HTTP api gateway][serverless-http-gateway]
- [REST api gateway][serverless-rest-gateway]
- [Lambda Url][serverless-lambda-url]
[serverless-http-gateway]: https://www.serverless.com/framework/docs/providers/aws/events/http-api
[serverless-rest-gateway]: https://www.serverless.com/framework/docs/providers/aws/events/apigateway
[serverless-lambda-url]: https://www.serverless.com/blog/aws-lambda-function-urls-with-serverless-framework
Automatically config custom domain and create Route 53 records
``bash`
$ npm install --save-dev serverless-with-cloudfront
OR
`bash`
$ yarn add -D serverless-with-cloudfront
> Add to plugins section
`yaml`
plugins:
- serverless-with-cloudfront
> Add to custom section
`yaml`
custom:
withCloudFront:
type: http # for HTTP Api Gateway
...
- The [HTTP api gateway][serverless-http-gateway] sample serverless yaml
`yamlserviceThe
block is the name of the service
service: sample
frameworkVersion: '3'
plugins:
- serverless-with-cloudfront
block defines where your service will be deployedcustom:
withCloudFront:
type: http # for HTTP Api Gateway
# hostedZoneId: 11223344
# domain: my-custom-domain.com
# certificate: arn:aws:acm:us-east-1:000000000000:certificate/00000000-1111-2222-3333-444444444444
headers:
- Accept
- Accept-Encoding
- Accept-Language
- Authorization
# cookies: 'all'
# querystring: 'all'
# waf: 00000000-0000-0000-0000-000000000000
# compress: true
# minimumProtocolVersion: 'protocol version'
# priceClass: 'PriceClass_All'
# logging:
# bucket: my-bucket.s3.amazonaws.com
# prefix: my-prefix
block defines what code to deploy block defines how to trigger the handler.helloWorld code
events:
- httpApi: '*'
`$3
$3
>
lambda must be set to the same name of target function>
function must set url property`yaml
custom:
withCloudFront:
type: lambda
lambda: helloWorld
...functions:
helloWorld:
url: true
...
`Configuration
| field | must | default | - |
| ---------------------- | ---- | ------------------ | --------------------------------------------------------------------------------- |
| type | \* | - | [_
http_][serverless-http-gateway] |
| | | | [_rest_][serverless-rest-gateway] |
| | | | [_lambda_][serverless-lambda-url] |
| lambda | △ | | If the type is _lambda_, this field must be set to the target function name |
| hostedZoneId | | | The Route 53 Hosted zone ID |
| domain | | | The custom domain name |
| certificate | | | The certificate of custom domain name |
| headers | | _[]_ | The headers that include in the cache key |
| cookies | | _all_ | The cookies that include in the cache key ( _all_, _none_ or a whitelist) |
| queryString | | _all_ | The query strings that include in the cache key _all_, _none_ or a whitelist) |
| waf | | | The id of WAF |
| compress | | _false_ | The auto compress option: _true_ \| _false_ |
| minimumProtocolVersion | | _TLSv1_ | _TLSv1_ \| _TLSv1_2016_ \| _TLSv1.1_2016_ \| _TLSv1.2_2018_ \| _SSLv3_ |
| priceClass | | _PriceClass_All_ | _PriceClass_All_ \| _PriceClass_100_ \| _PriceClass_200_ |
| logging | | | Bucket and prefix settings for saving access logs |Notes
- If
domain is set, certificate also needs to be set`yaml
domain: my-custom-domain.com
certificate: arn:aws:acm:us-east-1:000000000000:certificate/00000000-1111-2222-3333-444444444444
`- If
hostedZoneId is set, domain (and certificate) also needs to be set`yaml
hostedZoneId: 11223344
domain: my-custom-domain.com
certificate: arn:aws:acm:us-east-1:000000000000:certificate/00000000-1111-2222-3333-444444444444
`- [
headers][headers-default-cache] can be _[]_ (default) or a list of headers ([see CloudFront custom behaviour][headers-list]):`yaml
headers:
- Accept
- Accept-Encoding
- Accept-Language
- Authorization
``[headers-default-cache]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-defaultcachebehavior.html#cfn-cloudfront-distribution-defaultcachebehavior-forwardedvalues
[headers-list]: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/RequestAndResponseBehaviorCustomOrigin.html#request-custom-headers-behavior
In order to make this plugin work as expected a few additional IAM Policies might be needed on your AWS profile.
- cloudfront:...
- route53:...
You can read more about IAM profiles and policies in the Serverless documentation.