Showing posts with label azure. Show all posts
Showing posts with label azure. Show all posts

Saturday, 2 November 2024

Create windows vm in Azure using terraform




Creating a Windows VM in Azure using Terraform is a great way to manage your infrastructure as code. Here's a step-by-step guide to help you get started:

Step 1: Prerequisites

  1. Azure Subscription: Ensure you have an active Azure subscription.

  2. Terraform: Install Terraform on your local machine.

  3. Azure CLI: Install and configure the Azure CLI.

Step 2: Create Terraform Configuration Files

Create a directory for your Terraform configuration files and navigate to it:

sh
mkdir terraform-windows-vm
cd terraform-windows-vm

Create a main.tf file and add the following Terraform code:

hcl
# Provider configuration
provider "azurerm" {
  features {}
}

# Resource group
resource "azurerm_resource_group" "rg" {
  name     = "terraform-windows-vm-rg"
  location = "East US"
}

# Virtual network
resource "azurerm_virtual_network" "vnet" {
  name                = "terraform-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

# Subnet
resource "azurerm_subnet" "subnet" {
  name                 = "terraform-subnet"
  address_prefix       = "10.0.1.0/24"
  virtual_network_name = azurerm_virtual_network.vnet.name
  resource_group_name  = azurerm_resource_group.rg.name
}

# Public IP
resource "azurerm_public_ip" "public_ip" {
  name                = "terraform-public-ip"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  allocation_method   = "Dynamic"
}

# Network interface
resource "azurerm_network_interface" "nic" {
  name                = "terraform-nic"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
  }

  ip_configuration {
    name                          = "public"
    subnet_id                     = azurerm_subnet.subnet.id
    public_ip_address_id          = azurerm_public_ip.public_ip.id
  }
}

