Slide 1

Slide 1 text

S e r v e r l e s s f o r F ro n t - e n d S i n g l e P a g e A p p l i c a t i o n 2 0 1 8 . 2 . 2 2 S e r v e r l e s s M e e t u p To k y o # 7

Slide 2

Slide 2 text

P ro f i l e • Front-end engineer at PERSOL • GitHub: boiyaa
 Twitter: boiyaaaaaa

Slide 3

Slide 3 text

I am not familiar with infrastructure. But I would like to deliver front-end apps reliably.

Slide 4

Slide 4 text

Infrastructure Requirements It can route all requests to one HTML file. ʴ Ops (availability, security, etc) are managed. S PA U n f a m i l i a r w i t h o p s

Slide 5

Slide 5 text

O p t i o n s I t r i e d • Amazon CloudFront + Amazon S3 • Google App Engine • Firebase Hosting

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

P ro c e d u re o f b u i l d i n g i n f r a s t r u c t u re 1. Create S3 Bucket 2. Create CloudFront OAI 3. Configure Bucket Policy 4. Configure CloudFront Distribution 5. Create CloudFront Distribution ෳ ࡶ C o m p l i c a t e d

Slide 8

Slide 8 text

bucket-policy.json { "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity [OAI_ID]" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::[BUCKET_NAME]/*" }, { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity [OAI_ID]" }, "Action": "s3:ListBucket", "Resource": "arn:aws:s3:::[BUCKET_NAME]" } ] } distribution.json { "Comment": "", "Origins": { "Items": [ { "S3OriginConfig": { "OriginAccessIdentity": "origin-access-identity/cloudfront/[OAI_ID]" }, "Id": "origin", "DomainName": "[BUCKET_NAME].s3.amazonaws.com" } ], "Quantity": 1 }, "DefaultRootObject": "index.html", "PriceClass": "PriceClass_All", "Enabled": true, "DefaultCacheBehavior": { "TrustedSigners": { "Enabled": false, "Quantity": 0 }, "TargetOriginId": "origin", "ViewerProtocolPolicy": "allow-all", "ForwardedValues": { "Cookies": { "Forward": "none" }, "QueryString": false }, "MinTTL": 0, "Compress": true }, "CallerReference": "distribution-[BUCKET_NAME]", "CustomErrorResponses": { "Items": [ { "ErrorCode": 404, "ResponsePagePath": "/index.html", "ResponseCode": "200" } ], "Quantity": 1 } }

Slide 9

Slide 9 text

D e p l o y m e n t $ aws s3 sync build s3://[BUCKET_NAME] upload: build/index.html to s3://BUCKET_NAME/index.html upload: build/184e3117cf2cd3fa28a1f654dc5d4785.js to s3:// BUCKET_NAME/184e3117cf2cd3fa28a1f654dc5d4785.js Add the following option not to cache in development --cache-control "no-store, no-cache”

Slide 10

Slide 10 text

Pros • Flexible CDN cache control. • Flexible access control with AWS WAF. Cons • The procedure is complicated. • It takes time to understand configurations. • Should use SAM or Terraform.

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

P ro c e d u re o f b u i l d i n g i n f r a s t r u c t u re 1. Create a new project 2. Switch the project 3. Create App Engine 4. Create app.yaml for static website

Slide 13

Slide 13 text

runtime: php55 handlers: - url: /(.*\.(js|css|jpg|png|ico|svg))$ static_files: build/\1 upload: build/(.*\.(js|css|jpg|png|ico|svg))$ - url: /.* static_files: build/index.html upload: build/index.html skip_files: - ^(?!.*build).*$

Slide 14

Slide 14 text

D e p l o y m e n t $ gcloud app deploy Services to deploy: descriptor: […] source: […] target project: [PROJECT_NAME] target service: [default] target version: [20180222t003136] target url: [https://PROJECT_NAME.appspot.com] Do you want to continue (Y/n)? D o n o t f o rg e t p ro j e c t - s w i t c h i n g

Slide 15

Slide 15 text

Pros • Flexible routing configuration. • IP-address-based access control. • Configuring custom domain is also easy and cert is full-managed. • The latency between US and Tokyo is low. Cons • No CDN. • Cannot manage with tools such as Terraform.

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

P ro c e d u re o f b u i l d i n g i n f r a s t r u c t u re 1. Create Firebase configuration file 2. Create a new project 3. Associate the project

Slide 18

Slide 18 text

$ firebase init ? Which Firebase CLI features do you want to setup… ̋ Database: Deploy Firebase Realtime Database Rules ̋ Firestore: Deploy rules and create indexes for Firestore ̋ Functions: Configure and deploy Cloud Functions ›‒ Hosting: Configure and deploy Firebase Hosting sites ̋ Storage: Deploy Cloud Storage security rules ? Select a default Firebase project for this directory: [don't setup a default project] › [create a new project] ? What do you want to use as your public directory? public ? Configure as a single-page app (rewrite all urls to /index.html)? Yes A u t o c o n f i g u r a t i o n f o r S PA

Slide 19

Slide 19 text

σ ϓ ϩ Π $ firebase deploy === Deploying to ‘PROJECT_NAME'... i deploying hosting i hosting: preparing build directory for upload... ✔ hosting: 2 files uploaded successfully ✔ Deploy complete! Project Console: https://console.firebase.google.com/project/ PROJECT_NAME/overview Hosting URL: https://PROJECT_NAME.firebaseapp.com E a s y

Slide 20

Slide 20 text

Pros • Pretty easy. • Easy GUI and CLI. • Easy CDN cache control and immediate provisioning. • Configuring custom domain is also easy and cert is full-managed. Cons • Necessary to visit web console in construction procedure. • No access control system. • Cannot manage with tools such as Terraform.

Slide 21

Slide 21 text

C o n c l u s i o n s • I prefer Firebase. • CloudFront + S3 in the case of managing with CloudFormation or Terraform • App Engine if backend is on App Engine Standard • Next theme is serverless server-side rendering.

Slide 22

Slide 22 text

https://github.com/boiyaa/frontend-serverless-patterns

Slide 23

Slide 23 text

No content