[A-00205] CloudFormation入門(AWS)
awsのIaCであるcloudformationの入門用記事です。
・EC2インスタンスを作成してみる。
アーキテクチャはこちら。かなりシンプルで使い道のないアーキテクチャですがとりあえず作ってみます。

AWSTemplateFormatVersion: '2010-09-09'
Description: Simple Template
## Parameters
Parameters:
InstanceType:
Description: EC2 Instance Types.
Type: String
Default: t2.micro
AllowedValues:
- t2.micro
- t2.small
- t2.medium
## Resources.
### vpc
Resources:
simpleVpc:
Type: AWS::EC2::VPC
Properties:
CidrBlock: '192.168.0.0/16'
Tags:
- Key: 'Name'
Value: 'simple-vpc'
### subnet
simpleSubnet:
Type: AWS::EC2::Subnet
Properties:
CidrBlock: '192.168.1.0/24'
MapPublicIpOnLaunch: false
Tags:
- Key: 'Name'
Value: 'simple-subnet'
VpcId: !Ref simpleVpc
### ec2
simpleEC2Instance:
Type: AWS::EC2::Instance
Properties:
SubnetId: !Ref simpleSubnet
ImageId: "ami-0595d6e81396a9efb"
InstanceType: !Ref InstanceType
BlockDeviceMappings:
- DeviceName: '/dev/xvda'
Ebs:
VolumeType: 'gp2'
VolumeSize: 8
Tags:
- Value: 'simple-ec2-instance'
Key: 'Name'
上記がテンプレートになります。
次のコマンドでAWS上にスタックを作成します。
aws cloudformation deploy --template-file template.yaml --stack-name example
$ aws cloudformation deploy --template-file template.yaml --stack-name example
Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - example
上記の通りサクセスで完了したら削除します。
aws cloudformation delete-stack --stack-name example
・Appendix
参考文献はこちら
https://qiita.com/hiyanger/items/245d96312248017701b0
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/create-key-pairs.html
https://qiita.com/kanadeee/items/de8c1780e3c37811eb57
https://stackoverflow.com/questions/70881426/aws-cloudformation-default-keypair-for-ec2
https://dev.classmethod.jp/articles/read-aws-cli-cfn-options/#toc-15
コメントを残す