# Storage account
resource "azurerm_storage_account" "storage" {
  name                     = "terraformstorage"
  location                 = azurerm_resource_group.rg.location
  resource_group_name      = azurerm_resource_group.rg.name
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

# Virtual machine
resource "azurerm_windows_virtual_machine" "vm" {
  name                  = "terraform-vm"
  location              = azurerm_resource_group.rg.location
  resource_group_name   = azurerm_resource_group.rg.name
  network_interface_ids = [azurerm_network_interface.nic.id]
  vm_size               = "Standard_D2_v4"
  storage_account_name  = azurerm_storage_account.storage.name
  storage_account_key   = azurerm_storage_account.storage.primary_access_key
  os_disk_size_gb       = 30
  os_disk_caching       = "ReadWrite"
  admin_username        = "adminuser"
  admin_password        = "Password1234!"
  computer_name         = "terraform-vm"
}

Step 3: Initialize Terraform

Run the following command to initialize Terraform and download the required providers:

sh
terraform init

Step 4: Create Execution Plan

Run the following command to create an execution plan:

sh
terraform plan

Step 5: Apply the Execution Plan

Run the following command to apply the execution plan and create the resources in Azure:

sh
terraform apply

Step 6: Verify the Results

Once the resources are created, you can verify the results by checking the Azure portal or using Azure CLI commands.




Friday, 14 May 2021

How to check roles for sql users

 

The following query returns the members of the database roles.

SELECT DP1.name AS DatabaseRoleName,   

   isnull (DP2.name, 'No members'AS DatabaseUserName   
 FROM sys.database_role_members AS DRM  
 RIGHT OUTER JOIN sys.database_principals AS DP1  
   ON DRM.role_principal_id = DP1.principal_id  
 LEFT OUTER JOIN sys.database_principals AS DP2  
   ON DRM.member_principal_id = DP2.principal_id  
WHERE DP1.type = 'R'
ORDER BY DP1.name;  

Tuesday, 9 February 2021

Create User Group in Azure using Azure CLI (powershell)

Steps1 - Login to the azure account with  right account with azure AD access. 

Step 2 - Open CloudShell by clicking the icon on the top right corner.

Step 3 - You'll need to associate CLI session with a Storage account. So follow the instruction and connect with the storage account. 

Step 4 - Select the PowerShell in the dropdown window 


You'll see something like this screenshot:



Step 5 - type the below command and press enter:

 PS /home/sanjeeb> Connect-AzureAd

 Step 6 - type the below code (change the group name as your need):

PS /home/sanjeeb>New-AzureADGroup -Description "siteadmingroup" -DisplayName "siteadmingroup" -MailEnabled $false -SecurityEnabled $true -MailNickName "siteadmingroup"

On success, you'll see the object id like below: 

ObjectId                             DisplayName             Description

--------                             -----------             -----------

daf23147-8123-dc23-bsb8-d2d2a5224271 siteadmingroup siteadmingroup

Wednesday, 27 January 2021

Generate Managed Identity Using Powershell for Azure DataFactory

Open the powershell in the azure. (Select the correct subscription and create storage location for powershell or select an existing storage account)  

Run the below query with correct value inside double quote: 

Set-AzDataFactoryV2 -ResourceGroupName "MYResourceGroup" -Name "mydatafactory" -Location "UK South"


Press enter, and you'll be prompted to confirm. Type Y and press Enter.

You should see output like below:

Confirm

A data factory with the name mydatafactory in the resource group MYResourceGroup exists.

Continuing execution may overwrite the existing one.

Are you sure you want to continue?

[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y


DataFactoryName   : mydatafactory

DataFactoryId     : /subscriptions/e01105ad-822a-447a-2dd0-43124d16223l/resourceGroups/MYResourceGroup/providers/Microsoft.DataFactory/factories/mydatafactory

ResourceGroupName : MYResourceGroup

Location          : uksouth

Tags              : {}

Identity          : Microsoft.Azure.Management.DataFactory.Models.FactoryIdentity

ProvisioningState : Succeeded

RepoConfiguration :

GlobalParameters  :


For detail information read: Managed identity for Data Factory - Azure Data Factory | Microsoft Docs

Done!

Thursday, 21 January 2021

Create Build/Release Pipeline for SSIS in Azure SQL server



Create a new pipeline in devops.
Select the source:
Select an empty template

Select Get Sources in the left panel:


Now add a new agent job to Build SSIS. Click the + icon.



Search for SSS job. (if it's not already installed, then first install it from the marketplace)
Add SSIS build and then add SSIS Deploy 
Once you add these two jobs, your screen should look like this:

Now Select Build SSIS:
in the Project Path field, select the directory where .sln file is located. 
Leave the rest of the fields as default.
Now Select Deploy SSIS job to configure deploying settings 

add source path = $(Build.ArtifactStagingDirectory) (i.e. output path value from Build SSIS job-Previous step).
select destination type = SSISDB.
Destination Server = realdatabaseserver01.database.windows.net
Destination Path = /SSISDB/masterdatahub
Authentication type = SQL Server Authentication (you can choose any other option as per your need)


Click on the Triggers tab and configure Continuous Integration. 


Now Save and Queue the Pipeline. 
Once released you will see the ssis package under masterdatahub folder



That's everything for Azure devops continuous integration/ continuous deployment process for releasing ssis project to azure sql server. Good luck!


Wednesday, 11 November 2020

How To create SSIS user in Azure SQL server

I've assumed you have already configured domain in azure and have created a domain user.

Create Application in app registration  

First of all Create an app registration [myssissqlserver01]


Set up Active directory Admin in sql server

Now Go to the sql server (not the database) and on settings menu, click Active directory Admin



Click Set Admin and add your domain account. (in case you don't have domain configured , Configure a new domain and create a domain user first)

Connect to the database using MS Sql server management studio

Once admin is set , Open MS Sql server management studio and connect to the database.



Since we have added user to active directory admin, the user will have access to master database. So we can choose default database to connect, 


Create a user in ssis and add to a suitable role

Create a new query window selecting SSISDB database

execute the below queries. Note that the user name should be identical to the Application name created in App registration section . [ myssissqlserver01]

create user [myssissqlserver01] from external provider;

exec sp_addrolemember [db_owner],[myssissqlserver01];


Monday, 17 August 2020

Add Azure Devops plugins in Android Studio for Git or TFVC Version control

 By default, Android studio can not connect to the Azure devops source codes. To connect to the Azure devops, a plugins by Microsoft needs to be installed in the android studio. To install the plugin, follow the below steps:


1) Open a project in Android studio
2) go to File --> Setting
3) go to Plugins
4) Search for azure devops
5) Click Install
6) Restart the Android studio.
7) Menu->VCS->Checkout from Version Control -> Azure Devops Git
8)Click Sign in ... 
9) Follow the wizard to connect the project.

Create Azure SQL User and add a sql role

I wanted to add a new sql user "sqluser" in Azure SQL server to access a database "sanjeeb". 

Steps to create a new SQL user. 

1) Connect to the Azure sql server with existing user using Microsoft SQL Server Management studio

2) Open new query window under Master database


3) Run the below script
    
    CREATE LOGIN [sqllogin] WITH PASSWORD = 'verystrongpassword#1' 
   



4) Open new query window under database "sanjeeb"

5) Run the below script

CREATE USER [sqluser]
FOR LOGIN [sqllogin]
WITH DEFAULT_SCHEMA =  dbo
GO

-- Add user to the database owner role
EXEC sp_addrolemember N'db_owner', N'sqluser'
GO

6) You've now created a user login "sqllogin", user account "sqluser" and added the user to "db_owner role"



7) Connect to the database using the new user login account. 

login: sqllogin
password:verystrongpassword#1


