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
540
Node.js_で_CloudFormation_のテンプレートを分割して管理する.pdf
藤巻商店
September 08, 2018
Tweet
Share
Other Decks in Programming
See All in Programming
CJK and Unicode From a PHP Committer
youkidearitai
PRO
0
110
How Android Uses Data Structures Behind The Scenes
l2hyunwoo
0
440
Ruby×iOSアプリ開発 ~共に歩んだエコシステムの物語~
temoki
0
270
🔨 小さなビルドシステムを作る
momeemt
4
680
Processing Gem ベースの、2D レトロゲームエンジンの開発
tokujiros
2
130
Azure SRE Agentで運用は楽になるのか?
kkamegawa
0
2.2k
パッケージ設計の黒魔術/Kyoto.go#63
lufia
3
430
go test -json そして testing.T.Attr / Kyoto.go #63
utgwkk
3
300
AIと私たちの学習の変化を考える - Claude Codeの学習モードを例に
azukiazusa1
10
3.9k
Zendeskのチケットを Amazon Bedrockで 解析した
ryokosuge
3
300
Ruby Parser progress report 2025
yui_knk
1
440
Deep Dive into Kotlin Flow
jmatsu
1
330
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
525
40k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Writing Fast Ruby
sferik
628
62k
Music & Morning Musume
bryan
46
6.8k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
13k
We Have a Design System, Now What?
morganepeng
53
7.8k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
61k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Mobile First: as difficult as doing things right
swwweet
224
9.9k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.4k
How GitHub (no longer) Works
holman
315
140k
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