diff --git a/README.md b/README.md index 7133ecf..dced1a2 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ myWebsite: region: us-east-1 # The AWS region to deploy your website into bucketName: myBucket # (Optional) The Bucket name where `src` files/folder will be upload. # If not provided, it will create random bucket name and upload `src` files + cloudFront: # (Optional) + waitForCreateDistribution: true # (Optional) wait for create cloudfront distribution to complete + waitForUpdateDistribution: false # (Optional) wait for update cloudfront distribution to complete + customOrigin: true # (Optional) wait for custom origin to avoid s3 bucket redirect during cloudfront creation env: # Environment variables to include in a 'env.js' file with your uploaded code. API_URL: https://api.com @@ -84,3 +88,9 @@ $ serverless ### New to Components? Checkout the [Serverless Components](https://github.com/serverless/components) repo for more information. + +  + +### Publishing new versions +New versions of this fork should be published from the `master` branch +using `npm publish`. Remember to bump the package version as appropriate before publishing. \ No newline at end of file diff --git a/awsRegionUrls.json b/awsRegionUrls.json new file mode 100644 index 0000000..468f48f --- /dev/null +++ b/awsRegionUrls.json @@ -0,0 +1,22 @@ +{ + "us-east-2": "s3-website.us-east-2.amazonaws.com", + "us-east-1": "s3-website-us-east-1.amazonaws.com", + "us-west-1": "s3-website-us-west-1.amazonaws.com", + "us-west-2": "s3-website-us-west-2.amazonaws.com", + "ap-east-1": "s3-website.ap-east-1.amazonaws.com", + "ap-south-1": "s3-website.ap-south-1.amazonaws.com", + "ap-northeast-3": "s3-website.ap-northeast-3.amazonaws.com", + "ap-northeast-2": "s3-website.ap-northeast-2.amazonaws.com", + "ap-southeast-1": "s3-website-ap-southeast-1.amazonaws.com", + "ap-southeast-2": "s3-website-ap-southeast-2.amazonaws.com", + "ap-northeast-1": "s3-website-ap-northeast-1.amazonaws.com", + "ca-central-1": "s3-website.ca-central-1.amazonaws.com", + "cn-northwest-1": "s3-website.cn-northwest-1.amazonaws.com.cn", + "eu-central-1": "s3-website.eu-central-1.amazonaws.com", + "eu-west-1": "s3-website-eu-west-1.amazonaws.com", + "eu-west-2": "s3-website.eu-west-2.amazonaws.com", + "eu-west-3": "s3-website.eu-west-3.amazonaws.com", + "eu-north-1": "s3-website.eu-north-1.amazonaws.com", + "sa-east-1": "s3-website-sa-east-1.amazonaws.com", + "me-south-1": "s3-website.me-south-1.amazonaws.com" +} diff --git a/package.json b/package.json index eee006d..96e5cb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@serverless/website", - "version": "3.0.9", + "name": "@ublend-npm/serverless-website-component", + "version": "0.0.19", "main": "./serverless.js", "publishConfig": { "access": "public" @@ -14,8 +14,8 @@ "dependencies": { "@serverless/aws-s3": "^2.0.0", "@serverless/core": "^1.0.0", - "@serverless/domain": "^2.0.0", - "aws-sdk": "^2.499.0" + "@ublend-npm/serverless-compoonent-domain": "^0.0.8", + "aws-sdk": "^2.1259.0" }, "devDependencies": { "babel-eslint": "9.0.0", diff --git a/serverless.js b/serverless.js index bfc5657..5f51296 100644 --- a/serverless.js +++ b/serverless.js @@ -9,18 +9,20 @@ const { configureDomainForBucket, configureBucketForRedirect } = require('./utils') +const regionUrls = require('./awsRegionUrls') /* * Website */ class Website extends Component { - /** * Types */ - types() { return types } + types() { + return types + } /* * Default @@ -100,7 +102,7 @@ class Website extends Component { this.state.bucketName = inputs.bucketName this.state.region = inputs.region - this.state.url = `http://${bucketOutputs.name}.s3-website-${inputs.region}.amazonaws.com` + this.state.url = `http://${bucketOutputs.name}.${regionUrls[inputs.region]}` await this.save() const outputs = { @@ -110,16 +112,37 @@ class Website extends Component { // Configure custom domain, if specified if (inputs.domain) { - const domain = await this.load('@serverless/domain') + const domain = await this.load('@ublend-npm/serverless-compoonent-domain') const subdomain = inputs.domain.split('.')[0] const secondLevelDomain = inputs.domain.replace(`${subdomain}.`, '') const domainInputs = { + region: this.state.region, domain: secondLevelDomain, subdomains: {} } - domainInputs.subdomains[subdomain] = { url: this.state.url } + // eslint-disable-next-line prefer-destructuring + let cloudFront = inputs.cloudFront + const { institution } = inputs + if (inputs.securityHeaders) { + cloudFront = { + ...cloudFront, + customLambdaAssociations: [ + { + functionName: `${institution}-security-headers-injector-prod-injectHeaders`, + type: 'origin-response' + } + ] + } + } + + domainInputs.subdomains[subdomain] = { + url: this.state.url, + bucketName: this.state.bucketName, + cloudFront + } + const domainOutputs = await domain(domainInputs) outputs.domain = domainOutputs.domains[0] @@ -147,7 +170,7 @@ class Website extends Component { // Remove custom domain, if specified if (this.state.domain) { this.context.debug(`Removing custom domain.`) - const domain = await this.load('@serverless/domain') + const domain = await this.load('@ublend-npm/serverless-compoonent-domain') await domain.remove() } diff --git a/utils.js b/utils.js index 9d9ef3f..85d2910 100644 --- a/utils.js +++ b/utils.js @@ -1,6 +1,13 @@ const { utils } = require('@serverless/core') const configureBucketForHosting = async (s3, bucketName) => { + const publicAccessBlockConfig = { + BlockPublicAcls: false, + BlockPublicPolicy: false, + IgnorePublicAcls: false, + RestrictPublicBuckets: false + } + const s3BucketPolicy = { Version: '2012-10-17', Statement: [ @@ -41,6 +48,13 @@ const configureBucketForHosting = async (s3, bucketName) => { } try { + await s3 + .putPublicAccessBlock({ + Bucket: bucketName, + PublicAccessBlockConfiguration: publicAccessBlockConfig + }) + .promise() + await s3 .putBucketPolicy({ Bucket: bucketName,