8) Click Options and select Connection properties tab

9) input Connect to database = sanjeeb

10) Click connect

Thursday, 16 July 2020

Add nuget packages to Artifacts in Azure devops

download latest nuget.exe from the nuget site. (google it)

copy the nuget.exe in d: (any location is fine)

Use visual studio -> Package manager console. 
run the below command
D:\nuget.exe push -Source "IGTelerikFeed" -ApiKey az D:\Telerik.UI.for.AspNet.Core.2020.2.617.nupkg

[Note: It will ask Credential to connect to the devops when you press enter. ]

Friday, 26 June 2020

Powershell script to create OS disk in azure from storage account blob image file (vhd)

Powershell script to create OS disk in azure from storage account blob image file (vhd)


#Provide the subscription Id where Managed Disks will be created
$subscriptionId = 'd7x2bxx8-62xe-4xdx-xa8x-20x0x3x2x5xx'

#Provide the name of your resource group where Managed Disks will be created. 
$resourceGroupName ='MYResourceGroupName'

#Provide the name of the Managed Disk
$diskName = 'Project_Disk2'

#Provide the size of the disks in GB. It should be greater than the VHD file size.
$diskSize = '90'

#Provide the storage type for Managed Disk. PremiumLRS or StandardLRS.
$storageType = 'Premium_LRS'

#Provide the Azure region (e.g. westus) where Managed Disk will be located.
#This location should be same as the storage account where VHD file is stored
#Get all the Azure location using command below:
#Get-AzLocation
$location = 'ukwest'

#Provide the URI of the VHD file (page blob) in a storage account. Please not that this is NOT the SAS URI of the storage container where VHD file is stored. 
#e.g. https://contosostorageaccount1.blob.core.windows.net/vhds/contosovhd123.vhd
#Note: VHD file can be deleted as soon as Managed Disk is created.
$sourceVHDURI = 'https://sanjeebojha.blob.core.windows.net/myproject/myoriginalVM.vhd'

#Provide the resource Id of the storage account where VHD file is stored.
#e.g. /subscriptions/6472s1g8-h217-446b-b509-314e17e1efb0/resourceGroups/MDDemo/providers/Microsoft.Storage/storageAccounts/contosostorageaccount
#This is an optional parameter if you are creating managed disk in the same subscription
$storageAccountId = '/subscriptions/d7x2bxx8-62xe-4xdx-xa8x-20x0x3x2x5xx/resourceGroups/MYResourceGroupName/providers/Microsoft.Storage/storageAccounts/sanjeebojha'

#Set the context to the subscription Id where Managed Disk will be created
Select-AzSubscription -SubscriptionId $SubscriptionId

$diskConfig = New-AzDiskConfig -AccountType $storageType -OsType Windows -Location $location -CreateOption Import -StorageAccountId $storageAccountId -SourceUri $sourceVHDURI 

New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName

Convert Premium SSD to Standard HDD in Azure

Powershell script to convert Premium SSD disk to standard HDD 


# Name of the resource group that contains the VM
$rgName = 'MYResourceGroupName'

# Name of the your virtual machine
$vmName = 'MyVirtualMachineName'

# Choose between Standard_LRS and Premium_LRS based on your scenario
$storageType = 'Standard_LRS'

# Premium capable size
# Required only if converting storage from Standard to Premium
#$size = 'Standard_DS2_v2'


#Provide the subscription Id where Managed Disks will be created
$subscriptionId = 'd7x2bxx8-62xe-4xdx-xa8x-20x0x3x2x5xx'

Select-AzSubscription -SubscriptionId $SubscriptionId



# Stop and deallocate the VM before changing the size
Stop-AzVM -ResourceGroupName $rgName -Name $vmName -Force

$vm = Get-AzVM -Name $vmName -resourceGroupName $rgName

# Change the VM size to a size that supports Premium storage
# Skip this step if converting storage from Premium to Standard
#$vm.HardwareProfile.VmSize = $size
#Update-AzVM -VM $vm -ResourceGroupName $rgName

# Get all disks in the resource group of the VM
$vmDisks = Get-AzDisk -ResourceGroupName $rgName 

# For disks that belong to the selected VM, convert to Premium storage
foreach ($disk in $vmDisks)
{
if ($disk.ManagedBy -eq $vm.Id)
{
$diskUpdateConfig = New-AzDiskUpdateConfig –AccountType $storageType
Update-AzDisk -DiskUpdate $diskUpdateConfig -ResourceGroupName $rgName `
-DiskName $disk.Name
}
}

Start-AzVM -ResourceGroupName $rgName -Name $vmName

Create a Directory from existing disk in proxmox server

Scenario:   I have an NVMe disk with an ext4 partition that was previously used as a directory in a Proxmox server. After reinstalling Proxm...