Skip to content

Commit

Permalink
Merge branch 'serverless-deploy' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
oyilmaztekin committed Apr 7, 2019
2 parents f9003d1 + 27ab38f commit d1c4b3d
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 15 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"author": "özer yilmaztekin <yilmaztekin.ozer@gmail.com>",
"license": "UNLICENCED",
"private": true,
"engines": {
"node" : ">=0.12"
"engines": {
"node": ">=0.12"
},
"engineStrict":true,
"engineStrict": true,
"scripts": {
"dev": "esw . && node server.js",
"build": "next build",
Expand Down Expand Up @@ -68,7 +68,8 @@
"react-scroll-up": "^1.3.3",
"react-stickynode": "^2.1.1",
"serverless-http": "^1.9.1",
"serverless-offline": "^4.9.4"
"serverless-offline": "^4.9.4",
"serverless-s3-sync": "^1.7.3"
},
"devDependencies": {
"@babel/core": "^7.2.2",
Expand Down
169 changes: 162 additions & 7 deletions serverless.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,172 @@
service: serverless
service: nextjs-testing

package:
exclude:
- pages/**
- tests/**
- node_modules/.cache/**
- static/**
- .cache
- package.json
- yarn.lock
- package-lock.json
- "**.test.js"
- "__snapshots__/"
include:
- .next/**

plugins:
- serverless-offline
- serverless-s3-sync

custom:
endpointType: REGIONAL
region: "${opt:region, self:provider.region}"
stage: "${opt:stage, env:SLS_STAGE, 'dev'}"
serverless-offline:
port: 8000
dontPrintOutput: true
bucket_name: "${self:service}-${self:custom.stage}-static"
s3Sync:
- bucketName: ${self:custom.bucket_name}
bucketPrefix: static/
localDir: static
params:
- "**/*":
CacheControl: 'public, max-age=86400'
- bucketName: ${self:custom.bucket_name}
localDir: .next
bucketPrefix: _next/
params:
- "**/*":
CacheControl: 'public, max-age=31536000, immutable'
provider:
name: aws
runtime: nodejs8.10
region: eu-west-1
memorySize: 512
timeout: 15
environment:
NODE_ENV: production

plugins:
- serverless-offline

functions:
app:
nextJsApp:
handler: lambdas/serverless.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'
- http:
path: /
method: ANY
- http:
path: /{any+}
method: ANY

resources:
Description: Next.js testing in AWS lambda
Resources:
WebsiteBucketPolicy:
Type: "AWS::S3::BucketPolicy"
DependsOn:
- CDNOriginAccessIdentity
- WebsiteBucket
Properties:
Bucket: ${self:custom.bucket_name}
PolicyDocument:
Statement:
- Action:
- "s3:GetObject"
Effect: "Allow"
Resource:
Fn::Join:
- ""
- - Fn::GetAtt: [WebsiteBucket, Arn]
- "/*"
Principal:
CanonicalUser:
Fn::GetAtt: [CDNOriginAccessIdentity, S3CanonicalUserId]

CDNOriginAccessIdentity:
Type: "AWS::CloudFront::CloudFrontOriginAccessIdentity"
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment:
Fn::Join:
- ""
- - Ref: AWS::StackName
- "-origin-access-identity"
WebsiteBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Delete
Properties:
BucketName: ${self:custom.bucket_name}

CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
DependsOn:
- WebsiteBucket
Properties:
DistributionConfig:
Origins:
- DomainName:
Fn::GetAtt: [WebsiteBucket, DomainName]
Id: WebsiteBucket
S3OriginConfig:
OriginAccessIdentity:
Fn::Join:
- ""
- - "origin-access-identity/cloudfront/"
- Ref: CDNOriginAccessIdentity
- DomainName:
Fn::Join:
- ""
- - Ref: ApiGatewayRestApi
- ".execute-api."
- ${self:custom.region}
- ".amazonaws.com"
Id: APIGWOrigin
OriginPath: "/${self:custom.stage}"
CustomOriginConfig:
OriginKeepaliveTimeout: 60
OriginReadTimeout: 15
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: https-only
OriginSSLProtocols:
- 'TLSv1'
- 'TLSv1.1'
- 'TLSv1.2'
Enabled: 'true'
HttpVersion: http2
CacheBehaviors:
- PathPattern: /_next/*
AllowedMethods:
- GET
- HEAD
- OPTIONS
TargetOriginId: WebsiteBucket
Compress: true
ForwardedValues:
QueryString: false
ViewerProtocolPolicy: https-only
- PathPattern: /static/*
AllowedMethods:
- GET
- HEAD
- OPTIONS
TargetOriginId: WebsiteBucket
Compress: true
ForwardedValues:
QueryString: false
ViewerProtocolPolicy: https-only
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
TargetOriginId: APIGWOrigin
Compress: true
MinTTL: 60
DefaultTTL: 60
ForwardedValues:
QueryString: true
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
Loading

0 comments on commit d1c4b3d

Please sign in to comment.