Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9104

Serverless FrameworkでNode.jsのサーバーレスAPIをGCPにデプロイしてみた

$
0
0



この記事は、「Google Cloud Platform Advent Calendar 2020」の19日目の記事です。

Serverless FrameworkでNode.jsのサーバーレスAPIをGCPにデプロイしてみました :tada:


事前準備


事前設定ができていれば3コマンドでデプロイできます!


はじめに、Node.jsテンプレートでプロジェクトを作成します。

sls create -t google-nodejs -p sample

画像


次に、対象プロジェクトに移動しパッケージをインストールします。

cd sample
npm install

画像


作成されたプロジェクトを確認します。今回は、「serverless.yml」のみ変更します。

package.json

{"name":"sample","version":"0.1.0","description":"","main":"index.js","scripts":{"test":"echo \"Error: no test specified\"&& exit 1"},"author":"serverless.com","license":"MIT","devDependencies":{"serverless-google-cloudfunctions":"*"}}


index.js

'use strict';exports.http=(request,response)=>{response.status(200).send('Hello World!');};exports.event=(event,callback)=>{callback();};


serverless.yml

service:sampleprovider:name:googlestage:devruntime:nodejs10region:us-central1project:GCPのプロジェクト名credentials:~/.gcloud/keyfile.jsonframeworkVersion:'2'plugins:-serverless-google-cloudfunctionspackage:exclude:-node_modules/**-.gitignore-.git/**functions:first:handler:httpevents:-http:path

対象のGCPのプロジェクト名に変更。

project:GCPのプロジェクト名


プロジェクトが完成したらGCPにデプロイします。

sls deploy

画像


デプロイ後に、GCPのコンソールでCloud Functionsが作成されているのを確認してみます。

画像


「allUsers」を割り当ててAPIにアクセスできるように設定します。

画像

画像


表示されました :tada:

画像


Serverless FrameworkでNode.jsのサーバーレスAPIをGCPにデプロイできました :thumbsup:

Serverless Frameworkを利用することで、手軽にGCPにサーバーレス環境をデプロイできました :bulb:色々と設定できそうなので今後も試していきたいと思います :grinning:

以前にZappaでためした記事、「ZappaでDBもパッケージしたサーバーレスAPIを構築してみた」もあるので比較してみて頂ければと思います。


Serverless Frameworkについて、他にも記事を書いています。よろしければぜひ :bow:
tags - Serverless Framework

やってみたシリーズ :grinning:
tags - Try




book


Viewing all articles
Browse latest Browse all 9104

Latest Images

Trending Articles