A serverless plugin to create an S3 bucket to hosting your static website, it will automatically set public permissions and provide a specific deploy command.
$ npm install serverless-plugin-static-website --save-dev
or using yarn
$ yarn add serverless-plugin-static-website
plugins:
- serverless-plugin-static-website
custom:
frontend:
bucket: ${self:service}-${self:provider.stage} # Provide a bucket name
dir: './frontend' # Set frontend directory to publish
cacheControl: '31536000' # Set max-age cache control, default: 31536000
deploy: 'sync' # Set deploy strategy to cp (upload every time all files), or sync (upload only newer files), default: sync
If you want to enable CloudFront distribution in front of S3 bucket add cdn
configurations to custom.frontend
key:
custom:
frontend:
# ...frontend configuration..
cdn:
enabled: true
aliases: # Set aliases, default empty (default CloudFront domain)
- www.mywebsite.com
priceClass: PriceClass_All # Set price class, default: PriceClass_100
certificate: 'arn:aws:acm:region:account-id:certificate/certificate-id' # Set custom certificate
At the end of serverless deploy
you will see inside "Service Information" section the websites bucket's URL:
Service Information
service: myservice
stage: dev
region: eu-west-1
stack: myservice-dev
resources: 6
frontend: http://myservice-test.s3-website-eu-west-1.amazonaws.com <-- here thr URL to visit
api keys:
None
endpoints:
None
functions:
hello: myservice-test-hello
layers:
None
Deploy static websites files:
$ serverless deploy frontend
this will sync your configured frontend directory with S3 bucket.
Note: this command is a wrapper of aws s3 cp <local path> <bucket> --recursive
, so you must have the AWS CLI installed.
Remove all objects from bucket:
$ serverless remove frontend
When you destroy the environment with serverless remove
the created bucket will be emptied automatically to permit the deletion.