When AWS Active Directory(Type: "AWS::DirectoryService::MicrosoftAD") is created via cloud formation, the AWS also creates the security group for domain controllers. The description for such security group is "AWS created a security group for d-123456adb directory controllers".
This security group allows source in ingress as 0.0.0.0/0 for all ports.
I have to manually edit/set it to my vpc CIDR after cloud formation run and also I am not able to get its ID inside cloud formation.
Is there any way to edit OR self-define the security group while creating Microsoft AD("AWS::DirectoryService::MicrosoftAD") via cloud formation?
This response is an edited reply by AWS support to my similar inquiry as the OP's question.
This feature is currently not available and AWS's CloudFormation developer team is aware of this issue and there is a feature request in place.
As a workaround, you can leverage a Lambda backed custom resource to get the security ID and pass it to the custom resource so that it can be accessed in the CF stack.
In this approach, you will create a Lambda function that can take the security group name and VPC-id as input and give the security group-id as output. The custom resource created is the piece of code which will signal the Lambda function with a group name and VPC-id. Lambda returns the security group-id to this custom resource, you can get the sg-id as shown below:
{ "Fn::GetAtt" : ["CustomResouce", "security_group_id"] }
A sample template and a sample Lambda function (for acquiring the security group-id) are included at the end of this message. In the template, the function has been used in the output of the code, but you can use the same in the client's security_group as shown below:
Custom Lambda function, customresouce.py (to be placed in a S3 bucket where Lambda can access it):
A sample template that utilizes the custom Lambda function:
Rather kludgy, but seems like it might work as an interim solution until AWS gets around to implementing the functionality into their APIs/CloudFormation/Hosted AD. NOTE: I have not had the opportunity to test the above yet, but I'm posting it here for the OP and anyone else who might be looking for a solution to this issue.
References:
Custom Resource Reference
AWS Lambda-backed Custom Resources
AWS::Lambda::Function