Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure Sql via CDK #2592

Merged
merged 12 commits into from
Mar 4, 2024
Merged

Azure Sql via CDK #2592

merged 12 commits into from
Mar 4, 2024

Conversation

mitchdenny
Copy link
Member

@mitchdenny mitchdenny commented Mar 3, 2024

This PR adds support for creating Azure SQL Database resources via the CDK. Here is the usage:

builder.AddSqlServer("sql").AsAzureSqlDatabaseConstruct().AddDatabase("db");

This will produce the following Bicep output:

targetScope = 'resourceGroup'

@description('')
param location string = resourceGroup().location

@description('')
param principalId string

@description('')
param principalName string


resource sqlServer_l5O9GRsSn 'Microsoft.Sql/servers@2022-08-01-preview' = {
  name: toLower(take(concat('sql', uniqueString(resourceGroup().id)), 24))
  location: location
  properties: {
    version: '12.0'
    minimalTlsVersion: '1.2'
    publicNetworkAccess: 'Enabled'
    administrators: {
      administratorType: 'ActiveDirectory'
      login: principalName
      sid: principalId
      tenantId: subscription().tenantId
      azureADOnlyAuthentication: true
    }
  }
}

resource sqlFirewallRule_Zjpl1WAet 'Microsoft.Sql/servers/firewallRules@2020-11-01-preview' = {
  parent: sqlServer_l5O9GRsSn
  name: 'AllowAllWindowsAzureIps'
  properties: {
    startIpAddress: '0.0.0.0'
    endIpAddress: '0.0.0.0'
  }
}

resource sqlDatabase_nUUkbFiVl 'Microsoft.Sql/servers/databases@2022-08-01-preview' = {
  parent: sqlServer_l5O9GRsSn
  name: 'sqldb'
  location: location
  properties: {
  }
}

output sqlServerFqdn string = sqlServer_l5O9GRsSn.properties.fullyQualifiedDomainName

And the following manifest output:

{
  "resources": {
    "storagesku": {
      "type": "parameter.v0",
      "value": "{storagesku.inputs.value}",
      "inputs": {
        "value": {
          "type": "string"
        }
      }
    },
    "locationOverride": {
      "type": "parameter.v0",
      "value": "{locationOverride.inputs.value}",
      "inputs": {
        "value": {
          "type": "string"
        }
      }
    },
    "storage": {
      "type": "azure.bicep.v0",
      "path": "storage.module.bicep",
      "params": {
        "principalId": "",
        "principalType": "",
        "storagesku": "{storagesku.value}",
        "locationOverride": "{locationOverride.value}"
      }
    },
    "blobs": {
      "type": "value.v0",
      "connectionString": "{storage.outputs.blobEndpoint}"
    },
    "sql": {
      "type": "azure.bicep.v0",
      "connectionString": "Server=tcp:{sql.outputs.sqlServerFqdn},1433;Encrypt=True;Authentication=\u0022Active Directory Default\u0022",
      "path": "sql.module.bicep",
      "params": {
        "principalId": "",
        "principalName": ""
      }
    },
    "sqldb": {
      "type": "value.v0",
      "connectionString": "{sql.connectionString};Database=sqldb"
    },
    "api": {
      "type": "project.v0",
      "path": "../CdkSample.ApiService/CdkSample.ApiService.csproj",
      "env": {
        "OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES": "true",
        "OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES": "true",
        "ConnectionStrings__blobs": "{blobs.connectionString}",
        "ConnectionStrings__sqldb": "{sqldb.connectionString}"
      },
      "bindings": {
        "http": {
          "scheme": "http",
          "protocol": "tcp",
          "transport": "http"
        },
        "https": {
          "scheme": "https",
          "protocol": "tcp",
          "transport": "http"
        }
      }
    }
  }
}

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Mar 3, 2024
@mitchdenny mitchdenny marked this pull request as ready for review March 3, 2024 22:48
@mitchdenny mitchdenny merged commit 584420b into main Mar 4, 2024
8 checks passed
@mitchdenny mitchdenny deleted the mitchdenny/cdk-sqlserver branch March 4, 2024 02:16
@github-actions github-actions bot locked and limited conversation to collaborators Apr 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants