AWS Directory Service の一つ、 AWS Managed Microsoft AD(以下 AWS MMAD) 環境を構築できる CloudFormation を書いてみました。
Cfnテンプレートについて
★注意点★
- Edition は Standard となっています。
- パラメータで指定した VPC に対して、新規構築した DHCPOptionSet を関連付けます。DHCPOptionSet を変更したくない VPC は絶対に指定しないでください。
# Yml file to build the following AWS resources
# |Resource|Number|
# |---|---|
# |AWS MMAD|1|
# |DHCPOptionSet|1|
Parameters:
domainName:
Description: Input a domain name that you want AD to manage.
Type: String
Default: example.com
adminPassword:
Description: Input a password for Admin user. The password must comply with "Password Policy". Please read https://docs.aws.amazon.com/ja_jp/directoryservice/latest/admin-guide/ms_ad_getting_started_create_directory.html.
Type: String
Default: P@ssw0rd
vpcId:
Description: Select your VPC.
Type: AWS::EC2::VPC::Id
subnetIds:
Description: Select your subnet. AWS Managed Microsoft AD need at least 2 subnet ID.
Type: List<AWS::EC2::Subnet::Id>
dhcpName:
Description: Input a DHCP name you want. This parameter will be DHCPOptionSet Name Tag.
Type: String
Default: AD-test
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: NetWork Parameter
Parameters:
- vpcId
- subnetIds
-
Label:
default: AD Parameter
Parameters:
- domainName
- adminPassword
-
Label:
default: DHCPOptionSet Parameter
Parameters:
- dhcpName
Resources:
MyMicrosoftAd:
Type: AWS::DirectoryService::MicrosoftAD
Properties:
CreateAlias: False
Edition: Standard
EnableSso: False
Name: !Ref domainName
Password: !Ref adminPassword
# ShortName: String
VpcSettings:
SubnetIds: !Ref subnetIds
VpcId: !Ref vpcId
MyDHCPOptionSet:
Type: AWS::EC2::DHCPOptions
Properties:
DomainName: !Ref domainName
DomainNameServers: !GetAtt MyMicrosoftAd.DnsIpAddresses
Tags:
- Key: Name
Value: !Ref dhcpName
MyDHCPOptionAssociation:
Type: AWS::EC2::VPCDHCPOptionsAssociation
Properties:
DhcpOptionsId: !Ref MyDHCPOptionSet
VpcId: !Ref vpcId
Outputs:
MyMicrosoftAdId:
Description: Microsoft AD ID
Value: !Ref MyMicrosoftAd
MyMicrosoftAdDnsIpAddress:
Description: IP address of Microsoft AD DNS server
Value: !Join
- ','
- !GetAtt MyMicrosoftAd.DnsIpAddresses
MyDHCPOptionSetId:
Description: DHCPOptionSet ID
Value: !Ref MyDHCPOptionSet
作った背景
AWS MMAD は従量課金にもかかわらず停止ができないので、割とコストがかかります。
e.g. Standard Edition かつ、2つのドメインコントローラを持つ AWS MMAD を 1ヵ月間使った場合
1時間あたり 0.146USD × 24h × 30 days = 105.12 USD
参考: 料金 - AWS Directory Service | AWS
ゆえに、しばらく触らないときは消す必要がありましたが、何回も構築→削除を繰り返していたので、もういっそ CloudFormation に流すだけの方が楽なんじゃないかと思い作ってみました。
テンプレートの処理
このテンプレートを流すと、大きく3つの動作が行われます。
- AWS MMAD を構築する (30分近く時間がかかります)
- 構築した AWS MMAD の DNS サーバを関連付けた DHCPOptionSet を構築する。
- VPC と DHCPOptionSet を関連付けます。
最後に
当たり前ですが、スタックを削除したら AD 内のデータは消えてしまいます。
そこは、毎回環境を復元できる Powershell スクリプトを作るしかないと思います。。
スナップショットが AD 削除後も残ってくれれば一番いいのですが。。。
ほかにいい方法があればコメントで教えてください。
参考
- AWS::DirectoryService::MicrosoftAD - AWS CloudFormation
- AWS::EC2::DHCPOptions - AWS CloudFormation
- AWS::EC2::VPCDHCPOptionsAssociation - AWS CloudFormation
菅谷 歩 (記事一覧)