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
CloudFormationで登場したForEachをちゃんと理解してみる
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Yuki_Kurono
August 25, 2023
Technology
890
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
CloudFormationで登場したForEachをちゃんと理解してみる
フレッシュメンLT #0 夏祭り
https://connpass.com/event/285559/
Yuki_Kurono
August 25, 2023
More Decks by Yuki_Kurono
See All by Yuki_Kurono
Terraform(IaC)と実環境の乖離どうしていますか?
yuki_kurono
0
42
今年のre:inventから学ぶこと
yuki_kurono
0
110
re_invent 2023事前勉強会
yuki_kurono
0
100
CloudFormationの運用が 辛くならない方法を本気で考えてみた
yuki_kurono
0
340
Turnstileのウィジェット モードとは何者か
yuki_kurono
0
720
CodeGuru Security ってなんだ?
yuki_kurono
0
1.2k
えるしってるか CloudFrontはWAFがあっても カスタムエラーレスポンスを返せる
yuki_kurono
0
450
Cloudflare初心者がIaCから基本構成を学んでみた
yuki_kurono
1
640
Terraformのnull_resource ってなに?aws cli が実行できるらしい
yuki_kurono
1
1.6k
Other Decks in Technology
See All in Technology
トヨタ⽣産⽅式(TPS)⼊⾨
recruitengineers
PRO
3
940
Reference-Free Image Quality Assessment for Virtual Try-On via Human Feedback
zozotech
PRO
0
510
まちスペース®とデジタルツインと「まちづくり」
hiro_ogi
0
130
【CEDEC2026】Creative Approaches to Localizing the Dialects and Unique Speech of Umamusume: Pretty Derby Characters in English
cygames
PRO
3
25k
Bits AI を制するものは Datadog を制す / The player that controls Bits AI, controls Datadog
kaminashi
0
110
システム思考で問題に対処する
yussak
0
320
【CEDEC2026】次世代デジタルカードゲームのサーバー設計と運用 〜『Shadowverse: Worlds Beyond』の舞台裏~
cygames
PRO
1
1.4k
What's new in Go 1.27?
ciarana
0
150
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1.2k
JavaScript 研修 (2026)
recruitengineers
PRO
2
600
攻撃と防御で学ぶAI時代のプロダクトセキュリティ演習
recruitengineers
PRO
9
3k
Sets in Go
ramalho
1
940
Featured
See All Featured
We Are The Robots
honzajavorek
0
300
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Rails Girls Zürich Keynote
gr2m
96
14k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
280
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
Mobile First: as difficult as doing things right
swwweet
225
10k
Discover your Explorer Soul
emna__ayadi
2
1.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
650
Music & Morning Musume
bryan
47
7.3k
How to Think Like a Performance Engineer
csswizardry
28
2.7k
Transcript
CloudFormationで登場した ForEachをちゃんと理解してみる Yuki Kurono 2023/8/25 #フレッシュメンLT
自己紹介 #フレッシュメンLT 黒野 雄稀 Yuki Kurono アイレット株式会社 新卒4年目 普段はインフラ設計・構築や運用構築に従事 2022/2023
Japan AWS All Certifications Engineers 2023 Japan AWS Top Engineers AWS Community Builders(Cloud Operations)
CloudFormationとは AWSで提供されるInfrastructure as Code(IaC)サービス IaC = コードしてインフラを管理しようぜ! テンプレートと呼ばれるファイルにコードを記述する 記述方法はJson or
YAMLを使うことができる AWSTemplateFormatVersion: '2010-09-09' Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 #フレッシュメンLT
CloudFormationならではの良さもある。 • 環境構築が要らない • YAML,Jsonでの統一された書き方 • AWSサービスとの組み合わせの相性も良い • 対応しているAWSリソースの多さ •
etc… CloudFormation使ってる? #フレッシュメンLT
CloudFormationならではの良さもある。 • 環境構築が要らない • YAML,Jsonでの統一された書き方 • AWSサービスとの組み合わせの相性も良い • 対応しているAWSリソースの多さ •
etc… CloudFormation使ってる? #フレッシュメンLT NO
これまでのCloudFormationでの構築、運用は辛いと思うことが多かった。 • 冗長的なコードの記述 ←これが結構大きかった。 • etc… CloudFormationなんで使わないの? #フレッシュメンLT
様々なプログラミング言語でも使える様なループ処理が行える機能です。 これを使うことで冗長なコードの定義が不要となりました。 ForEachってなんだ? #フレッシュメンLT Parameters: VPCList: Type: List<String> Default: VPC01,VPC02,VPC03
Resources: Fn::ForEach::VPCListLoop: - VPCNames - !Ref VPCList - ${VPCNames}: Type: AWS::EC2::VPC Properties: CidrBlock: "192.168.0.0/16" Tags: - Key: Name Value: !Ref VPCNames Resources: VPC01: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 VPC02: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 VPC03: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 ==
ForEachは単体で使うと1つの値しかループさせることができない。 例えばサブネット名とか。 →でもサブネットを作るにはCIDR、Azの名前など色々な値を可変させたいです よね? ForEachイケてると思う使い方 #フレッシュメンLT Fn::ForEach::SubnetListLoop: - SubnetNames -
!Ref SubnetList - ${SubnetNames}: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: : "192.168.0.0/16" AvailabilityZone: "ap-northeast-1a" Tags: - Key: Name Value: !Ref SubnetNames
AWSTemplateFormatVersion: 2010-09-09 Transform: "AWS::LanguageExtensions" Parameters: SubnetList: Type: List<String> Default: SubnetPuba,SubnetWeba,SubnetDBa
Mappings: SubnetMappings: SubnetPuba: cidrBlock: "192.168.1.0/24" az: ap-northeast-1a nameTags: subnet-pub-a SubnetWeba: cidrBlock: "192.168.2.0/24" az: ap-northeast-1a nameTags: subnet-web-a SubnetDBa: cidrBlock: "192.168.3.0/24" az: ap-northeast-1a nameTags: subnet-db-a Resources: Fn::ForEach::SubnetListLoop: - SubnetNames - !Ref SubnetList - ${SubnetNames}: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: !FindInMap - SubnetMappings - !Ref SubnetNames - cidrBlock AvailabilityZone: !FindInMap - SubnetMappings - !Ref SubnetNames - az Tags: - Key: Name Value: !FindInMap - SubnetMappings - !Ref SubnetNames - nameTags Mappings Foreach 複数の値をMappingで渡して 参照する
まとめ • これまで冗長なコードを記載する必要があったものが解決されかなり 便利になるアップデートだと思います! • ForEachと併せてMappingを使うと複数の値(パラメータ値)をループさ せることができるので、これを使うことでよりコードを短縮して書く ことができるのでオススメ • ForEach内で使用できる組み込み関数やクオータなどの制約事項につい
ても注意が必要となりますので、より詳しく知りたい方は公式ページ を参考にするとよいかと思います。 ◦ https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-foreach.html #フレッシュメンLT