Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
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
Building AI with AI
inesmontani
PRO
0
160
PHPライセンス変更の議論を通じて学ぶOSSライセンスの基礎
matsuo_atsushi
0
140
Claude Code on the Web を超える!? Codex Cloud の実践テク5選
sunagaku
0
530
SUZURIの規約違反チェックにおけるクリエイタフィードバックの試⾏錯誤/Trial and Error in Creator Feedback for SUZURI's Terms of Service Violation Checks
ae14watanabe
1
150
What’s Fair is FAIR: A Decentralised Future for WordPress Distribution
rmccue
0
170
Java_プロセスのメモリ監視の落とし穴_NMT_で見抜けない_glibc_キャッシュ問題_.pdf
ntt_dsol_java
0
170
チーム開発の “地ならし"
konifar
7
4.2k
The Missing Link in Angular's Signal Story: Resource API and httpResource
manfredsteyer
PRO
0
120
DartASTとその活用
sotaatos
2
120
Querying Design System デザインシステムの意思決定を支える構造検索
ikumatadokoro
1
1.1k
Feature Flags Suck! - KubeCon Atlanta 2025
phodgson
0
100
Honoを技術選定したAI要件定義プラットフォームAcsimでの意思決定
codenote
0
160
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
253
22k
Code Review Best Practice
trishagee
72
19k
[RailsConf 2023] Rails as a piece of cake
palkan
57
6.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
36
6.1k
How to train your dragon (web standard)
notwaldorf
97
6.4k
Designing for Performance
lara
610
69k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Why You Should Never Use an ORM
jnunemaker
PRO
60
9.6k
Six Lessons from altMBA
skipperchong
29
4.1k
Mobile First: as difficult as doing things right
swwweet
225
10k
Building Applications with DynamoDB
mza
96
6.8k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
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