0
0

More than 3 years have passed since last update.

AWS CDKのフォルダ階層の変更をしたメモ

Posted at

概要

前回までにApiGatewayが使えるまでになった。
ここで、「形で考えるサーバーレス設計」のCDKテンプレートとフォルダ構造がずれていたので、揃えてみる。

ソースコード

修正

  • cdk以下にaws cdk関係のファイルを移動
  • フォルダ階層を反映する必要があるものは修正

setup

cdk/lib/process/setup.ts
#!/usr/bin/env node
import * as childProcess from 'child_process';
import * as fs from 'fs-extra';
import * as path from 'path'

const bundlePath = './bundle';
export const NODE_LAMBDA_LAYER_DIR = path.resolve(process.cwd(), bundlePath);
const NODE_LAMBDA_LAYER_RUNTIME_DIR_NAME = `nodejs`;
const runtimeDirName = path.resolve(process.cwd(), `${bundlePath}/${NODE_LAMBDA_LAYER_RUNTIME_DIR_NAME}`);
const distFilePath = (file: string) => path.resolve(process.cwd(), `${bundlePath}/${NODE_LAMBDA_LAYER_RUNTIME_DIR_NAME}/${file}`)
- const srcFilePath = (file: string) => path.resolve(`${process.cwd()}/${file}`)
+ const srcFilePath = (file: string) => path.resolve(`${process.cwd()}/../${file}`)

export const bundleNpm = () => {
  // create bundle directory
  copyPackageJson();

stack

cdk/lib/sample-stack.ts
import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import * as apigateway from '@aws-cdk/aws-apigateway'
import { NODE_LAMBDA_LAYER_DIR } from './process/setup';

export class SampleStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
+    const entryHandlerDir = '../src/lambda/handlers/';
    const helloFunction = new NodejsFunction(this, 'hello', {
      runtime: lambda.Runtime.NODEJS_14_X,
-      entry: `src/lambda/handlers/hello.ts`,
+      entry: `${entryHandlerDir}/hello.ts`,
      functionName: 'kotahello',
      handler: 'lambdaHandler'
    });

参考

Website & Mobile starter project - github
サーバーレスをこれから始める方へ!「形で考えるサーバーレス設計」のCDKテンプレート(web/mBaaS編)を試して解説してみた
【AWS】 CDKの始め方

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