SQL Server terraform azurerm_mssql_server creation time_out with pipeline

smtd7mpg  于 2023-06-21  发布在  其他
关注(0)|答案(1)|浏览(85)

I use terraform scripts to create azure sql server resource, scripts seems ok because I can run terraform apply successful in my local PC, but when apply it by pipeline, it always get timeout error

module.sql.azurerm_mssql_server.sql_server: Still creating... [30m10s elapsed]
2023-06-08T07:59:30.903Z [ERROR] provider.terraform-provider-azurerm_v3.58.0_x5: Response contains error diagnostic: tf_rpc=ApplyResourceChange tf_req_id=741d1c06-f641-256b-eb93-421bb15a7eb5 diagnostic_detail= diagnostic_severity=ERROR diagnostic_summary="waiting for creation/update of Server: (Name "a250621-ds-sql-server-eu2-dev" / Resource Group "eastus2-250621-datasquad"): Code="OperationTimedOut" Message="The operation timed out and automatically rolled back. Please retry the operation."" tf_proto_version=5.3 tf_provider_addr=provider @caller=github.com/hashicorp/terraform-plugin-go@v0.14.3/tfprotov5/internal/diag/diagnostics.go:55 @module=sdk.proto tf_resource_type=azurerm_mssql_server timestamp=2023-06-08T07:59:30.903Z
2023-06-08T07:59:30.903Z [ERROR] vertex "module.sql.azurerm_mssql_server.sql_server" error: waiting for creation/update of Server: (Name "a250621-ds-sql-server-eu2-dev" / Resource Group "eastus2-250621-datasquad"): Code="OperationTimedOut" Message="The operation timed out and automatically rolled back. Please retry the operation."
╷
│ Error: waiting for creation/update of Server: (Name "a250621-ds-sql-server-eu2-dev" / Resource Group "eastus2-250621-datasquad"): Code="OperationTimedOut" Message="The operation timed out and automatically rolled back. Please retry the operation."
│ 
│   with module.sql.azurerm_mssql_server.sql_server,
│   on ../modules/sql/main.tf line 8, in resource "azurerm_mssql_server" "sql_server":
│    8: resource "azurerm_mssql_server" "sql_server" {

enter image description here

the terraform version is

$ terraform --version
Terraform v1.2.2

azure provider version is 3.58.0, I tried 3.59.0(the newest one, still same issue)

$ terraform init -backend-config="../tfvars/tf-backend-dev.conf"
Initializing modules...
- sql in ../modules/sql
Initializing the backend...
Successfully configured the backend "azurerm"! Terraform will automatically
use this backend unless the backend configuration changes.
Initializing provider plugins...
- Finding hashicorp/random versions matching "~> 3.0"...
- Finding hashicorp/azurerm versions matching "3.58.0"...
- Installing hashicorp/random v3.5.1...
- Installed hashicorp/random v3.5.1 (signed by HashiCorp)
- Installing hashicorp/azurerm v3.58.0...
- Installed hashicorp/azurerm v3.58.0 (signed by HashiCorp)

but in my local running, the sql server can be created in a very shot time, always less than 2mins, could someone help on this issue?

5t7ly7z5

5t7ly7z51#

I created one Project in gitlab and added the Terraform files to deploy Azure SQL like below:-

I referred the terraform files from this Quickstart and just added the extra code below in providers.tf from the Quickstart:-

terraform {
  required_version = ">=1.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>3.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~>3.0"
    }
  }
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
  
subscription_id = "xxxxd6-b4fd-e2b6e97cb2a7"
tenant_id = "xxxxx9ed-af9038592395"
client_id = "xxxxxx6d26a31435cb"
client_secret = "xxxxxxxxxx-CS0ifbLE"
features {
  resource_group {
    prevent_deletion_if_contains_resources = false
  }
}

}

Added Variables like below:-

But we have also provided the service principal settings in providers.tf file as mentioned above.

Added the below code in the pipeline above:-

.gitlab-ci.yml

stages:
  - validate
  - plan
  - apply

default:
  image:
    name: hashicorp/terraform:latest
    entrypoint:
      - /usr/bin/env
      - "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

  before_script:
    - terraform init
  cache:
    key: terraform
    paths:
      - .terraform

terraform_validate:
  stage: validate
  script:
    - terraform validate

terraform_plan:
  stage: plan
  script: 
    - terraform plan -out main.tfplan"
  artifacts:
    paths:
      - plan

terraform_apply:
  stage: apply
  script:
    - terraform apply --main.tfplan
  when: manual
  allow_failure: false
  only:
    refs:
      - main

After the validate and plan stage, I manually started the Apply stage by clicking on it. Make sure to click on apply stage manually to start the deployment if it does not start by itself.

Output:-

I ran the pipeline again and it created another resource with unique name as the code files in the Quickstart creates a unique string for Azure SQL resources, This time the apply stage ran by itself without the need to manually start it:-

Reference:- Using GitLab To Deploy Terraform Code in Azure | by Rafael Medeiros | Medium

相关问题