LoginSignup
0
0

More than 3 years have passed since last update.

node.jsとserverlessを使ってlambdaでアプリを動かす -複数の関数をlambdaにアップする-

Last updated at Posted at 2020-12-01

前回までのあらすじ

node.jsとserverlessでローカル環境のコードをlambdaにアップできましたとさ

目的

ローカル環境で作成した複数の関数をLambdaにアップして動かしたい

実践

環境

node.js v12.18.2

ディレクトリ構成

ls -a
.                       .gitignore              node_modules            package.json
..                      handler.js              package-lock.json       serverless.yml

package.jsonの確認

{
  "name": "application-no-namae-desu",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "serverless": "^2.13.0"
  }
}

handler.js


'use strict';

module.exports.hello = async event => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'Go Serverless v1.0! Your function executed successfully!',
        input: event,
      },
      null,
      2
    ),
  };

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};

前回までの状態を確認できたので、複数の関数をLambdaで使えるようにしていく。

handler.jsで複数エクスポートできるようにする

handler.jsを以下のように修正


'use strict';

async function hello() {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: "Go Serverless v1.0! Your function executed successfully!",
      },
      null,
      2
    ),
  };
};

// bonjour関数を追加、メッセージをフランス語にする
async function bonjour() {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: "Passez à la v1.0 sans serveur! Votre fonction s'est exécutée avec succès!",
      },
      null,
      2
    ),
  };
};


module.exports = { hello, bonjour };

Lambdaの関数として使用するためには、Lambdaファンクションの設定を行わなければならない。
設定はserverless.ymlで行う。

serverless.ymlの設定



service: application-no-namae-desu

frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x


functions:
  hello:
    handler: handler.hello

  # ここを追加
  bonjour:
      handler: handler.bonjour

bonjour関数をローカルで実行

sls invoke local --function bonjour
Serverless: Running "serverless" installed locally (in service node_modules)
{
    "statusCode": 200,
    "body": "{\n  \"message\": \"Passez à la v1.0 sans serveur! Votre fonction s'est exécutée avec succès!\"\n}"
}

いけそうなので、AWSにデプロイしよう。

AWSにデプロイ

serverless deploy --region ap-northeast-1

デプロイした関数を実行

sls invoke --function hello --region ap-northeast-1
Serverless: Running "serverless" installed locally (in service node_modules)
{
    "statusCode": 200,
    "body": "{\n  \"message\": \"Go Serverless v1.0! Your function executed successfully!\"\n}"
}
sls invoke --function bonjour --region ap-northeast-1
Serverless: Running "serverless" installed locally (in service node_modules)
{
    "statusCode": 200,
    "body": "{\n  \"message\": \"Passez à la v1.0 sans serveur! Votre fonction s'est exécutée avec succès!\"\n}"
}

おしまい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0