Showing posts with label Azure Cloud. Show all posts
Showing posts with label Azure Cloud. Show all posts

Friday, September 13, 2024

Install Azure CLI in Windows | How to setup Azure CLI in Windows Laptop | How to Install Azure CLI in Windows Machine

The Azure command-line interface (Azure CLI) is a set of commands used to create and manage Azure resources. The Azure CLI is available across Azure services and is designed to get you working quickly with Azure, with an emphasis on automation. Azure CLI is Microsoft's cross-platform command-line experience for managing Azure resources.

Azure CLI can be installed in Windows using MSI or Zip distributions:

Download and install the latest release of the Azure CLI. When the installer asks if it can make changes to your computer, select the "Yes" box.

Or you can also try installing using zip as well.

Zip distributions:

https://aka.ms/installazurecliwindowszipx64

To check the version of Azure CLI

az version

Run the Azure CLI with the az command. To sign in, use the az login command.

az login

Thursday, April 18, 2024

GitHub Actions CICD Pipeline to Deploy Java WebApp into Azure App Service | Integration GitHub Actions with Azure App Service


Pre-requisites:

What are we going to do in this lab?
1. Create a Web App in Azure Cloud
2. Configure WebApp to Deploy using gitHub Actions
3. Create workflow yaml
4. Add steps/tasks in the yaml file
5. Run the workflow yaml
6. Check if Java Web App is deployed in Azure App Service

How to Create WebApp in Azure Portal?

1. Login portal.azure.com
2. Click on App services


3.Click on + Add or click on Create app service


Click on Web App. Choose your Azure subscription, usually Pay as you Go or Free trial subscription
Create a new resource group or you can use existing resource group)


Enter App service name(it should be unique)
Publish as Code
Run time stack as Java 17
Java Web Server stack --> Tomcat 10.0
Operating System as Linux
Region as Central US or where ever you are based at

Enter LinuxPlan name
Choose pricing plan

Now go to Deployment tab:
Enable basic authentication
and enable Continuous Deployment 


Click on GitHub account, Authorize.
Authorize AzureappService
now select organization, repo, branch



You can also click on preview file to get pipeline YAML code 

Click on Review and Create




Create Web App
Now make sure AzureAppService_PublishProfile secret is automatically created in GitHub repo you selected.



Create GitHub Actions CICD workflow yaml:

name: Build and deploy WAR app to Azure Web App
on:
  push:
    branches:
      - main
  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Java version
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'
      - name: Build with Maven
        run: mvn clean install -f MyWebApp/pom.xml
      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v3
        with:
          name: MyWebApp
          path: '${{ github.workspace }}'
  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: MyWebApp
      - name: Deploy to Azure Web App
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: 'spingbootwebapp'
          slot-name: 'Production'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_76B948D486E54ED7B06775D572207D40 }}
          package: '*.war'


Check the output after running the pipeline:


Verify if WebApp has been deployed into Azure App Service by browsing Web App url.

https://mysuperjavaapp.azurewebsites.net/MyWebApp/

Watch here all the steps in YouTube channel:

Thursday, August 24, 2023

Install Ansible on Red Hat Linux | How to setup Ansible on Red Hat Linux VM | Ansible install on Azure Linux Virtual Machine | Ansible Azure Integration

How to setup Ansible on Red Hat Linux VM and Integrate with Azure Cloud?

Ansible is #1 configuration management tool. It can also be used for infrastructure provisioning as well. or You can use Ansible in combination of Terraform which can take care of infra automation and Ansible can do configuration management. We will be setting up Ansible on Red Hat VM in Azure cloud And create some resources in Azure Cloud by using Ansible playbooks.


 
Ansible Architecture:
 

The best way to install Ansible in Linux is to use PIP, a package manager for Python.

Pre-requisites:
How to setup Ansible on Red Hat Linux VM

Watch Steps in YouTube channel:

Change host name to AnsibleMgmtNode
sudo hostnamectl set-hostname 
AnsibleMgmtNode

Update Repository
sudo yum update -y

Install Python-pip3
sudo yum install python3-pip -y

Upgrade pip3 sudo pip3 install --upgrade pip


# Install Ansible pip3 install "ansible==2.9.17"



check Ansible version
ansible --version


# Install Ansible azure_rm module for interacting with Azure.
pip3 install ansible[azure]

Authenticate with Azure


To configure Azure credentials, you need the following information:

  • Your Azure subscription ID and tenant ID
  • The service principal application ID and secret

Create an Azure Service Principal

Login to Azure first
az login
Enter Microsoft credentials

Run the following commands to create an Azure Service Principal:

