【Serverless】はじめてのServerless Framework v.1 rc1 その2
はじめに
前回に引き続き、Serverless Frameworkを使った開発をしていきます。
【Serverless】はじめてのServerless Framework v.1 rc1 その1 - FujiYasuの日記
アーキテクチャ図
Cloudcraftというサービスを使い書いてみました。
https://cloudcraft.co/view/5d16ea58-14e0-439b-a8eb-18c047e9f39a?key=wIxzJw5VW44YEquqe4O7Sw
※今回は、ServerlessなのでAPI GatewayとAWS Lambdaの話がメインです。
環境
- OS X El capitan
- nodebrew 0.9.6
- Serverless v.1 rc1
前提条件
- 前回の記事まで読み進んでいる人
やってみた
はじめに
前回までのAPIは、URLがわかってしまうと誰でもAPIを叩いてしまうことができ
セキュリティ的に良くない状況となってしまいます。
また、API GatewayはAPIを実行された分だけお金がかかるので
いたずらにやられてしまうと大きな金額の請求がきてしまいます。
API Keyの作成
ここでは、API Keyを発行します。 GUIからでも作れますが、AWS CLIからKeyを発行するします。
aws apigateway create-api-key --name 'dev-my-first-serverless-rc1' --description 'Used for development' --enabled --region ap-northeast-1
serverless.ymlを編集
Keyが作成されたので、API Keyの名前をServerless側で記述します。 さらにAPIをAPI Keyを指定するため、event単位で「private: true」を設定します。
provider: name: aws apiKeys: - dev-my-first-serverless-rc1 functions: hello: handler: handler.hello events: - http: path: handler/hello method: get private: true
ドキュメントは、以下のページです。
デプロイ
ここまで設定したら、後はデプロイするだけです。
[0:00:46] Y_Fujikawa:my-first-serverless-rc1 $ serverless deploy -v erverless: Packaging service... Serverless: Removing old service versions... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading service .zip file to S3... Serverless: Updating Stack... Serverless: Checking Stack update progress... CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - my-first-serverless-rc1-dev CloudFormation - UPDATE_IN_PROGRESS - AWS::Lambda::Function - InsertLambdaFunction CloudFormation - UPDATE_IN_PROGRESS - AWS::Lambda::Function - HelloLambdaFunction CloudFormation - UPDATE_COMPLETE - AWS::Lambda::Function - InsertLambdaFunction CloudFormation - UPDATE_COMPLETE - AWS::Lambda::Function - HelloLambdaFunction CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1474902440291 CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1474902440291 CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Deployment - ApiGatewayDeployment1474902440291 CloudFormation - UPDATE_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - my-first-serverless-rc1-dev CloudFormation - DELETE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1474902169689 CloudFormation - DELETE_COMPLETE - AWS::ApiGateway::Deployment - ApiGatewayDeployment1474902169689 CloudFormation - UPDATE_COMPLETE - AWS::CloudFormation::Stack - my-first-serverless-rc1-dev Serverless: Stack update finished... Service Information service: my-first-serverless-rc1 stage: dev region: ap-northeast-1 endpoints: GET - https://xxxx.execute-api.ap-northeast-1.amazonaws.com/dev/handler/hello functions: my-first-serverless-dev-hello: arn:aws:lambda:ap-northeast-1:201892032205:function:my-first-serverless-rc-1dev-hello
動作確認
Endpointsも書かれているので、Curlコマンドを叩けばすぐ確認できます。
[0:03:27] Y_Fujikawa:my-first-serverless-rc1 $ curl https://xxxx.execute-api.ap-northeast-1.amazonaws.com/dev/handler/hello {"message": "Forbidden"}% [0:05:49] Y_Fujikawa:my-first-serverless-rc1 $ curl -H "x-api-key: <作成したAPI Key>" https://xxxx.execute-api.ap-northeast-1.amazonaws.com/dev/handler/hello {"message":"Go Serverless v1.0! Your function executed successfully!"}
まとめ
今回はAPI Keyの発行をAWS CLIから実行しましたが、Serverlessからでもできそうな感じがあります。
それでも、わざわざGUIから設定せずスムーズに作業をできるのはいいですね。