Deploying Windows FSx joined to an AD using AWS CDK
Nidhi Sharma
Feb 25, 2023
In a Windows Server setup, it is common practice to use Active Directory for server and user management. For this purpose, you can create an AWS managed Active Directory. Any EC2 instances or other resources that you would like to manage should join the AD upon their creation. At a client, we had a setup that required a common file storage for sharing resources across our Windows EC2 instances. For this purpose, we used AWS FSx for Windows.
Seems straightforward?
It isn’t due to how the provisioning of the AD’s security group is handled internally by the AWS API. Let’s see why.
Roadblock coming ahead
To join the AD, certain ports from the FSx’s security group should be whitelisted in the Inbound rules of the AD’s security group. Marked below in red in the diagram. This basically means, before creating your FSx, you must have:
An Active Directory’s ID
The AD’s security group ID
The FSx’s security group ID
When you create an AD, a security group is automatically assigned to the AD and you cannot assign a different security group to it. In addition to that, you cannot figure out the security group ID after the AD is created as it is not returned by the CDK API via any property or method.
Figuring a way out
In scenarios where the CDK API isn’t sufficient, AWS CDK Custom Resource comes to our rescue. This CDK Construct allows us to query the AWS API via a singleton Lambda function to make calls to gather information or perform actions that cannot be done via the CDK API. You can specify exactly which API calls are invoked for the ‘CREATE’, ‘UPDATE’ and ‘DELETE’ life cycle events.By default, the Active Directory’s security group has a fixed naming pattern: <active-directory-id>_controllers
. So, we will use a CDK Custom Resource to make an AWS API call and fetch the AD’s security group ID based on its name. We will then add an Inbound Rule in the AD’s security group to allow traffic from the FSx’s security group.Our approach to create this setup is:
Create a security group for the FSx
Create an AWS managed AD
Import the AD’s security group by ID (using an AWS CDK Custom Resource)
Add the ports from the FSx’s security group in the Inbound Rules of the AD’s security group
Finally, instantiate a new FSx server and assign it the security group created in step 1 so that it can join the AD
NOTE An easier way in this case would be to import the AD’s security group by using its name as a lookup, but the idea here is to illustrate the usage of AWS CDK Custom Resource to make AWS API calls. If you would like to import the AD’s security group by its name then use this method instead and skip Step 3. If using this approach, you will first have to search for the Active Directory ID from the AWS console and hard-code the value <active-directory-id>_controllers
for the lookup to work. You cannot import the value of the AD ID from CFN Outputs and then use it for lookup, because tokens are not resolved during the construction and synthesis stage of the app’s lifecycle. If you attempt doing it, the following error message will be returned: Error: All arguments to look up a security group must be concrete (no Tokens)
.
Cruising smoothly
The first thing you will notice when trying to deploy an AD or an FSx for Windows File Server is that there are no CDK L2 constructs for them!So, instead use the L1 constructs. As you might know, these are low-level Constructs that correspond directly to the CloudFormation resources. With them, you work with exactly the same structures as in raw CloudFormation. Not so clean but this is a viable option.
Note: Variables that contain IDs or configurable parameters are passed via the props Interface. Feel free to replace them with your environment variables or actual values as you deem fit in your setup.
Step 1: Create a security group for the FSx:
Step 2: Create an AWS managed AD:
Step 3: Import the AD’s security group by its ID (using an AWS CDK Custom Resource):
Debugging Tip: To figure out the response keys returned by any AWS API call, you can check the Cloudwatch logs of the Lambda function created by the Custom Resource. You can find the Lambda function in the Resources tab of CloudFormation.
Step 4: Add the ports from the FSx’s security group in the Inbound Rules of the AD’s security group
Step 5: Instantiate a new FSx server
Your app’s entrypoint
Putting it all together:
Your Windows FSx file server is now joined to your AWS managed Active Directory. To start using the FSx storage from the EC2 instances joined to the same Active Directory, whitelist the instances’ security group on port 445 in the Inbound Rules of the FSx’s security group and you are good to go!
References:
Amazon FSx: https://aws.amazon.com/fsx/
CDK Active Directory: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_directoryservice.CfnMicrosoftAD.html
CDK Windows FSx: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_fsx.CfnFileSystem.html#windowsconfiguration-1
CDK Custom Resource: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.custom_resources.AwsCustomResource.html
SDK Describe EC2 Security Group: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#describeSecurityGroups-propertyBack to blog