## =================== VERSION ===================
AWSTemplateFormatVersion: “2010-09-09”
## =================== DESCRIPTION ===================
Description: >–
AWS CloudFormation sample template
Create an IAM User and optionally attach it to IAM group(s)
AWS doc: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-iam-user.html
## =================== PARAMETERS ===================
Parameters:
1Stage:
Description: DEV, STG, PRD
Type: String
Default: “DEV”
AllowedPattern: “[a-zA-Z0-9]*”
ConstraintDescription: “DEV, STG, PRD”
2VpcCIDR:
Description: Please enter the IP range (CIDR notation) for this VPC
Type: String
Default: 10.10.0.0/16
3PublicSubnet1CIDR:
Description: Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone
Type: String
Default: 10.10.1.0/24
3PublicSubnet3CIDR:
Description: Please enter the IP range (CIDR notation) for the public subnet in the second Availability Zone
Type: String
Default: 10.10.3.0/24
4PrivateSubnet1CIDR:
Description: Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone
Type: String
Default: 10.10.11.0/24
4PrivateSubnet3CIDR:
Description: Please enter the IP range (CIDR notation) for the private subnet in the second Availability Zone
Type: String
Default: 10.10.13.0/24
5DBSubnet1CIDR:
Description: Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone
Type: String
Default: 10.10.21.0/24
5DBSubnet3CIDR:
Description: Please enter the IP range (CIDR notation) for the private subnet in the second Availability Zone
Type: String
Default: 10.10.23.0/24
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instances. Linked to AWS Parameter
Type: AWS::EC2::KeyPair::KeyName
Default: DEV-KeyPair # PRD
ConstraintDescription: must be the name of an existing EC2 KeyPair.
## =================== RESOURCES ===================
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref 2VpcCIDR
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
– Key: Name
Value: !Sub “${1Stage}-VPC”
– Key: 1Stage
Value: !Ref 1Stage
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
– Key: Name
Value: !Sub “${1Stage}-IGW”
– Key: 1Stage
Value: !Ref 1Stage
InternetGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
PublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: ap-northeast-2a
CidrBlock: !Ref 3PublicSubnet1CIDR
MapPublicIpOnLaunch: true
Tags:
– Key: Name
Value: !Sub “${1Stage}-Public-Subnet-AZ1”
– Key: 1Stage
Value: !Ref 1Stage
PublicSubnet3:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: ap-northeast-2c
CidrBlock: !Ref 3PublicSubnet3CIDR
MapPublicIpOnLaunch: true
Tags:
– Key: Name
Value: !Sub “${1Stage}-Public-Subnet-AZ3”
– Key: 1Stage
Value: !Ref 1Stage
PrivateSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: ap-northeast-2a
CidrBlock: !Ref 4PrivateSubnet1CIDR
MapPublicIpOnLaunch: false
Tags:
– Key: Name
Value: !Sub “${1Stage}-Private-Subnet-AZ1”
– Key: 1Stage
Value: !Ref 1Stage
PrivateSubnet3:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: ap-northeast-2c
CidrBlock: !Ref 4PrivateSubnet3CIDR
MapPublicIpOnLaunch: false
Tags:
– Key: Name
Value: !Sub “${1Stage}-Private-Subnet-AZ3”
– Key: 1Stage
Value: !Ref 1Stage
DBSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: ap-northeast-2a
CidrBlock: !Ref 5DBSubnet1CIDR
MapPublicIpOnLaunch: true
Tags:
– Key: Name
Value: !Sub “${1Stage}-DB-Subnet-AZ1”
– Key: 1Stage
Value: !Ref 1Stage
DBSubnet3:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: ap-northeast-2c
CidrBlock: !Ref 5DBSubnet3CIDR
MapPublicIpOnLaunch: true
Tags:
– Key: Name
Value: !Sub “${1Stage}-DB-Subnet-AZ3”
– Key: 1Stage
Value: !Ref 1Stage
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
– Key: Name
Value: !Sub “${1Stage}-Public-Routes”
– Key: 1Stage
Value: !Ref 1Stage
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
– Key: Name
Value: !Sub “${1Stage}-Private-Routes”
– Key: 1Stage
Value: !Ref 1Stage
DBRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
– Key: Name
Value: !Sub “${1Stage}-DB-Routes”
– Key: 1Stage
Value: !Ref 1Stage
DefaultPublicRoute:
Type: AWS::EC2::Route
DependsOn: InternetGatewayAttachment
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
DefaultDBRoute:
Type: AWS::EC2::Route
DependsOn: InternetGatewayAttachment
Properties:
RouteTableId: !Ref DBRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet1
PublicSubnet3RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet3
PrivateSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateRouteTable
SubnetId: !Ref PrivateSubnet1
PrivateSubnet3RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateRouteTable
SubnetId: !Ref PrivateSubnet3
DBSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref DBRouteTable
SubnetId: !Ref DBSubnet1
DBSubnet3RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref DBRouteTable
SubnetId: !Ref DBSubnet3
PublicSecurityGroup:
Type: ‘AWS::EC2::SecurityGroup’
Properties:
GroupName: !Sub “${1Stage}-Public-Security-Group”
GroupDescription: Enable SSH
VpcId: !Ref VPC
SecurityGroupIngress:
– IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Description: SSH Service
– IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
Description: HTTP Service
– IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
Description: HTTPS Service
– IpProtocol: tcp
FromPort: 943
ToPort: 943
CidrIp: 0.0.0.0/0
Description: OpenVPN-Admin Web UI
– IpProtocol: udp
FromPort: 1194
ToPort: 1194
CidrIp: 0.0.0.0/0
Description: OpenVPN-Client Connect
Tags:
– Key: Name
Value: !Sub “${1Stage}-Public-Security-Group”
– Key: 1Stage
Value: !Ref 1Stage
PrivateSecurityGroup:
Type: ‘AWS::EC2::SecurityGroup’
Properties:
GroupName: !Sub “${1Stage}-Private-Security-Group”
GroupDescription: Enable SSH
VpcId: !Ref VPC
SecurityGroupIngress:
– IpProtocol: tcp
FromPort: ’22’
ToPort: ’22’
CidrIp: 0.0.0.0/0
Description: SSH Service
Tags:
– Key: Name
Value: !Sub “${1Stage}-Private-Security-Group”
– Key: 1Stage
Value: !Ref 1Stage
DBSecurityGroup:
Type: ‘AWS::EC2::SecurityGroup’
Properties:
GroupName: !Sub “${1Stage}-DB-Security-Group”
GroupDescription: Enable Mysql
VpcId: !Ref VPC
SecurityGroupIngress:
– IpProtocol: tcp
FromPort: ‘3306’
ToPort: ‘3306’
CidrIp: 0.0.0.0/0
Tags:
– Key: Name
Value: !Sub “${1Stage}-DB-Security-Group”
– Key: 1Stage
Value: !Ref 1Stage
## =================== Outputs ===================
Outputs:
VPC:
Description: A reference to the created VPC
Value: !Ref VPC
PublicSubnets:
Description: A list of the public subnets
Value: !Join [ “,”, [ !Ref PublicSubnet1, !Ref PublicSubnet3 ]]
PrivateSubnets:
Description: A list of the private subnets
Value: !Join [ “,”, [ !Ref PrivateSubnet1, !Ref PrivateSubnet3 ]]
DBSubnets:
Description: A list of the DB subnets
Value: !Join [ “,”, [ !Ref DBSubnet1, !Ref DBSubnet3 ]]
PublicSubnet1:
Description: A reference to the public subnet in the 1st Availability Zone
Value: !Ref PublicSubnet1
PublicSubnet3:
Description: A reference to the public subnet in the 3st Availability Zone
Value: !Ref PublicSubnet3
PrivateSubnet1:
Description: A reference to the public subnet in the 1st Availability Zone
Value: !Ref PrivateSubnet1
PrivateSubnet3:
Description: A reference to the public subnet in the 3st Availability Zone
Value: !Ref PrivateSubnet3
PublicSecurityGroup:
Description: Security group with no ingress rule
Value: !Ref PublicSecurityGroup
PrivateSecurityGroup:
Description: Security group with no ingress rule
Value: !Ref PrivateSecurityGroup