Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serverless Website Component: Added CloudFront Feture #5

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
22 changes: 22 additions & 0 deletions awsRegionUrls.json
Original file line number Diff line number Diff line change
@@ -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"
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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",
Expand Down
35 changes: 29 additions & 6 deletions serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand All @@ -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]
Expand Down Expand Up @@ -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()
}

Expand Down
14 changes: 14 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -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: [
Expand Down Expand Up @@ -41,6 +48,13 @@ const configureBucketForHosting = async (s3, bucketName) => {
}

try {
await s3
.putPublicAccessBlock({
Bucket: bucketName,
PublicAccessBlockConfiguration: publicAccessBlockConfig
})
.promise()

await s3
.putBucketPolicy({
Bucket: bucketName,
Expand Down