39
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Expressで作ったアプリをAWS Lambdaで動かすまで

Last updated at Posted at 2019-02-10

概要

  • 最近expressで作ったアプリをLambdaで動かすことが多いので手順をメモしておくもの

手順

Expressアプリの作成

  • 雛形の作成
mkdir express-sample
cd express-sample
yarn init -y
yarn add express
touch index.js
  • index.jsを最小限作る
index.js
const express = require('express');
const app = express();

app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept',
  );
  next();
});

app.get('/', (req, res) => {
  res.send('Hello');
});

const port = '8080';
app.listen(port, () => {
  console.log(`app start listening on port ${port}`);
});
  • 起動して動作確認
node index.js
open http://localhost:8080
  • ブラウザにHelloが表示されればOK

Lambdaへデプロイ

  • ServerlessFrameworkの準備
touch serverless.yml
yarn add serverless-http
  • sreverless.ymlに設定を記載
serverless.yml
service: express-sample
provider:
  name: aws
  runtime: nodejs8.10
  region: ap-northeast-1
functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'
  • index.jsを修正
    • 一行目と最終行を追加
index.js
const serverless = require('serverless-http'); // <- 追加
const express = require('express');
const app = express();

app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept',
  );
  next();
});

app.get('/', (req, res) => {
  res.send('Hello');
});

const port = '8080';
app.listen(port, () => {
  console.log(`app start listening on port ${port}`);
});

module.exports.handler = serverless(app);  // <- 追加
  • awsのkey情報を設定していることを確認
cat ~/.aws/credentials
[default]
aws_access_key_id = xxxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
output = json
region = ap-northeast-1
  • 以下のコマンドでデプロイ
    • このコマンドだけでエンドポイント生成やS3へのバックアップ等々全部やってくれる!超便利!
npx serverless deploy
  • 以下のようなログが表示される
    • endpointsのURLにアクセスしてHelloが返ればOK
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service express-sample.zip file to S3 (663.48 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..........
Serverless: Stack update finished...
Service Information
service: express-sample
stage: dev
region: ap-northeast-1
stack: express-sample-dev
resources: 11
api keys:
  None
endpoints:
  ANY - https://la0umc61i3.execute-api.ap-northeast-1.amazonaws.com/dev
  ANY - https://la0umc61i3.execute-api.ap-northeast-1.amazonaws.com/dev/{proxy+}
functions:
  app: express-sample-dev-app
layers:
  None

開発サイクル

  • ※もっといいやりかたあれば教えて下さい
  • ローカルでの開発はnode index.jsで起動できるのでそれで進める
  • Lambdaへのデプロイはnpx serverless deployすればLambdaへ反映される

アプリの削除

  • 不要なアプリは以下のコマンドで削除できる
    • AWSコンソールから手動で消すとゴミが残ってややこしくなるので注意
npx serverless remove
39
33
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
39
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?