The purpose of this article is to provide you with step-by-step guidance on how to use Terraform templates, to onboard your AWS/GCP environment to Microsoft Defender for Cloud. Terraform is an Infrastructure as Code (IaC) tool you can use to build, change, and version your public cloud infrastructure safely and efficiently. In addition to being a widely used tool, an advantage of using Terraform to onboard your environment to Defender for Cloud, is that you can use it for both AWS and GCP. Using Terraform to onboard your AWS/GCP environment to Defender for Cloud, allows you to automate the onboarding process and integrate it into your existing processes.
If you need to install Terraform, you can start at https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli. Guidance on how to get started with Terraform in Defender for Cloud, you can start at Deploy Microsoft Defender for Cloud via Terraform - Microsoft Community Hub.
Have you ever asked yourself how to onboard your AWS or GCP environment to Defender for Cloud with Terraform? This article provides you with guidance on how to use Terraform templates that have already been created to onboard your AWS/GCP environment to Microsoft Defender for Cloud. By the time you finish reading this article you’ll learn how to accomplish the following scenarios:
While these two scenarios mention AWS, to onboard GCP you can follow the same process. This article provides step-by-step guidance for both scenarios, starting with the first scenario.
1. Scenario: Onboarding several AWS accounts not grouped under an AWS management account to Microsoft Defender for Cloud, using Terraform.
By default, when you onboard your AWS/GCP environment at the management account-/organizational-level through the Azure portal, the security connectors that are created for each account/project are stored in the same resource group. Some organizations have requirements for security connectors to reside in different resource groups. You can use the Terraform templates provided to achieve this purpose.
1.1 Prepare the cloud you want to onboard:
Clone (or download) the Terraform templates available at https://aka.ms/MDCTerraformTemplates. After you clone the Terraform templates there are three steps you need to do.
terraform init \
terraform plan –var-file <variable-file-name>.tfvars \
terraform apply –var-file <variable-file-name>.tfvars
After these commands run, Terraform does the rest. You need to repeat this process for every account or project you want to onboard. This ensures that credentials are provided by you for each account or project that you want to onboard. You can use the same credentials for all AWS accounts that you wish to onboard. This provides Terraform with the necessary permissions to the resources in question.
The Terraform templates you cloned, can integrate with existing CI/CD processes. If you need to onboard multiple accounts or projects you can also use CI/CD pipelines, to which you can add these Terraform templates to onboard your accounts or projects. After you complete these three steps, your destination cloud is ready to be onboarded and you can proceed with the onboarding process on the Azure side.
1.2 Prepare the Azure cloud:
Now let’s go into the process of telling Azure that you’re ready to onboard these accounts/projects. Clone (or download) the Terraform templates located in the folder ‘Azure’, that are available at https://aka.ms/MDCTerraformTemplates.
You need to provide all the accounts/projects you want to onboard and provide Terraform the Azure credentials you want it to use, for creating the security connector. You can think of the security connector as the object that connects Defender for Cloud to AWS/GCP and checks if it has all the necessary permissions.
You can create a single variables file to onboard multiple accounts/projects. After you provide everything that you want, you need to run the following commands:
terraform init \
terraform plan –var-file <variable-file-name>.tfvars \
terraform apply –var-file <variable-file-name>.tfvars
After you run these commands, Terraform creates all the security connectors inside of the same resource group. This effectively allows you automate the onboarding process of your accounts, projects, and everything else.
Some organizations have requirements for security connectors to reside in different resource groups. You can achieve this by modifying the Terraform templates you cloned. Now let's look at the second scenario.
2. Scenario: Onboarding all AWS accounts grouped under an AWS management account to Microsoft Defender for Cloud, using Terraform.
Have you ever asked yourself how you can onboard all AWS account under a AWS management account to Defender for Cloud using Terraform. You can achieve this scenario by performing the following actions.
2.1 Prepare the cloud you want to onboard:
Clone (or download) the Terraform templates available at https://aka.ms/MDCTerraformTemplates.
After you clone the Terraform templates there are three steps you need to do:
terraform init \
terraform plan –var-file <variable-file-name>.tfvars \
terraform apply –var-file <variable-file-name>.tfvars
After you apply it, Terraform does the rest. You need to do this process only once to onboard all accounts/projects grouper under a management account or organization. The Terraform templates you cloned, can integrate with your existing CI/CD process.
After you complete these three steps, your destination cloud is ready to be onboarded and you can proceed with the onboarding process on the Azure side.
2.2 Prepare the Azure cloud:
Now let’s go into the process of telling Azure that you’re ready to onboard these accounts/projects. Clone (or download) the Terraform templates located in the folder ‘Azure’ that are available at https://aka.ms/MDCTerraformTemplates.
You need to provide the management account/organization you want to onboard and you provide Terraform the Azure credentials you want it to create the security connector with. You can think of the security connector as the object that connects Defender for Cloud to AWS/GCP and checks if it has all the necessary permissions.
You can create a single variables file to onboard accounts/projects below a management account/organization. After you provide everything that you want, you need to run the following commands:
terraform init \
terraform plan –var-file <variable-file-name>.tfvars \
terraform apply –var-file <variable-file-name>.tfvars
After you run these commands, Terraform creates the security connector.
If you need to validate in the UI that the security connector(s) has been successfully created, log-in to the Azure portal and navigate to the Defender for Cloud Dashboard. Then select Environment Settings. The page that opens should display the security connectors for the accounts/projects you onboarded with Terraform templates.
Conclusion
Terraform templates provide you with a flexible way to automate the onboarding process of your AWS/GCP environment. In addition to being a widely used tool, an advantage of using Terraform is that you can use it for both AWS and GCP, Additionally, you can integrate the Terraform templates covered in this article, with your existing CI/CD process.
Ilay Alog, Site Reliability Engineer II,
Tomer Hoter, Senior Site Reliability Engineer,
Omer Hasson, Site Reliability Engineer
Lior Arviv, Senior Product Manager,
Ilay Alog, Site Reliability Engineer II
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.