Home
Slides
Blog
slide.seike460.com
Home
Slides
Blog
Home
Slides
Serverless Fukuoka #2
Serverless Fukuoka #2
Serverless Fukuoka #2
2018年10月19日
Serverless
Serverless
DX
Architecture
Keyboard Shortcuts
←
→
Navigate slides
Space
Next slide
F
Fullscreen
ESC
Exit fullscreen
Home
First slide
End
Last slide
B
.
Pause
S
Speaker notes
?
Reveal.js help
Close
## Serverless Framework Production Deploy Fusic Co.Ltd. 清家史郎(@seike460)
###### Who? Fusic Co., Ltd.  清家史郎  @seike460    
### tech - Program Language - PHP - Go - Elixir - infrastructure - Server - infrastructure as code - Network - Serverless(AWS)
###### products ##### Go - s3ry (s3 prompt cli) - https://github.com/seike460/s3ry - utakata (Serverless Slack Notificatier) - https://github.com/seike460/utakata
##### Community - PHP - fukuoka.php - Go - fukuoka.go - Elixir - fukuoka.ex
###### Organizer - Serverless Meetup Fukuoka
Serverless Framework
初めて聞いた 🤔
Hello World してみた 😶
開発に使ってる 🙂
Production Deploy 🤩
#### Agenda - Serverless Frameworkについて - Dev環境 Deploy - Production環境 Deploy - おまけ(時間があれば)
Serverless Frameworkとは ❓ - Everything you need to operationalize serverless development
Serverless 開発に必要なもの 全てを操作する事が出来る
#### Supports Top Cloud Providers - Amazon Web Services - Microsoft Azure - Google Cloud Platform - IBM Cloud - Kubernetes
- 何が出来るの? - Serverless Application 構成管理 - 何が嬉しいの? - infrastructure as code - Consoleで行う複雑な設定の排除 - CloudFormationじゃ駄目なの? - CloudFormationよりも簡単 - Plugin等の付加価値 - どんなリソース作れるの?
- AWS IAM - AWS IoT - AWS Lambda - Alexa Skill - Amazon API Gateway - Amazon CloudWatch - Amazon Cognito - Amazon DynamoDB - Amazon S3 - Amazon SNS - Amazon SQS (まだあります)
#### 構成例 
#### Dev環境 - Install - AWSの設定 - serverless.yml作成(設定ファイル) - Deploy
#### Install ##### Global Install - npm ``` npm install -g serverless ``` - yarn ``` yarn global add serverless ``` ##### Local Install - npm ``` npm install serverless ``` - yarn ``` yarn add serverless ```
scripts設定 - package.json ``` "scripts": { 〜 "deploy": "yarn install && ./node_modules/serverless/bin/serverless deploy", "createTemplate": "./node_modules/serverless/bin/serverless create --template", "destroy": "./node_modules/serverless/bin/serverless remove", "info": "./node_modules/serverless/bin/serverless info", 〜 ```
#### AWSの設定 - create an IAM User with Admin access ``` https://serverless.com/framework/docs/providers/aws/guide/credentials#creating-aws-access-keys ``` - aws configure ``` [seike460] aws_access_key_id=XxxxxxxxxxxxxxxxxxxX aws_secret_access_key=YyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyY ```
#### serverless.yml 以下コマンドでテンプレートが作成可能 ``` serverless create --template aws-go-dep ```
#### Deploy - default利用 ``` serverless deploy ``` - AWS_PROFILEを指定 (例:seike460) ``` AWS_PROFILE=seike460 serverless deploy ``` ``` serverless deploy --aws-profile seike460 ```
``` Serverless: Packaging service... Serverless: Excluding development dependencies... Serverless: Creating Stack... Serverless: Checking Stack create progress... ..... Serverless: Stack create finished... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading artifacts... Serverless: Uploading service .zip file to S3 (4.5 MB)... Serverless: Validating template... Serverless: Updating Stack... Serverless: Checking Stack update progress... ................................................ Serverless: Stack update finished... Service Information service: aws-go-dep stage: dev region: us-east-1 stack: aws-go-dep-dev api keys: None endpoints: GET - https://lfnk9lzhll.execute-api.us-east-1.amazonaws.com/dev/hello GET - https://lfnk9lzhll.execute-api.us-east-1.amazonaws.com/dev/world functions: hello: aws-go-dep-dev-hello world: aws-go-dep-dev-world ```
#### Production環境 - Production Deployするにあたっての課題 - 環境の切り分け - Test - ログ管理 - アクセス制限(プライベートなAPI)
#### 環境の切り分け API Gateway における stage 以下の場合`dev`環境にDeploy ``` provider: stage: dev ``` 設定ファイルを毎回更新するのは面倒、煩雑、事故の元
Deploy時オプション指定 ``` serverless deploy --stage production ``` ``` provider: stage: ${opt:stage, 'dev'} ``` ※第二引数は初期値
Deployする環境の切り替えは出来た! 一方で環境に応じて設定値の変更は必ず発生する
#### 環境変数 環境変数は以下形式にて設定 ``` provider: environment: SaveDataBucket: seike460Data ``` もちろん`stage`に応じた値に切り替えたい
以下の様な設定ファイルを用意 ``` config/dev.json config/production.json ``` serverless.ymlの記述方法として `${self:provider.stage}`にて実行時のstageを取得出来る ``` provider: environment: SaveDataBucket: ${file(config/${self:provider.stage}.json):SaveDataBucket} ``` configの内容 ``` { "SaveDataBucket":"seike460DataProduction", } ```
#### Test - プログラムは手元にある - Localテスト出来るものは実際にTestを書く - 直接APIリクエストを送ってdebugしたい時 - serverless invoke -f 【関数名】 - Postman、mockmock等のAPIリクエストを行える別ソフトを使う - CIとの連携 - LocalStack(AWSサービスを再現)を使う - 実際にCI用のリソースを立てる
#### Logging - Lambda内でLog転送(S3等) - CloudWatch Logs
CloudWatch Logs ``` provider: cfLogs: true ``` ``` START RequestId: 4341cd2b-a04a-11e8-9dfd-a98371069cb3 Version: $LATEST 2018/08/15 05:15:45 Nothing SaveKey: XxxxxX YyyyyY ZzzzZ END RequestId: 4341cd2b-a04a-11e8-9dfd-a98371069cb3 REPORT RequestId: 4341cd2b-a04a-11e8-9dfd-a98371069cb3 Duration: 1.03 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 32 MB ```
#### APIKey - APIKeyの設定も可能 ``` provider: apiKeys: - seike460Key ``` 出力されたAPIKEYをHTTPHeader[X-Api-Key]に設定 -> APIアクセスが可能
#### IP制限 Amazon API Gateway のアクセスポリシーを設定 Serverless Framework 1.28.0にて対応 ``` resourcePolicy: - Effect: Allow Principal: "*" Action: execute-api:Invoke Resource: - execute-api:/*/*/* Condition: IpAddress: aws:SourceIp: - "8.8.8.8" ```
VPCエンドポイント ``` provider: endpointType: private ```  ※ただし現状はVPCのエンドポイント設定は自分でやらないといけない
#### 纏め - Serverless Framework を活用して Serverless Application 構成管理 - Serverless 管理レスなのが魅力、もっと管理をツールに寄せよう - アプリケーションに集中して、 Serverless Application の価値向上
おまけ やってみたい人にオススメのQiita https://qiita.com/horike37/items/b295a91908fcfd4033a2
`serverless logs -f 【関数名】 -t` CloudWatch のログをtail出来る (くっそ便利、はよ知りたかった ``` START RequestId: 4341cd2b-a04a-11e8-9dfd-a98371069cb3 Version: $LATEST 2018/08/15 05:15:45 Nothing SaveKey: XxxxxX YyyyyY ZzzzZ END RequestId: 4341cd2b-a04a-11e8-9dfd-a98371069cb3 REPORT RequestId: 4341cd2b-a04a-11e8-9dfd-a98371069cb3 Duration: 1.03 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 32 MB START RequestId: 33517d8e-a04b-11e8-a2c1-a9d5aa9c2e7a Version: $LATEST 2018/08/15 05:15:45 Nothing SaveKey: XxxxxX ZzzzZ END RequestId: 33517d8e-a04b-11e8-a2c1-a9d5aa9c2e7a REPORT RequestId: 33517d8e-a04b-11e8-a2c1-a9d5aa9c2e7a Duration: 17.24 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 32 MB START RequestId: ad80a2bb-a04b-11e8-afbd-e5259e279b10 Version: $LATEST 2018/08/15 05:15:45 Nothing SaveKey: ZzzzZ END RequestId: ad80a2bb-a04b-11e8-afbd-e5259e279b10 REPORT RequestId: ad80a2bb-a04b-11e8-afbd-e5259e279b10 Duration: 13.57 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 32 MB ````
AWS SAM - AWS 公式 (CloudFormationの形式の一つ) - AWS関連サービスとの親和性が高い - ローカル環境でのテストが可能 https://github.com/awslabs/serverless-application-model
Thank you! Fusicは技術が大好きなエンジニアを募集しています  https://fusic.github.io/
Swipe to navigate
Previous
Next
Related Slides
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス
2024/4/1
View
実践サーバーレスパフォーマンスチューニング
2024/2/1
View
Secure Serverless Architecture
2023/10/1
View