CiscoDevNet / cloud-cml

Run Cisco Modeling Labs on cloud infrastructure
https://www.cisco.com/go/cml
Apache License 2.0
51 stars 17 forks source link

Fix requirement to have both, AZ and AWS, CLI tools installed when using only one #11

Closed rschmied closed 8 months ago

rschmied commented 8 months ago

Problem description

Apparently, when trying to deploy to AWS and only having AWS CLI installed, the tooling creates an error like this:

$ terraform plan
module.deploy.random_id.id: Refreshing state... [id=_-Ij-Q]
module.deploy.module.aws[0].data.aws_ami.ubuntu: Reading...
module.deploy.module.aws[0].aws_security_group.sg-tf: Refreshing state... [id=sg-03eb5f041c6cd925e]
module.deploy.module.aws[0].data.aws_ami.ubuntu: Read complete after 1s [id=ami-0e6b0a1f7f29fa192]
module.deploy.module.aws[0].aws_instance.cml: Refreshing state... [id=i-0de8be81b8248ed20]

Planning failed. Terraform encountered an error while generating this plan.

╷
│ Error: unable to build authorizer for Resource Manager API: could not configure AzureCli Authorizer: could not parse Azure CLI version: launching Azure CLI: exec: "az": executable file not found in $PATH
│
│   with module.deploy.provider["registry.terraform.io/hashicorp/azurerm"],
│   on modules/deploy/main.tf line 13, in provider "azurerm":
│   13: provider "azurerm" {
│
╵

$

The above error says that the Azure CLI az isn't available (even though we do not want to deploy to Azure) and thus the entire plan and consequently the deploy fails.

In addition, the terraform init already installs both providers, currently Azure and AWS even though it's very likely that only ONE will be used by a particular person using the tooling.

Workaround

Install the Azure CLI by following the instructions here. Even if the CLI is not needed to deploy to AWS and there's no valid authentication/configuration for Azure, Terraform's Azure provider will call the Azure CLI to check various things.

Long term solution

It's likely that the different deployment paths need to be put into separate directories with potential code duplication. Otherwise, the issue we're currently seeing can not be avoided. Especially, if more cloud providers are added (GCP, DO, ...).

rschmied commented 8 months ago

It appears that without a valid Azure configuration (via az login), the tooling will not work in an AWS-only environment. Need to fix this ASAP.

rschmied commented 8 months ago

basically this:

https://stackoverflow.com/questions/70428374/how-to-make-the-provider-configuration-optional-and-based-on-the-condition-in-te

Looks like I need to re-structure the project to not have this problem.

rschmied commented 8 months ago

Fixed by PR #12 -- there's now a dummy module for the provider not in use... the downside here is that the user needs to run a 'prepare' script first to properly configure the deploy module. On Linux/macOS etc, this sets symbolic links, on Windows it simply copies over the file. An additional step to do but at least it removes the need to have both providers installed AND configured.