az ad sp create-for-rbac --name <service-principal-name> \ 
--role Contributor \ 
--scopes /subscriptions/<subscription_id>
Save the above output in a file as you will not be able retrieve later.
Configure the Ansible credentials using one of the following techniques:

Option 1: Create Ansible credentials file

In this section, you create a local credentials file to provide credentials to Ansible. For security reasons, credential files should only be used in development environments.

mkdir ~/.azure 

vi ~/.azure/credentials


Insert the following lines into the file. Replace the placeholders with the service principal values.
[default] subscription_id=<subscription_id> client_id=<service_principal_app_id> secret=<service_principal_password> tenant=<service_principal_tenant_id>

Option 2: Define Ansible environment variables

On the host virtual machine, export the service principal values to configure your Ansible credentials.

export AZURE_SUBSCRIPTION_ID=<subscription_id> export AZURE_CLIENT_ID=<service_principal_app_id> export AZURE_SECRET=<service_principal_password> export AZURE_TENANT=<service_principal_tenant_id>

Test Ansible installation

You now have a virtual machine with Ansible installed and configured!

This section shows how to create a test resource group within your new Ansible configuration. If you don't need to do that, you can skip this section.

Option 1: Use an ad-hoc ansible command

Run the following ad-hoc Ansible command to create a resource group:

ansible localhost -m azure_rm_resourcegroup -a "name=my-rg123 location=eastus"

Option 2: Write and run an Ansible playbook

Create a simple playbook to create resource group in Azure.

sudo vi create-rg.yml

---

- hosts: localhost

  connection: local

  tasks:

    - name: Creating resource group

      azure_rm_resourcegroup:

        name: "myResourceGroup"

        location: "eastus"

Execute the playbook using ansible-playbook command.

ansible-playbook create-rg.yml

Now Login to Azure cloud to see if the resource group have been created.



Clean up Resources

Save the following code as delete-rg.yml

sudo vi delete-rg.yml

--- - hosts: localhost tasks: - name: Deleting resource group - "{{ name }}" azure_rm_resourcegroup: name: "{{ name }}" state: absent register: rg - debug: var: rg

ansible-playbook delete-rg.yml --extra-vars "name=myResourceGroup"

check in Azure cloud to see if the resource group have been deleted.

Tuesday, August 22, 2023

How to create Red Hat Linux Virtual Machine (VM) in Azure? | Create Red Hat Linux VM in Azure | How to connect to Azure VM from your local machine

How to Create Red Hat Linux Virtual Machines(VM) in Azure Cloud? 

Creating Virtual Machine is easy and straight forward in Azure Cloud. Let us see how to do that in Azure portal. 

Watch Steps in YouTube channel:

Pre-requisites:

  • Azure account subscription, click here if you don't have one.
  • SSH client such as Git bash, putty or iTerm(mac os)
Steps to create Red Hat Linux Virtual Machine in Azure
 
1. Login to Azure portal, go to https://portal.azure.com/
2. Click on Virtual Machines.

3. Click on Add/Create virtual machine. 

 

Enter resource group name, select region.



4. Select security type as Standard
5. Now enter the details as below or give values per your subscription and requirements. Select Red Hat Enterprise Linux 8.x VM from the drop down.


6. choose authentication type as SSH public key, enter azureuser as user name, enter key pair name.
This step will eventually create SSH keys and allow you to download in your machine.



7. Under Networking



Go with Allow selected ports - SSH port 22
And also select Delete public IP and NIC when VM is deleted option


8. Under Monitoring, disable Boot diagnostics

9. Click on Review, it may take a few mins to finish the validations. If all good, it should pass the validations. Click on Create.

10.  Now download the SSH keys and save it locally.
 

11. Once created, Click on virtual machines.

12. You should see the new VM is running like below:


How to connect to Azure VM from your local machine?



1. Now select that instance, click on connect


2.Then choose SSH 


3. Enter ssh key name in private key path 
4. Copy the value as it shows below in your local terminal(iTerm for Apple laptop) or Git bash for Windows laptop.

5. Make sure your SSH keys is not accessible by others, by executing the below command:

chmod 400 myUbuntuVM_key.pem
6. Now ssh into VM from your local machine using the key

ssh -i myUbuntuVM_key.pem azureuser@your_ip_address


Now it should show you that you are connected to Azure.

Monday, August 21, 2023

Install Ansible on Ubuntu | How to setup Ansible on Azure Ubuntu 22.0.4 instance | Ansible install on Azure Ubuntu Virtual Machine

