Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
TerraformリファクタリングでSGを消した話
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
KiyamaMizuki
March 10, 2026
Technology
80
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
TerraformリファクタリングでSGを消した話
KiyamaMizuki
March 10, 2026
Transcript
12 slides
Terraformリファクタリングで SGを消した話 木山瑞基 2026/03/10(火) AWS業務の体験談 ~成功も失敗も丸ごとシェアするLT大会~ 1
自己紹介 名前:木山瑞基 所属:シナジーマーケティング株式会社 業務:CRMシステムのインフラ運用を行っ ています AWS / Mail server 趣味:ボルダリング、旅行
2
がっつりTerraformの話になってしまいますが、 「AWS業務の体験談」ということで許し てください! 3
目次 1. 前提1: Terraformの仕組み 2. 前提2: Terraformのリソース定義 3. やろうとしたこと 4.
実際のコード例(修正前 / 修正後) 5. 何が起きたか 6. なぜ起きたか 7. まとめ・教訓 4
前提1:Terraformの仕組み [※ ] TerraformはStateファイルで現在の状態を管理し、.tfファイルとの差分をAWS APIで反映する 5
前提2:Terraformのリソース定義 予約語であるリソースタイプとユーザーが定義するリソース名のペアでリソースを定義 し、ドキュメントに従って引数を設定する resource "aws_security_group_rule" "allow_https" { type = "ingress"
... } 要素 説明 resource 予約語(リソースブロックであることを示す) "aws_security_group_rule" リソースタイプ(AWSプロバイダが提供) "allow_https" リソース名(自分で命名する識別子) 6
やろうとしたこと SGルールの整理 既存のSGリソースは変更せずに、tfのリソース定義を修正したかった 1. リソース名の修正 — 命名規則に合わせて名前を変更 i. リソース名を変更する場合、再作成となる 2.
非推奨リソースの修正 i. 作成するAWSリソースは変わらず、Terraform上のリソース定義だけを変更す る必要があった ii. importブロック で既存リソースを取り込み — Stateに取り込めば安全に移行 できると考えた [1] importブロック:既存のAWSリソースをStateファイルに取り込む機能。Stateの追加のみを行い、AWSリソース自体には触ら ない。 7
実際のコード例(修正前 / 修正後) 修正前 resource "aws_security_group_rule" "allow_https_security_group_rule" { type =
"ingress" security_group_id = aws_security_group.main.id ... } 修正後 resource "aws_vpc_security_group_ingress_rule" "allow_https" { security_group_id = aws_security_group.main.id ... } 8
何が起きたか 移行先のリソースに構文エラーがあり、適用中にエラーが発生し、処理が途中で止ま った 1. リソース名の変更により、旧リソースの削除が先に実行された 2. 続く新リソースの作成で構文エラーが発生し、処理が中断 3. 旧リソースは削除済み、新リソースは未作成という状態に 結果:開発環境の限定アクセス用SGルールが消失
開発環境だったため実害は無かったが、利用中であれば通信断の可能性があった 成功しててもリソースの際作成が行われており、嬉しくない 9
なぜ起きたか importブロックに意識が向きすぎて、 リソース名の変更がAWSリソースの再作成を引き起こすことを見落としていた。 10
まとめ・教訓 今回の失敗から得た学び 11
まとめ・教訓 ① plan結果に destroy が出たら必ず立ち止まる 意図しないリソース削除がないか、必ずplan結果を一行ずつ確認する ② リソース名の変更には moved ブロックを使う
movedブロック = State内のリソース名を変更する → AWSリソースの削除・再作成が発生しない → 今回のユースケースにはこちらが適切だった moved { from = aws_security_group_rule.old_name to = aws_security_group_rule.new_name } 12
More Decks by KiyamaMizuki
See All by KiyamaMizuki
より綺麗なTerraformコードをチームで書き続ける取り組み
kiyamamizuki
0
58
AWS公式のIaC自動レビューツール使ってみた
kiyamamizuki
0
160
沖縄出身の新卒が振り返るシナマケでの社会人生活
kiyamamizuki
0
570
AWS使いすぎ注意! コスト管理とSlackアラート構築術
kiyamamizuki
0
910
Other Decks in Technology
See All in Technology
NutanixへのSSHアクセスの今後について
shadowhat
0
180
Taking back control of your AI development
inesmontani
PRO
0
320
コーディングできない僕にBobが舞い降りた
shumpei3
0
210
メール到達性を支える、リンク先URLのドメイン評価の扱い方
wind111
0
200
信頼性を犠牲にせず、PR マージからデプロイまで人の介入を完全にゼロにするまでの道のり
knwoop
4
2.2k
技術書典で本を売る流れ
yoshimura_datam
0
160
Kiro Enterprise Management: 管理者が 1 か所で決め、全員の Kiro に届ける
inariku
PRO
0
110
AWS DevOps Agentの構造から読み解く運用 AI エージェントの設計原則/aws-devops-agent-design-principles
tomoki10
0
290
ワークフローからハーネスへ — Web フレームワークの作者たちが AI エージェントに向かう理由
azukiazusa1
4
2.2k
フロントエンドのテストカバレッジは 100%強制がいい
bengo4com
7
2.8k
ノードが消える前提で設計する!Karpenterで金融システムを止めないためのノウハウ大全
daitak
1
250
Amazon Bedrock AgentCore の基礎とセキュリティ・ガバナンス
ren8k
0
280
Featured
See All Featured
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
620
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.5k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
610
My Coaching Mixtape
mlcsv
0
400
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
650
How to make the Groovebox
asonas
2
2.6k
The Spectacular Lies of Maps
axbom
PRO
1
1.2k
Git: the NoSQL Database
bkeepers
PRO
433
67k
Leo the Paperboy
mayatellez
11
2.5k
How STYLIGHT went responsive
nonsquared
100
6.4k
Building AI with AI
inesmontani
PRO
1
1.4k