0
votes

I'm trying to create a Logic App through Terraform and facing issue related to API Connection.

Here are the manual steps for creating the API Connection:

  1. Create a Logic App in your resource group and go to Logic App Designer
  2. Select the HTTP trigger request and click on "Next Step", then search and select "Azure Container Instance"
  3. Click on Create or update container group and it should ask you to sign-in
  4. Now if you scroll all the way down, you should see "Connected to ...... Change Connection"
  5. If the Change Connection is clicked, it will show the existing aci connections or create a new one.

I'm trying to create a Logic App and I'm facing an issue with the above mentioned steps.

What I'm doing is:

  • Exported the existing Logic App template from another environment
  • Converted the values in the json as parameters and kept them in variables.tf and the final values in terraform.tfvars
  • The terraform plan is working fine, however the terraform apply is causing an issue

Error message:

Error: waiting for creation of Template Deployment "logicapp_arm_template" (Resource Group "resource_group_name"): Code="DeploymentFailed" Message="At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details." Details=[{"code":"NotFound","message":"{\r\n \"error\": {\r\n \"code\": \"ApiConnectionNotFound\",\r\n \"message\": \"**The API connection 'aci' could not be found**.\"\r\n }\r\n}"}]

Further troubleshooting shows that the error occurs in this line in terraform.tfvars

connections_aci_externalid = "/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.Web/connections/aci"

Deduced that the issue is since the "aci" is not created.

So, created the aci manually through the Azure Portal (see top of the post for steps).

However, when I hit terraform apply the new error below shows up:

A resource with the ID "/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.Resources/deployments/logicapp_arm_template" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_resource_group_template_deployment" for more information.

My question is, since I'm creating the Logic App using the existing template how should the "aci" portion be handled through Terraform?

1

1 Answers

1
votes

For your last error message, you could remove the terraform.tfstate and terraform.tfstate.backup files in the terraform working directory and existing resources in the Azure portal then run terraform plan and terraform apply again.

If you have a separate working ARM Template, you can invoke the template deployment with terraform resource azurerm_resource_group_template_deployment. You could provide the contents of the ARM Template parameters file with argument parameters_content and the contents of the ARM Template file with argument template_content.

In this case, If you have manually created a new API Connection, you can directly input your new API connection id /subscriptions/<subscription_id>/resourceGroups/<resourceGroup_id>/providers/Microsoft.Web/connections/aci. Alternatively, you can create the API Connections automatically when you deploy your ARM Template with resource Microsoft.Web/connections. Read this blog for more samples.