Open4
CloudFormationメモ
CloudFormation概要
- yamlかjsonで記述する宣言的なIaCのマネージドサービス
- スタック
- CloudFormationで作ったリソースをスタックという
- テンプレートの記述と紐づいた状態になる
- 手動での変更を検知するDrift Ditectionとうサービスもある
Cloud Formationテンプレート
- Resourcesセクション
- Logical ID、リソースタイプ、プロパティを指定する
Resources:
Logical ID:
Type: Resource type
Properties:
Set of properties
- リソースタイプ
- 127くらいあって順次追加されている
- AWS::EC2::Instanceなど
- 127くらいあって順次追加されている
CloudFormationでVPC作成
- Install the extension
- Create new file
- Set syntax to JSON or YAML
- Type start and press tab key to populate basic template skeleton
- Start typing desired resource name and hit tab key
- VSCodeのextention
- startと打ってTabをうつと骨格が出る
- エラーが出てしまうのでVSCodeのsetting.jsonに以下を追加する
"yaml.schemas": {
"https://d33vqc0rt9ld30.cloudfront.net/latest/gzip/CloudFormationResourceSpecification.json": [
"*.cf.yaml",
"*.cf.yml",
"*.cfn/*.yaml",
"*.cfn/*.yml",
"cloud*formation/*.yaml",
"cloud*formation/*.yml"
]
},
"yaml.customTags": [
"!Ref",
"!Sub scalar",
"!Sub sequence",
"!Join sequence",
"!FindInMap sequence",
"!GetAtt scalar",
"!GetAtt sequence",
"!Base64 mapping",
"!GetAZs",
"!Select scalar",
"!Select sequence",
"!Split sequence",
"!ImportValue",
"!Condition",
"!Equals sequence",
"!And",
"!If",
"!Not",
"!Or"
],
以下のような最低限のVPCを作成してみる
AWSTemplateFormatVersion: 2010-09-09
# Description: ---
# Metadata:
# Parameters:
# Mappings:
# Conditions:
Resources:
MyVPC2:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.8.0/21
EnableDnsSupport: true
Tags:
- Key: Name
Value: MyVPC2fromCF
# Outputs:
CloudFormationの更新
- テンプレートを更新するとスタックが変わる
- Security Groupを作成する
以下のように追加
subnetName:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: "ap-northeast-1a"
VpcId: !Ref MyVPC2
CidrBlock: 10.0.8.0/24
Tags:
- Key: Name
Value: subnet1fromCF
secGroupName:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: GroupName-SG
GroupDescription: GroupDescription-SG
VpcId: !Ref MyVPC2
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: SGfromCF
- 更新の際は、CloudFormationの対象スタックを選択して、更新
- 既存テンプレートを置き換える
- テンプレートファイルのアップロード
- 既存テンプレートを置き換える