0.0.9 • Published 2 years ago
serverless-with-cloudfront v0.0.9
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.
Automatically config custom domain and create Route 53 records
Installation
$ npm install --save-dev serverless-with-cloudfront
OR
$ yarn add -D serverless-with-cloudfront
Usage
Samples
A HTTP api gateway sample
Add to
plugins
section
plugins:
- serverless-with-cloudfront
Add to
custom
section
custom:
withCloudFront:
type: http # for HTTP Api Gateway
...
- The HTTP api gateway sample serverless yaml
# The `service` block is the name of the service
service: sample
frameworkVersion: '3'
plugins:
- serverless-with-cloudfront
# The `provider` block defines where your service will be deployed
provider:
name: aws
runtime: nodejs12.x
region: ap-northeast-1
custom:
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
# The `functions` block defines what code to deploy
functions:
helloWorld:
handler: handler.helloWorld
# The `events` block defines how to trigger the handler.helloWorld code
events:
- httpApi: '*'
A REST api gateway sample
A Lambda Url sample
lambda
must be set to the same name of targetfunction
function
must seturl
property
custom:
withCloudFront:
type: lambda
lambda: helloWorld
...
functions:
helloWorld:
url: true
...
Configuration
field | must | default | - |
---|---|---|---|
type | * | - | http |
rest | |||
lambda | |||
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
domain: my-custom-domain.com
certificate: arn:aws:acm:us-east-1:000000000000:certificate/00000000-1111-2222-3333-444444444444
- If
hostedZoneId
is set,domain
(andcertificate
) also needs to be set
hostedZoneId: 11223344
domain: my-custom-domain.com
certificate: arn:aws:acm:us-east-1:000000000000:certificate/00000000-1111-2222-3333-444444444444
headers
can be[]
(default) or a list of headers (see CloudFront custom behaviour):
headers:
- Accept
- Accept-Encoding
- Accept-Language
- Authorization
IAM Policy
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.