はじめに
WindowsServer2019 on EC2 + SG を作成する CloudFormation テンプレートを書きました。
最新の EBS タイプ gp3 に対応しています!
検証などの際に是非お使いください
記事目安...5分
cfn テンプレート
# Yml file that builds the following AWS resources
# |Resource|Number|
# |---|---|
# |EC2|1|
# |Security Group|1|
Parameters:
# EC2
AmiId:
Description: Input an ami-id. Default is the latest WindowsServer 2019 AMI.
Type: AWS::SSM::Parameter::Value<String>
Default: /aws/service/ami-windows-latest/Windows_Server-2019-English-Full-Base
InstanceType:
Description: Input an Instance type.
Type: String
Default: t3.small
KeyPair:
Description: Select your key pair.
Type: "AWS::EC2::KeyPair::KeyName"
InstanceName:
Description: Input a instance name.
Type: String
Default: test
SubnetId:
Description: Select your SubnetId.
Type: AWS::EC2::Subnet::Id
AttachPublicIpAddress:
Description: Set to true if you want to assign a public IP address, set to false if you don't.
Type: String
Default: false
AllowedValues:
- "true"
- "false"
# SG
VpcId:
Description: Select your VpcId.
Type: AWS::EC2::VPC::Id
RdpInboundIp:
Description: Input an IP address you want to access with RDP. e.g. 0.0.0.0/0
Type: String
Default: "0.0.0.0/0"
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: EC2
Parameters:
- AmiId
- SubnetId
- AttachPublicIpAddress
- InstanceType
- KeyPair
- InstanceName
-
Label:
default: SG
Parameters:
- VpcId
- RdpInboundIp
Resources:
# EC2
MyInstance:
Type: AWS::EC2::Instance
Properties:
EbsOptimized: true
DisableApiTermination: false
InstanceInitiatedShutdownBehavior: stop
Monitoring: false
Tenancy: default
ImageId: !Ref AmiId
InstanceType: !Ref InstanceType
KeyName: !Ref KeyPair
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
VolumeSize: 30
VolumeType: gp3
Iops: 3000
DeleteOnTermination: true
NetworkInterfaces:
- AssociatePublicIpAddress: !Ref AttachPublicIpAddress
DeleteOnTermination: true
GroupSet:
- !Ref MySG
DeviceIndex: 0
SubnetId: !Ref SubnetId
Tags:
- Key: Name
Value: !Sub ${InstanceName}
# UserData: String
# SG
MySG:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VpcId
GroupDescription: !Sub Security Group attached to ${InstanceName}
GroupName: !Sub sg_${InstanceName}
SecurityGroupIngress:
- CidrIp: !Ref RdpInboundIp
Description: RDP port
FromPort: 3389
IpProtocol: tcp
# SourceSecurityGroupId: String
# SourceSecurityGroupName: String
ToPort: 3389
Outputs:
OutputsInstanceId:
Description: EC2 Instance ID
Value: !Ref MyInstance
OutputsSgId:
Description: SG ID
Value: !Ref MySG
cfnテンプレートの詳細
テンプレートで構築されるリソースの詳細は以下です。
〇 EC2 × 1
| Key | Value |
|---|---|
| OS | WindowsServer 2019 |
| EBS type | gp3 |
〇 SG × 1
| Key | Value |
|---|---|
| Inbound | RDP(3389) |
| Outbound | ALL |
構築物の ID は、アウトプットセクションに記載されるので、合わせてご確認ください
注意事項
AMI は SSM 公開パラメータストアから最新のものを取得します。
このテンプレートで構築した SG は EC2 に自動的にアタッチされます。
菅谷 歩 (記事一覧)