$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Node.js_で_CloudFormation_のテンプレートを分割して管理する.pdf
Search
藤巻商店
September 08, 2018
Programming
0
550
Node.js_で_CloudFormation_のテンプレートを分割して管理する.pdf
藤巻商店
September 08, 2018
Tweet
Share
Other Decks in Programming
See All in Programming
251126 TestState APIってなんだっけ?Step Functionsテストどう変わる?
east_takumi
0
300
AIエンジニアリングのご紹介 / Introduction to AI Engineering
rkaga
2
1.1k
Querying Design System デザインシステムの意思決定を支える構造検索
ikumatadokoro
1
1.2k
AWS CDKの推しポイントN選
akihisaikeda
1
240
「文字列→日付」の落とし穴 〜Ruby Date.parseの意外な挙動〜
sg4k0
0
360
社内オペレーション改善のためのTypeScript / TSKaigi Hokuriku 2025
dachi023
1
480
TUIライブラリつくってみた / i-just-make-TUI-library
kazto
1
310
俺流レスポンシブコーディング 2025
tak_dcxi
13
7.8k
AI時代もSEOを頑張っている話
shirahama_x
0
230
『実践MLOps』から学ぶ DevOps for ML
nsakki55
2
550
20 years of Symfony, what's next?
fabpot
2
310
AIコードレビューがチームの"文脈"を 読めるようになるまで
marutaku
0
310
Featured
See All Featured
It's Worth the Effort
3n
187
29k
Side Projects
sachag
455
43k
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
Embracing the Ebb and Flow
colly
88
4.9k
Designing for humans not robots
tammielis
254
26k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
960
Writing Fast Ruby
sferik
630
62k
A Tale of Four Properties
chriscoyier
162
23k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.1k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.8k
Designing Experiences People Love
moore
142
24k
Transcript
Node.js で CloudFormation の テンプレートを分割管理する
自己紹介 藤巻商店(@fujimakishouten) • 株式会社 Eq • Debian GNU/Linux をデスクトップ環境で愛用 •
交互浴が好きで週に5〜6回銭湯に行く • 恵海人
株式会社 Eq について • サーバーを見られるエンジニアは2名 • サーバー管理のコストを下げたい • 小さなチームなので、効率よく開発したい ◦
AWS のクラウドサービスを組み合わせて開発 ◦ Lambda、APIGateway、DynamoDB などを使用
株式会社 Eq について • 環境構築には CloudFormation を選択 ◦ SAM を利用する前提
◦ SAM-Local で Lambda のテストをする予定だった
CloudFormation • テンプレートが大きくなりやすい ◦ DynamoDB のオートスケールをしようとすると 5つくらい設定が必要 ◦ リソースによっては設定する項目も多い •
ごちゃごちゃして見通しが悪くなる
CloudFormation テンプレートを分割して管理したい
テンプレートを分割する方法 • 検索すると2つの方法が見つかった ◦ AWS::Include ◦ AWS::CloudFormation::Stack • 予め S3
に置いてあるテンプレートを 読み込んで利用できるらしい
テンプレートを分割する方法 • S3 にテンプレートを置くのが面倒 ◦ できればスタックを1つにしたい ◦ とりあえず動かして試したいだけなのに、 アップロードする仕組みをつくらないとダメなの?
面倒くさいところ • テンプレートを JSON で書く場合、 プロパティ名をクォートする必要がある ◦ JavaScript ならこんな感じで書くことが可能 {
Type: 'AWS::S3::Bucket', Properties: { BucketName: 'documents.honoka.io', AccessControl: 'Private' } };
Node.js を使ってテンプレートを結合する JavaScript で書いて、 JSON.stringify() すればいいのでは!?
Node.js を使ってテンプレートを結合する ./templates/resources/FunctionApi.js module.exports = { Type: 'AWS::Lambda::Function', Properties: {
Code: './dest/api', Description: 'honoka.io JSON-RPC 2.0 API', FunctionName: 'honoka-api', Handler: 'index.handler', Role: { 'Fn::GetAtt': ['RoleLambda', 'Arn'] }, Runtime: 'nodejs8.10' } };
Node.js を使ってテンプレートを結合する ./templates/resources/index.js module.exports = fs.readdirSync(__dirname).reduce((collection, filename) => { if
('index.js' === filename) { return collection; } const name = filename.replace(path.extname(filename), ''); collection[name] = require(path.resolve(__dirname, name)); return collection; }, {});
Node.js を使ってテンプレートを結合する ./templates/index.js const converter = require('../lib/converter/resources'); module.exports = {
AWSTemplateFormatVersion: '2010-09-09', Parameters: {}, Conditions: {}, Resources: converter({}, require('./resources')) };
Node.js を使ってテンプレートを結合する ./package.json "scripts": { "template": "node -e \"console.log(JSON.stringify(require('./templates'), null,
4));\" > template.json" } npm run template を実行すると、 template.json が出力されるようにしている
できるようになったこと • テンプレートを分割して管理できた ◦ サブディレクトリを作ることも可能
できるようになったこと • テンプレートに JavaScript が書ける ◦ 環境に合わせて設定ファイルを変えたり const config =
require(path.resolve(__dirname, '../../config', process.env.environment)); ◦ 外部ファイルを読み込んだり RequestTemplates: { 'application/json': fs.readFileSync(path.resolve(__dirname, '../mapping/JSONRequest')).toString('UTF-8') }
まとめ • 内容がすっきりして、見通しがよくなった ◦ 見たい部分だけに注目できる ◦ エラーが出ている部分や、 変更が必要な部分が見つけやすくなった ◦ テンプレートがきれいに書ける
• 面倒くさく無くなった!
まとめ CloudFormation を使っていて、 同じようなことで困っている方の、 参考になれば嬉しいです。
参考 URL サンプルを公開しています https://github.com/honokaio/honoka-api-base