目次
CloudFormationとは
AWSには、CloudFormationというサービスがあります。
CloudFormationは、AWSリソースのインフラストラクチャをコードで管理するサービスです。JSONまたはYAML形式のテンプレートを使用して、リソースの定義、デプロイ、更新を行います。これにより、効率的かつ一貫性のあるAWS環境の構築と管理が可能です。
弊社では、これを活用して、VPC環境を整理、統一化しています。
また、すまいる顔では、
ITシステムを使って、合理的な経営を実現することをコンセプトに、
専任SEによる無料相談を実施しています。
AWS環境の構築に興味をお持ちの方は、下記よりお気軽にご相談ください。
社内での標準化
弊社内では、
dir
├make.yml
├params
└param.json
という構造のフォルダを作り、 dir 内で以下のコマンドを実行することで、環境を作成しております。
[作成]aws cloudformation deploy \
--template-file make.yml \
--stack-name test \
--parameter-overrides "file://params/param.json"
[削除]
aws cloudformation delete-stack --stack-name test
今回は、弊社での、VPS、EC2、RDS環境を作るコードを紹介させていただきます。
環境構築例
作る環境は、以下のようなVPS環境です。
make.yml
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
Project:
Type: String
VPCCIDR:
Type: String
PublicSubnetACIDR :
Type: String
PublicSubnetCCIDR :
Type: String
PrivateSubnetACIDR :
Type: String
PrivateSubnetCCIDR :
Type: String
ec2ImageId :
Type: String
ec2InstanceType :
Type: String
mysqlpassword :
Type: String
mysqlversion :
Type: String
Resources:
##----------------------------------##
# VPC
##----------------------------------##
VPC:
Type: "AWS::EC2::VPC"
Properties:
CidrBlock: !Ref VPCCIDR
EnableDnsSupport: "true"
EnableDnsHostnames: "true"
InstanceTenancy: default
Tags:
- Key: Name
Value: !Sub "${Project}-vpc"
##----------------------------------##
# Internet Gateway
##----------------------------------##
InternetGateway:
Type: "AWS::EC2::InternetGateway"
Properties:
Tags:
- Key: Name
Value: !Sub "${Project}-igw"
InternetGatewayAttachment:
Type: "AWS::EC2::VPCGatewayAttachment"
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
# ------------------------------------------------------------#
# Subnet
# ------------------------------------------------------------#
# Public SubnetA Create
PublicSubnetA:
Type: "AWS::EC2::Subnet"
Properties:
AvailabilityZone: "ap-northeast-1a"
CidrBlock: !Ref PublicSubnetACIDR
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub "${Project}-public-subnet-a"
# Public SubnetC Create
PublicSubnetC:
Type: "AWS::EC2::Subnet"
Properties:
AvailabilityZone: "ap-northeast-1c"
CidrBlock: !Ref PublicSubnetCCIDR
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub "${Project}-public-subnet-c"
# Private SubnetA Create
PrivateSubnetA:
Type: "AWS::EC2::Subnet"
Properties:
AvailabilityZone: "ap-northeast-1a"
CidrBlock: !Ref PrivateSubnetACIDR
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub "${Project}-private-subnet-a"
# Private SubnetC Create
PrivateSubnetC:
Type: "AWS::EC2::Subnet"
Properties:
AvailabilityZone: "ap-northeast-1c"
CidrBlock: !Ref PrivateSubnetCCIDR
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub "${Project}-private-subnet-c"
# ------------------------------------------------------------#
# RouteTable
# ------------------------------------------------------------#
# Public RouteTableA Create
RouteTableA:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub "${Project}-rt-a"
RouteTableC:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub "${Project}-rt-c"
SubnetRouteTableAssociationA:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
RouteTableId: !Ref RouteTableA
SubnetId: !Ref PublicSubnetA
SubnetRouteTableAssociationC:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
RouteTableId: !Ref RouteTableC
SubnetId: !Ref PublicSubnetC
RouteA:
Type: "AWS::EC2::Route"
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
RouteTableId: !Ref RouteTableA
RouteC:
Type: "AWS::EC2::Route"
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
RouteTableId: !Ref RouteTableC
##----------------------------------##
# ec2
##----------------------------------##
KeyPair:
Type: 'AWS::EC2::KeyPair'
Properties:
KeyName: !Sub "${Project}-ec2-key"
EC2Instance:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: !Ref ec2ImageId
InstanceType: !Ref ec2InstanceType
BlockDeviceMappings:
- DeviceName: '/dev/xvda'
Ebs:
VolumeType: 'gp2'
VolumeSize: 8
Tags:
- Key: Name
Value: !Sub "${Project}-ec2"
KeyName: !Ref KeyPair
NetworkInterfaces:
- AssociatePublicIpAddress: "true"
DeviceIndex: "0"
SubnetId: !Ref PublicSubnetA
GroupSet:
- !Ref ec2SecurityGroup
UserData:
Fn::Base64: |-
#!/bin/bash
yum update -y
dnf update -y
dnf install -y httpd wget php-fpm php-mysqli php-json php php-devel
dnf -y localinstall https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm
dnf -y install mysql mysql-community-client
systemctl start httpd
systemctl enable httpd
touch /var/www/html/index.html
echo "Hello World from user data" > /var/www/html/index.html
ec2SecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: !Sub "${Project}-ec2-sg"
VpcId: !Ref VPC
Tags:
- Key: 'Name'
Value: !Sub "${Project}-ec2-sg"
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
##----------------------------------##
# rds
##----------------------------------##
rdsSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: !Sub "#{Project}-rds-sg"
SecurityGroupEgress:
- IpProtocol: -1
FromPort: -1
ToPort: -1
CidrIp: 0.0.0.0/0
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 3306
ToPort: 3306
SourceSecurityGroupId: !Ref ec2SecurityGroup
VpcId: !Ref VPC
rdsSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: !Sub "${Project}-rds-subnetgroup"
SubnetIds:
- !Ref PrivateSubnetA
- !Ref PrivateSubnetC
DBSubnetGroupName: !Sub "${Project}-rds-subnetgroup"
Tags:
- Key: Name
Value: !Sub "${Project}-rds-subnetgroup"
RDS:
Type: AWS::RDS::DBInstance
Properties:
AllocatedStorage: 20
DBInstanceClass: "db.t2.small"
DBSubnetGroupName: !Ref rdsSubnetGroup
Engine: mysql
EngineVersion: !Ref mysqlversion
MasterUsername: "root"
MasterUserPassword: !Ref mysqlpassword
StorageType: gp2
Tags:
- Key: Name
Value: !Sub "${Project}-rds"
VPCSecurityGroups:
- !Ref rdsSecurityGroup
param.json
[
{
"ParameterKey": "Project",
"ParameterValue": "smilekao-sample"
},
{
"ParameterKey": "VPCCIDR",
"ParameterValue": "10.0.0.0/16"
},
{
"ParameterKey": "PublicSubnetACIDR",
"ParameterValue": "10.0.0.0/24"
},
{
"ParameterKey": "PublicSubnetCCIDR",
"ParameterValue": "10.0.1.0/24"
},
{
"ParameterKey": "PrivateSubnetACIDR",
"ParameterValue": "10.0.2.0/24"
},
{
"ParameterKey": "PrivateSubnetCCIDR",
"ParameterValue": "10.0.3.0/24"
},
{
"ParameterKey": "ec2ImageId",
"ParameterValue": "ami-012261b9035f8f938"
},
{
"ParameterKey": "ec2InstanceType",
"ParameterValue": "t2.micro"
},
{
"ParameterKey": "mysqlpassword",
"ParameterValue": "12345678"
},
{
"ParameterKey": "mysqlversion",
"ParameterValue": "8.0.33"
}
]
上記のファイルを作成し、コマンドを実行すると、画像のVPS環境が作成されます!
テストで作成した後は、deleteをお忘れなく!
お問合せ
AWS運用、環境構築に関するご相談、お問合せ、お待ちいたしております!