Ansible is #1 configuration management tool. It can also be used for infrastructure provisioning as well. or You can use Ansible in combination of Terraform which can take care of infra automation and Ansible can do configuration management. We will be setting up Ansible on Azure Ubuntu VM.
 
Ansible Architecture:
 

The best way to install Ansible for Ubuntu is to add the project's PPA (personal package archive) to your system.

Pre-requisites:
Create new Ubuntu VM in Azure Cloud for installing Ansible, just open port 22.

Change host name to AnsibleMgmtNode
sudo hostnamectl set-hostname 
AnsibleMgmtNode

Update Repository
sudo apt-get update

Install Python-pip3
sudo apt install python3-pip -y

Upgrade pip3 sudo pip3 install --upgrade pip

# Install Ansible. pip3 install "ansible==2.9.17"

# Install Ansible azure_rm module for interacting with Azure.
pip3 install ansible[azure]

# Install Ansible.
sudo apt install ansible -y




Saturday, July 16, 2022

How to import existing Azure resources into Terraform Management? | Using Terraform to import existing resources on Azure

Let's say that you have created any resources in Azure Cloud by some other means(manually or using azure CLI) before you started using Terraform. You can import them under Terraform management instead of destroying and re-creating them from scratch. The terraform import command can be used to import existing resources. 

This command currently can import only one resource at a time. This means you can't yet point Terraform import to an entire collection of resources such as azure resource group and import all the resources under that group.

To achieve this import exercise, we will do the following:

1. Create resource group, app service manually in Azure cloud (yes, by not using terraform)

2. Create terraform file and write code to create the resource. 

3. Run terraform apply to see the error complaining resource exists

4. run terraform import

5. Verify in the state file that resource is imported into Terraform state.

6. Perform terraform destroy to clean up the imported resources

Watch the steps in YouTube channel:


Pre-requisites:

1. Install Terraform on your machine

2. Azure account setup

3. VS Code or any IDE


Let's create the resource manually in Azure Cloud first

Login to Azure portal - https://portal.azure.com/#home

Create App service manually in portal 


Create terraform file 

To import any resource, create a tf file first write a resource block for it in your configuration, establishing the name by which it will be known to Terraform:


terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">=2.99.0"
    }
  }
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}
# Add this block code if you want to import existing Resource group
resource "azurerm_resource_group" "rg" {
  name     = "myManualRG"
  location = "southcentralus"
  tags = {
    owner       = "ak"
    orgnization = "MyOrg"
  }
}
resource "azurerm_service_plan" "service-plan" {
  name                = "myServicePlan"
  location = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  os_type             = "Linux"
  sku_name            = "S1"
  tags = {
          environment = "dev"
  }
}
# Create JAVA app service
resource "azurerm_linux_web_app" "app-service" {
  name = "MyAwesomeSuperWebApp"
  location = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  service_plan_id = azurerm_service_plan.service-plan.id
  site_config {
    minimum_tls_version = "1.0"
    application_stack {
      java_server         = "TOMCAT"
      java_version        = "java11"
      java_server_version = 9.5
    }
  }
tags = {
    environment = "dev"
  }
}


Now run terraform apply command



terraform import can be run to import the resource under Terraform management.

Import Resource Group created manually in Azure Cloud into Terraform Management:

terraform import azurerm_resource_group.rg /subscriptions/XXXXX/resourceGroups/myManualRG


Terraform State View
Execute --> terraform state list
The above command will show the below output.
azurerm_resource_group.rg


This command will list the resources that are part of Terraform state.

Import App service plan created manually in Azure Cloud into Terraform Management:

terraform import azurerm_app_service_plan.service-plan /subscriptions/XXXXXX/resourceGroups/myManualRG/providers/Microsoft.Web/serverfarms/myServicePlan

Terraform State View
Execute --> terraform state list
The above command will show the below output.
azurerm_app_service_plan.service-plan
azurerm_resource_group.rg

Import App Service created manually in Azure Cloud into Terraform Management:
terraform import azurerm_app_service.app-service /subscriptions/XXXX/resourceGroups/myManualRG/providers/Microsoft.Web/sites/myPythonWebApp

Terraform State View
Execute --> terraform state list


The above command will show the below output.

azurerm_app_service.app-service
azurerm_app_service_plan.service-plan
azurerm_resource_group.rg

finally clean up the resources 
Execute --> terraform destroy
enter yes


This will clean up all three resources we have imported from Azure cloud.

🚀 Live AI-Enabled DevSecOps & Cloud Engineering Bootcamp – Sep 2026

Live AI-Enabled DevSecOps & Cloud Engineering Bootcamp from Coach AK - Sep 2026 Schedule