serverless frameworkを使って、cloud functionを立ち上げてみた
何をするの?
serverless frameworkを使って、Cloud Functionを立ち上げてみました。
なぜやるの?
副業でcloudfunctionを使って、APIを構築することになったためです。
手動でscriptを構築するよりも、今後のことを考えると良いことがたくさんあるのと、AWS lambdaでは、利用したことがあったのですが、Cloud Functionではなかったので、良い機会だと思いTryしてみました。
何をしたの?
serverless frameworkを使って、cloud functionを立ち上げるまでにやったことを記載します。
導入で知っておくと良さそうなこと
「マジ初めてserverless frameworkとか聞きました!」って人向けに、さっと事前知っておいた方がいいことをまとめてみました。
そもそも「serverless framework」って?
Serverless Framework はServerless Applicationを構成管理デプロイするためのツールです。詳細は下記の記事を見ていただくのが良いかと思いますが
メリットとしては、上記の記事でも語られていますが
- コマンド1行で、さくっとDeployできる (もちろん、AWSやGCPとの認証するような設定はしないといけないですが)
- 構成管理が抜けがちな、FaaS(Function as a Service/ lambdaやCloud Functionのようなもの)の構成管理(≒ IaC化)が手軽にできる
- ローカルでテストでき、効率的に開発できる(自前でeventデータを作成し、任意のinputを用いて実行できます)
の3点かなと思っています。ローカルテストに関しては、こちらの記事でコマンドを確認すると良いかと思います。
以前、zappa
というツールを使ってみたことがあったのですが、aws lambda/ pythonでしか利用できないのに対し、serverless frameworkは様々なサービス(AWS/GCP)や言語(javascript/python/golang等)に対応している分、汎用性が高いツールです。メンテナンスも頻繁であるため、基本的にはこちらを使うのが良いかと思います。
やったこと
- この辺の記事を参考にしました
install方法
serverless frameworkのインストール
公式docを見るといくつか方法がありますが、今回は npm
を使って、Macにインストールしました。理由は、Reactを書いたり、typescriptを使うのでnode jsがインストールされているためです。npm = Node Package Manager
でnode jsのインストールが必要になります。node jsのインストールですが、僕はnodebrewを使ってイントールしました。 node js
ってなんぞや?って人は ここをみてください。
# グローバルインストール $ npm install -g serverless # serverlessの情報(エラーになってますが、`Your Environment Information ` をみていただければ、私の実行環境がわかるかと思います。 ) $ serverless info Serverless Error --------------------------------------- This command can only be run in a Serverless service directory. Make sure to reference a valid config file in the current working directory if you're using a custom config file Get Support -------------------------------------------- Docs: docs.serverless.com Bugs: github.com/serverless/serverless/issues Issues: forum.serverless.com Your Environment Information --------------------------- Operating System: darwin Node Version: 14.9.0 Framework Version: 1.82.0 Plugin Version: 3.8.3 SDK Version: 2.3.1 Components Version: 2.34.9
templateの作成
sls(serverless)コマンドを実行し、templateを作ります。実行すると、下記のようなファイルが作成されます。
# serverless frameworkを使うのに必要なモジュールのインストール $ npm install --save serverless-google-cloudfunctions # company-hpのディレクトリの下にpythonのtemplateを作成する $ sls create --template google-python --path company-hp # ディレクトリ構造 $ tree company-hp company-hp ├── main.py ├── package.json └── serverless.yml
cloud functionの設定
GCP認証突破のために、事前にサービスアカウントを作成し、そのCredential情報を用います。下記を参考にしながら作成しました。
設定ファイルである serverless.yml
を編集し、適切な設定に変更します。
$ cat serverless.yml| head -n 15 service: company-hp provider: name: google stage: dev runtime: python37 region: asia-northeast1 # regionを東京に指定する project: xxxxxxx # 自身の利用するProject名に変更する # The GCF credentials can be a little tricky to set up. Luckily we've documented this for you here: # https://serverless.com/framework/docs/providers/google/guide/credentials/ # # the path to the credentials file needs to be absolute credentials: ~/.gcloud/test.json # サービスアカウントのアカウントkey(json file形式)への絶対パスを記載する frameworkVersion: '1'
deploy
早速deployをしようとしたのですが、エラーになってしまいました。
$ sls deploy ==省略== Error -------------------------------------------------- Error: Cloud Deployment Manager V2 API has not been used in project xxxxxxx before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/deploymentmanager.googleapis.com/overview?project=xxxxxxx then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
どうやら、APIの有効化の手順が抜けていたようなので実行します。
$ gcloud services enable deploymentmanager.googleapis.com cloudfunctions.googleapis.com storage-component.googleapis.com logging.googleapis.com
gcloudコマンドインストール、および初期設定できていない方は、こちらを参考に実施すると良いと思います。
APIを設定すると、無事deployできました。
$ sls deploy ==省略== Serverless: Done... Service Information service: company-hp project: xxxxx stage: dev region: asia-northeast1 Deployed functions first https://asia-northeast1-xxxxx.cloudfunctions.net/company-hp-dev-first ******************************************************************************************************************** Serverless: Announcing Metrics, CI/CD, Secrets and more built into Serverless Framework. Run "serverless login" to activate for free.. ******************************************************************************************************************** ......
まとめ
さくっと、GCPでもserverless frameworkでCloud Function Deployできました。結構、lambdaとかCloud Functionは作って放置されがちなんですが、お手軽に構成管理もテストもできるので是非使ってみてはいかがでしょうか?
また、以前 AWS AppSyncを使って、GraphQLのバックエンドをserverless frameworkを使って作りました。よりスピーディな現場では、このような小さく初めてスケールする仕組みを使っており、アプリケーションエンジニアのみでほぼ全てが完結する世界も広がってるなーと思いました(Gloud ServiceのIAMの適切な設定等では、クラウドエンジニアは必要ですが)。今度時間があれば、その時の話でも書こうと思います。