<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Karthikmp on Medium]]></title>
        <description><![CDATA[Stories by Karthikmp on Medium]]></description>
        <link>https://medium.com/@karthikmp9686?source=rss-c1b51362229b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*F9HtDQMgraKVjHxf</url>
            <title>Stories by Karthikmp on Medium</title>
            <link>https://medium.com/@karthikmp9686?source=rss-c1b51362229b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 07:29:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@karthikmp9686/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ Deploying a Java Application to Azure Virtual Machine using CI/CD with Azure DevOps]]></title>
            <link>https://medium.com/@karthikmp9686/deploying-a-java-application-to-azure-virtual-machine-using-ci-cd-with-azure-devops-388c3f541619?source=rss-c1b51362229b------2</link>
            <guid isPermaLink="false">https://medium.com/p/388c3f541619</guid>
            <dc:creator><![CDATA[Karthikmp]]></dc:creator>
            <pubDate>Fri, 18 Jul 2025 14:52:00 GMT</pubDate>
            <atom:updated>2025-07-18T14:52:00.732Z</atom:updated>
            <content:encoded><![CDATA[<p>In this blog post, we’ll walk you through the complete process of deploying a Java application to an <strong>Azure Virtual Machine (VM)</strong> using <strong>Azure DevOps CI/CD pipelines</strong>. This tutorial is perfect for developers and DevOps engineers who want to automate Java app deployment to Azure infrastructure using modern DevOps tools and best practices.</p><p>We will follow an end-to-end DevOps workflow, including:</p><ul><li>Building and packaging a Java application using <strong>Maven</strong></li><li>Containerizing the application with <strong>Docker</strong></li><li>Pushing the Docker image to <strong>Azure Container Registry (ACR)</strong></li><li>Provisioning an <strong>Ubuntu-based Azure Virtual Machine</strong></li><li>Installing <strong>Docker</strong> and <strong>Docker Compose</strong> on the VM</li><li>Using an <strong>Azure DevOps Release Pipeline</strong> to connect to the VM and deploy the containerized app</li></ul><p>This setup is ideal for teams who:</p><ul><li>Prefer using Virtual Machines over Platform-as-a-Service (PaaS)</li><li>Want full control over their app runtime environment</li><li>Need to deploy custom infrastructure and software along with their apps</li><li>Are moving from traditional on-premise environments to the cloud</li></ul><h3>Why Choose Azure VM for Java App Deployment?</h3><p>While Azure App Service and AKS are excellent choices for modern apps, Azure Virtual Machines still offer unmatched flexibility and control. You can install any software, configure custom dependencies, run background services, and fine-tune security policies. This makes VMs a solid choice for certain workloads, including:</p><ul><li>Monolithic applications</li><li>Legacy Java apps</li><li>Systems requiring custom OS-level control</li><li>Internal services that don’t need autoscaling or Kubernetes</li></ul><h3>What You’ll Learn</h3><p>By the end of this tutorial, you’ll understand how to:</p><p>✅ Provision a VM with Terraform or Azure Portal<br> ✅ Configure secure access with SSH and Service Principals<br> ✅ Automate Java builds and Docker image creation in Azure DevOps<br> ✅ Push Docker images to Azure Container Registry (ACR)<br> ✅ SSH into the Azure VM and deploy containers using Release Pipelines<br> ✅ Implement production-ready CI/CD with deployment stages</p><h3>Step 1: Preparing Your Java Application and Dockerfile</h3><p>Before we start with the pipeline and Azure setup, we need to ensure your Java application is Docker-ready. In this section, we’ll:</p><ul><li>Build a simple Java application (using Maven)</li><li>Write a Dockerfile to containerize it</li><li>Test it locally (optional but recommended)</li></ul><h3>🧱 Project Structure</h3><p>Assume you have a basic Maven Java application like this:</p><pre>java-app/<br>├── Dockerfile<br>├── pom.xml<br>└── src/<br>    └── main/<br>        └── java/<br>            └── com/<br>                └── example/<br>                    └── App.java</pre><h3>🔧 Sample Java Code (App.java)</h3><pre>package com.example;<br>public class App {<br>    public static void main(String[] args) {<br>        System.out.println(&quot;Java app running in Docker on Azure VM!&quot;);<br>    }<br>}</pre><h3>📦 Maven Build (pom.xml)</h3><p>Ensure your pom.xml includes a plugin to build an executable .jar file:</p><pre>&lt;build&gt;<br>    &lt;plugins&gt;<br>        &lt;plugin&gt;<br>            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;<br>            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;<br>            &lt;version&gt;3.8.1&lt;/version&gt;<br>            &lt;configuration&gt;<br>                &lt;source&gt;11&lt;/source&gt;<br>                &lt;target&gt;11&lt;/target&gt;<br>            &lt;/configuration&gt;<br>        &lt;/plugin&gt;<br>        &lt;plugin&gt;<br>            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;<br>            &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;<br>            &lt;version&gt;3.3.0&lt;/version&gt;<br>            &lt;configuration&gt;<br>                &lt;archive&gt;<br>                    &lt;manifest&gt;<br>                        &lt;mainClass&gt;com.example.App&lt;/mainClass&gt;<br>                    &lt;/manifest&gt;<br>                &lt;/archive&gt;<br>                &lt;descriptorRefs&gt;<br>                    &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;<br>                &lt;/descriptorRefs&gt;<br>            &lt;/configuration&gt;<br>            &lt;executions&gt;<br>                &lt;execution&gt;<br>                    &lt;id&gt;make-assembly&lt;/id&gt;<br>                    &lt;phase&gt;package&lt;/phase&gt;<br>                    &lt;goals&gt;<br>                        &lt;goal&gt;single&lt;/goal&gt;<br>                    &lt;/goals&gt;<br>                &lt;/execution&gt;<br>            &lt;/executions&gt;<br>        &lt;/plugin&gt;<br>    &lt;/plugins&gt;<br>&lt;/build&gt;</pre><h3>🐳 Writing the Dockerfile</h3><p>Create a Dockerfile in the root of your project:</p><pre># Use an OpenJDK base image<br>FROM openjdk:17-jdk-slim<br># Set the working directory<br>WORKDIR /app<br># Copy the jar file to the image<br>COPY target/java-app-jar-with-dependencies.jar app.jar<br># Run the application<br>ENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;, &quot;app.jar&quot;]</pre><h3>🧪 Local Test (Optional)</h3><p>If you want to test locally:</p><pre># Build the app<br>mvn clean package<br># Build the Docker image<br>docker build -t java-app .<br># Run the container<br>docker run --rm java-app</pre><p>Output should be:</p><pre>Java app running in Docker on Azure VM!</pre><p>Once your Java app and Dockerfile are ready, you’re all set to move to <strong>CI/CD automation in Azure DevOps</strong>.</p><h3>🚀 Step 2: Setting Up Azure DevOps CI Pipeline to Build &amp; Push Docker Image</h3><p>This step will cover how to configure your <strong>Azure DevOps CI pipeline</strong> to:</p><ul><li>Build your Java project with Maven</li><li>Create a Docker image using the Dockerfile</li><li>Push that image to <strong>Azure Container Registry (ACR)</strong></li></ul><h3>✅ Prerequisites</h3><p>Before you create the pipeline, make sure:</p><ol><li>✅ You have an <strong>Azure DevOps Project</strong> created.</li><li>✅ Your code (Java app + Dockerfile) is pushed to a <strong>Git repository</strong> in that project.</li><li>✅ You’ve created an <strong>Azure Container Registry (ACR)</strong>.</li><li>✅ ACR login server URL, username, and password are available.</li></ol><h3>🔑 Get ACR Credentials</h3><p>From Azure Portal:</p><ul><li>Go to your ACR resource → <strong>Access Keys</strong></li><li>Copy the following:</li><li>Login server (e.g., yourregistry.azurecr.io)</li><li>Username</li><li>Password</li></ul><p>You’ll use these as pipeline secrets.</p><h3>🔐 Add Pipeline Secrets (Service Connections)</h3><ol><li>Go to <strong>Project Settings → Service connections</strong></li><li>Click <strong>New service connection → Docker Registry</strong></li><li>Fill the details:</li></ol><ul><li>Registry type: Others</li><li>Docker registry: <a href="https://yourregistry.azurecr.io/">https://yourregistry.azurecr.io</a></li><li>Username and Password: From ACR Access Keys</li><li>Name: acr-connection</li></ul><h3>📄 Create azure-pipelines.yml in Your Repo Root</h3><pre>trigger:<br>  - main<br>pool:<br>  vmImage: &#39;ubuntu-latest&#39;<br>variables:<br>  imageName: &#39;java-app&#39;<br>stages:<br>  - stage: Build<br>    jobs:<br>      - job: BuildAndPush<br>        steps:<br>          - task: Maven@3<br>            inputs:<br>              mavenPomFile: &#39;pom.xml&#39;<br>              goals: &#39;package&#39;<br>              options: &#39;-DskipTests=true&#39;<br>              javaHomeOption: &#39;JDKVersion&#39;<br>              jdkVersionOption: &#39;1.11&#39;<br>              mavenVersionOption: &#39;Default&#39;<br>              publishJUnitResults: false<br>          - task: Docker@2<br>            displayName: &#39;Build Docker Image&#39;<br>            inputs:<br>              command: &#39;build&#39;<br>              Dockerfile: &#39;**/Dockerfile&#39;<br>              tags: |<br>                $(imageName):$(Build.BuildId)<br>          - task: Docker@2<br>            displayName: &#39;Push Docker Image to ACR&#39;<br>            inputs:<br>              containerRegistry: &#39;acr-connection&#39;<br>              repository: &#39;$(imageName)&#39;<br>              command: &#39;push&#39;<br>              tags: |<br>                $(Build.BuildId)</pre><h3>💡 🔄 What This Pipeline Does — Step-by-Step</h3><ol><li><strong>Trigger on Code Push</strong></li></ol><ul><li><strong>Trigger:</strong></li><li>The pipeline is automatically triggered on every push to the main branch.</li><li>Ensures CI/CD runs as soon as code changes are committed.</li></ul><ol><li><strong>Build the Java Application</strong></li></ol><ul><li><strong>Task:</strong> Maven@3</li><li>Compiles the Java code.</li><li>Runs tests (if defined).</li><li>Packages the application into a <strong>fat JAR</strong> (includes all dependencies).</li><li>Common Maven command:</li></ul><pre>mvn clean package</pre><ol><li><strong>Build Docker Image</strong></li></ol><ul><li><strong>Task:</strong> Docker@2 (build step)</li><li>Uses the Dockerfile in the repository to build a container image.</li><li>Tags the image appropriately (e.g., myapp:$(Build.BuildId)).</li><li>Example configuration:</li></ul><pre>docker build -t myacr.azurecr.io/myapp:$(Build.BuildId) .</pre><ol><li><strong>Push Docker Image to ACR</strong></li></ol><ul><li><strong>Task:</strong> Docker@2 (push step)</li><li>Logs into Azure Container Registry using the Azure DevOps <strong>service connection</strong>.</li><li>Pushes the built Docker image to the specified ACR repository.</li><li>Makes the image available for use on the Azure VM.</li></ul><h3>📌 Best Practices</h3><ul><li>Store sensitive values (if needed) in <strong>Library &gt; Variable Groups</strong> or <strong>Pipeline secrets</strong>.</li><li>Use meaningful tags for images like :latest, :v1.0.0, or :$(Build.SourceBranchName) instead of just BuildId.</li><li>Enable build validation and code scan if needed.</li></ul><h3>🖥️ Step 3: Provisioning Azure VM for Deployment</h3><p>Before we can deploy our Java application to a Virtual Machine (VM), we need to provision and prepare the VM on Azure. This step ensures the VM is ready to run Docker containers pulled from Azure Container Registry (ACR).</p><h3>✅ What You’ll Do in This Step:</h3><ul><li>Create an Azure Virtual Machine (Ubuntu or any Linux distro)</li><li>Install Docker on the VM</li><li>Allow network access through appropriate ports (e.g., 22 for SSH, 8080 or 80 for app)</li><li>Enable access to Azure Container Registry from the VM</li></ul><h3>🔧 1. Create the Azure VM</h3><p>You can use the Azure Portal or Azure CLI to create the VM.</p><h4>Using Azure CLI:</h4><pre>az vm create \<br>  --name myJavaVM \<br>  --resource-group myResourceGroup \<br>  --image UbuntuLTS \<br>  --admin-username azureuser \<br>  --generate-ssh-keys</pre><ul><li>--image UbuntuLTS: Uses the latest stable Ubuntu image.</li><li>--generate-ssh-keys: Automatically creates and stores SSH keys on your local machine.</li></ul><h3>🌐 2. Open Required Ports on VM</h3><p>By default, only port 22 (SSH) is open. You’ll likely want to expose additional ports depending on your application (e.g., 8080 for Spring Boot apps).</p><h4>Example: Open port 8080</h4><pre>az vm open-port --port 8080 --resource-group myResourceGroup --name myJavaVM</pre><h3>🐳 3. Install Docker on the VM</h3><p>SSH into the VM:</p><pre>ssh azureuser@&lt;your-vm-public-ip&gt;</pre><p>Then run the following to install Docker:</p><pre>sudo apt update<br>sudo apt install -y docker.io<br>sudo systemctl start docker<br>sudo systemctl enable docker</pre><p>Give the current user permission to run Docker without sudo:</p><pre>sudo usermod -aG docker $USER<br>newgrp docker</pre><h3>🔑 4. Login to Azure Container Registry from VM</h3><p>To pull Docker images from ACR, log in from the VM using:</p><pre>az acr login --name &lt;your-acr-name&gt;</pre><p>Or generate credentials:</p><pre>az acr credential show --name &lt;your-acr-name&gt;</pre><p>Then use those credentials with:</p><pre>docker login &lt;your-acr-name&gt;.azurecr.io</pre><h3>✅ At This Point:</h3><p>Your Azure VM is:</p><ul><li>Running and reachable via SSH</li><li>Docker is installed and operational</li><li>The VM can access your private ACR and pull images</li><li>Necessary ports are open for external traffic</li></ul><h3>🚀 Step 4: Creating a CD Pipeline to Deploy on Azure VM</h3><p>In this step, we’ll create a <strong>Release pipeline (CD)</strong> in Azure DevOps that will <strong>connect to the Azure VM over SSH</strong>, pull the Docker image from ACR, and <strong>run the container</strong> on the VM.</p><h3>✅ What You’ll Achieve</h3><ul><li>Set up an <strong>Azure DevOps Release Pipeline</strong> (Classic or YAML)</li><li>Use <strong>SSH task</strong> to connect and deploy to your VM</li><li>Pull the image from ACR and run the container</li></ul><h3>📦 Prerequisites</h3><p>Before you proceed, ensure:</p><ul><li>The Java Docker image is successfully pushed to ACR</li><li>Azure VM is running and accessible over SSH</li><li>SSH public key is added to Azure DevOps service connection (for agentless deployment)</li></ul><h3>🛠️ Steps to Create the CD Pipeline</h3><h3>1. Go to Pipelines &gt; Releases in Azure DevOps</h3><p>Click <strong>“New pipeline”</strong>, then choose <strong>“Empty job”</strong>.</p><h3>2. Add an Artifact</h3><ul><li>Select your <strong>build pipeline</strong> as the source</li><li>This will fetch the Docker image details or artifacts from the previous build step</li></ul><h3>3. Add a Stage (e.g., “Deploy to VM”)</h3><p>Click on the stage and select <strong>“Agentless job”</strong> since we’ll use SSH directly.</p><h3>4. Add an SSH Deployment Task</h3><p>Search for <strong>“SSH”</strong> and select <strong>“SSH — Run Shell Script”</strong>.</p><h4>Fill in:</h4><ul><li><strong>SSH connection</strong>: Add or choose existing service connection (pointing to the VM)</li><li><strong>Script inline</strong>:</li></ul><pre>docker login &lt;your-acr-name&gt;.azurecr.io -u &lt;username&gt; -p &lt;password&gt;<br>docker pull &lt;your-acr-name&gt;.azurecr.io/my-java-app:latest<br>docker stop my-java-app || true<br>docker rm my-java-app || true<br>docker run -d --name my-java-app -p 8080:8080 &lt;your-acr-name&gt;.azurecr.io/my-java-app:latest</pre><p>Replace placeholders:</p><ul><li>&lt;your-acr-name&gt;: Your Azure Container Registry name</li><li>my-java-app: Your application/container name</li><li>&lt;username&gt; and &lt;password&gt;: ACR credentials from az acr credential show</li></ul><h3>5. Save and Create Release</h3><p>Click <strong>“Save”</strong>, then <strong>“Create release”</strong>, select your stage and click <strong>“Deploy”</strong>.</p><p>You can also <strong>automate this</strong> to trigger after each successful build (via Continuous Deployment trigger under the artifact settings).</p><h3>✅ What This CD Pipeline Does</h3><ul><li>🔐 <strong>Connects to Azure VM via SSH</strong> using the provided service connection.</li><li>🔑 <strong>Authenticates with Azure Container Registry (ACR)</strong> using ACR credentials.</li><li>📥 <strong>Pulls the latest Docker image</strong> from ACR.</li><li>🛑 <strong>Stops the currently running container</strong> (if any) to avoid port conflicts.</li><li>🧹 <strong>Removes the old container</strong> to clean up previous deployments.</li><li>🚀 <strong>Runs a new container</strong> from the pulled image with port binding (e.g., 8080:8080).</li></ul><h3>✅ Step 5: Verifying the Deployment on Azure VM</h3><p>Once your CD pipeline has run successfully and deployed the Dockerized Java application to the Azure VM, it’s time to verify that everything is working as expected.</p><h3>🔍 1. Check Azure DevOps Pipeline Logs</h3><ul><li>Go to your <strong>Azure DevOps project</strong> → <strong>Pipelines</strong> → Select your CD pipeline.</li><li>Open the latest run and check:</li><li>✅ SSH connection to VM was successful.</li><li>✅ Docker image pulled successfully.</li><li>✅ Old container removed and new one started.</li><li>✅ No errors in any script or Docker run commands</li></ul><h3>🌐 2. Access the Java Application in Browser</h3><p>If your application listens on port 8080, and you mapped it to port 8080 on the VM, then:</p><ul><li>Open your browser and visit:</li><li>http://&lt;VM_PUBLIC_IP&gt;:8080</li><li>Replace &lt;VM_PUBLIC_IP&gt; with your <strong>Azure VM’s public IP address</strong> (you can find it in the Azure Portal under the VM&#39;s &quot;Overview&quot; tab).</li><li>You should see your Java application running!</li></ul><h3>🧪 3. Optional: Validate API or App Functionality</h3><ul><li>If it’s a REST API: Use <strong>Postman</strong> or <strong>cURL</strong> to test the endpoints.</li><li>If it’s a web app: Click through pages, check login forms, etc.</li></ul><h3>🛡️ 4. Enable Network Security Group (NSG) Rules</h3><ul><li>Ensure the VM’s <strong>NSG allows incoming traffic</strong> on the port you exposed (e.g., 8080 or 80).</li><li>Go to <strong>Azure Portal</strong> → <strong>Networking</strong> → Add Inbound Port Rule:</li><li>Port: 8080</li><li>Protocol: TCP</li><li>Priority: &lt;Keep default or lower&gt;</li><li>Action: Allow</li></ul><h3>📓 5. Optional Logging and Monitoring</h3><ul><li>SSH into your VM:</li><li>ssh azureuser@&lt;VM_PUBLIC_IP&gt;</li><li>Check running Docker containers</li><li>docker ps</li><li>Check logs of the container:</li><li>docker logs &lt;container_id&gt;</li></ul><p>This helps debug if something goes wrong.</p><h3>✅ Step 6: Optional Enhancements for Production Readiness</h3><p>Once your Java application is deployed and running on an Azure VM, you can make several enhancements to ensure security, scalability, and maintainability. These steps are optional but highly recommended for real-world deployments.</p><h3>🔒 1. Enable HTTPS with SSL (TLS) using NGINX or Certbot</h3><ul><li><strong>Install NGINX</strong> as a reverse proxy:</li><li>sudo apt update &amp;&amp; sudo apt install nginx -y</li><li><strong>Configure NGINX</strong> to forward traffic to your Docker app:</li><li>server { listen 80; server_name yourdomain.com; location / { proxy_pass http://localhost:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }</li><li><strong>Install Certbot for SSL</strong></li><li>sudo apt install certbot python3-certbot-nginx -y sudo certbot --nginx</li><li>After successful SSL configuration, your app will be accessible securely at:</li><li><a href="https://yourdomain.com/">https://yourdomain.com</a></li></ul><h3>🌐 2. Point a Custom Domain to Azure VM</h3><ul><li>Buy a domain (from GoDaddy, Namecheap, etc.)</li><li>Go to your domain registrar’s <strong>DNS settings</strong> and add an <strong>A record</strong>:</li><li><strong>Name</strong>: @</li><li><strong>Type</strong>: A</li><li><strong>Value</strong>: &lt;VM_PUBLIC_IP&gt;</li><li>Optional: Add a www CNAME pointing to your root domain.</li></ul><p>Once DNS propagates, your app is accessible via:</p><pre>http://yourdomain.com</pre><h3>📈 3. Enable Monitoring &amp; Logging with Azure Monitor</h3><ul><li>Go to your VM → <strong>Monitoring</strong> → Enable <strong>Diagnostics Settings</strong>.</li><li>Use <strong>Log Analytics Workspace</strong> to:</li><li>View container logs</li><li>Monitor CPU/memory/network usage</li><li>Set alerts on high usage or failures</li></ul><h3>🛠️ 4. Automate with Infrastructure as Code (IaC)</h3><ul><li>Use <strong>Terraform</strong> or <strong>ARM Templates</strong> to:</li><li>Provision Azure VM</li><li>Create NSG, public IP, and other resources</li><li>Automate end-to-end deployment</li></ul><p>This ensures <strong>repeatable and version-controlled</strong> infrastructure.</p><h3>🔁 5. Enable Auto-Restart or Container Health Checks</h3><ul><li>Use Docker --restart always:</li><li>docker run -d --restart always ...</li><li>Or define a <strong>healthcheck</strong> in your Dockerfile:</li></ul><pre>HEALTHCHECK CMD curl --fail http://localhost:8080/health || exit 1</pre><h3>🧩 6. Container Orchestration (Optional for Scale)</h3><ul><li>If you plan to scale:</li><li>Use <strong>Azure Kubernetes Service (AKS)</strong> or <strong>Docker Swarm</strong>.</li><li>Avoid running production workloads manually on VMs if scaling becomes complex.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=388c3f541619" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Deploying a Java Application to AKS with CI/CD using Azure DevOps]]></title>
            <link>https://medium.com/@karthikmp9686/deploying-a-java-application-to-aks-with-ci-cd-using-azure-devops-f6df885268a0?source=rss-c1b51362229b------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6df885268a0</guid>
            <dc:creator><![CDATA[Karthikmp]]></dc:creator>
            <pubDate>Fri, 18 Jul 2025 13:31:53 GMT</pubDate>
            <atom:updated>2025-07-18T13:31:53.449Z</atom:updated>
            <content:encoded><![CDATA[<p>In today’s cloud-native landscape, Kubernetes has become the standard for orchestrating containerized applications. Azure Kubernetes Service (AKS) simplifies Kubernetes cluster management in the Azure cloud. In this blog, we will walk through how to deploy a <strong>Java application to AKS</strong> using a fully automated <strong>CI/CD pipeline built with Azure DevOps</strong>.</p><p>Our goal is to take a Java application, containerize it with Docker, push it to <strong>Azure Container Registry (ACR)</strong>, and deploy it to <strong>AKS</strong> through DevOps pipelines — all in a seamless, automated manner.</p><h3>What You Will Learn:</h3><ul><li>Containerizing a Java application using Docker</li><li>Creating and pushing Docker images to Azure Container Registry (ACR)</li><li>Provisioning an AKS cluster (manually or via Terraform if needed)</li><li>Building CI/CD pipelines in Azure DevOps to automate:</li><li>Java application build and Docker image creation</li><li>Image push to ACR</li><li>Deployment to AKS via YAML manifest files</li><li>Managing secrets with Azure Key Vault</li><li>Using Azure DevOps service connections to securely access Azure resources</li><li>Accessing the application deployed on AKS</li></ul><p>By the end of this guide, you will have a working DevOps pipeline that:</p><ul><li>Builds your Java app with Maven</li><li>Packages it into a Docker image</li><li>Pushes it to ACR</li><li>Deploys it to AKS automatically using kubectl apply</li><li>Retrieves connection strings or secrets from <strong>Azure Key Vault</strong> securely during deployment</li></ul><h3>Setting Up CI/CD Pipeline in Azure DevOps for Dockerized Java Application</h3><p>In this section, we’ll deep dive into <strong>how to set up a CI/CD pipeline in Azure DevOps</strong> to build, containerize, and deploy a Java application to Azure Kubernetes Service (AKS). Azure DevOps provides robust tools to automate the entire build and release lifecycle — from code commit to deployment — ensuring consistency, speed, and reliability in your delivery process.</p><h3>📁 Prerequisites</h3><p>Before setting up the pipeline, make sure you have the following:</p><ul><li>A Java application (preferably built with Maven)</li><li>A Dockerfile in the root of the project</li><li>An Azure DevOps project</li><li>A Git repository with your Java application code</li><li>Azure Kubernetes Service (AKS) cluster set up and running</li><li>Azure Container Registry (ACR) created</li><li>Service connections configured in Azure DevOps:</li><li><strong>Docker Registry Service Connection</strong> for ACR</li><li><strong>Azure Resource Manager Service Connection</strong> for AKS and Key Vault</li></ul><h3>Step 1: Prepare Your Java App and Dockerfile</h3><p>Let’s assume you have a simple Maven-based Java application. Create a Dockerfile in your root project directory.</p><p><strong>Example Dockerfile:</strong></p><pre># Use OpenJDK 17 as the base image<br>FROM openjdk:17-jdk-slim<br># Set the working directory<br>WORKDIR /app<br># Copy the built jar file into the container<br>COPY target/myapp.jar app.jar<br># Expose the application port<br>EXPOSE 8080<br># Run the application<br>ENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;, &quot;app.jar&quot;]</pre><h3>Step 2: Create a Build Pipeline (CI)</h3><ol><li>Go to your Azure DevOps project.</li><li>Navigate to <strong>Pipelines</strong> &gt; <strong>Create Pipeline</strong>.</li><li>Choose the repository where your code is stored (GitHub, Azure Repos, etc.).</li><li>Select <strong>YAML</strong> as the pipeline configuration method.</li><li>Replace the content with the following YAML (save it as azure-pipelines.yml in your repo):</li></ol><pre>trigger:<br>  branches:<br>    include:<br>      - main<br>variables:<br>  imageName: &#39;myapp&#39;<br>stages:<br>- stage: Build<br>  displayName: &#39;Build Stage&#39;<br>  jobs:<br>  - job: Build<br>    pool:<br>      vmImage: &#39;ubuntu-latest&#39;<br>    steps:<br>    - task: Maven@3<br>      inputs:<br>        mavenPomFile: &#39;pom.xml&#39;<br>        goals: &#39;package&#39;<br>        options: &#39;-DskipTests&#39;<br>    - task: Docker@2<br>      displayName: &#39;Build Docker Image&#39;<br>      inputs:<br>        containerRegistry: &#39;ACR-Service-Connection&#39;<br>        repository: &#39;$(imageName)&#39;<br>        command: &#39;buildAndPush&#39;<br>        Dockerfile: &#39;**/Dockerfile&#39;<br>        tags: |<br>          $(Build.BuildId)<br>    - publish: $(Pipeline.Workspace)<br>      artifact: drop</pre><h3>🧾 What This Pipeline Does:</h3><ul><li><strong>Triggers</strong> on every push to the main branch</li><li><strong>Builds</strong> the Java project using Maven</li><li><strong>Builds and pushes</strong> a Docker image to Azure Container Registry using the specified service connection</li></ul><h3>Step 3: Create a Release Pipeline (CD)</h3><p>Now that your image is being pushed to ACR, it’s time to deploy it to AKS.</p><ol><li>Go to <strong>Pipelines &gt; Releases</strong> in Azure DevOps.</li><li>Click <strong>New pipeline</strong>.</li><li>Add an <strong>artifact</strong> from your build pipeline (choose the one we just created).</li><li>Add a <strong>stage</strong>, name it something like “Deploy to AKS”.</li></ol><h3>Configure the Deployment Stage Tasks:</h3><p><strong>Task 1: Azure CLI or Kubectl Task</strong><br> Install and configure kubectl to communicate with your AKS cluster:</p><pre>- task: AzureCLI@2<br>  inputs:<br>    azureSubscription: &#39;Azure-Service-Connection&#39;<br>    scriptType: &#39;bash&#39;<br>    scriptLocation: &#39;inlineScript&#39;<br>    inlineScript: |<br>      az aks get-credentials --resource-group myResourceGroup --name myAKSCluster</pre><p><strong>Task 2: Kubernetes Deployment</strong><br> Apply your Kubernetes manifests (like deployment.yaml, service.yaml) to deploy the app:</p><pre>- task: Kubernetes@1<br>  inputs:<br>    connectionType: &#39;Azure Resource Manager&#39;<br>    azureSubscriptionEndpoint: &#39;Azure-Service-Connection&#39;<br>    azureResourceGroup: &#39;myResourceGroup&#39;<br>    kubernetesCluster: &#39;myAKSCluster&#39;<br>    command: apply<br>    useConfigurationFile: true<br>    configuration: &#39;manifests/deployment.yaml&#39;</pre><p>Make sure your deployment.yaml refers to the image built in the CI stage:</p><pre>containers:<br>  - name: myapp<br>    image: myacr.azurecr.io/myapp:$(Build.BuildId)</pre><h3>Step 4: Secure Access Using Azure Key Vault</h3><p>If your Java app requires secrets like DB credentials or storage connection strings:</p><ol><li>Store secrets in Azure Key Vault.</li><li>Grant access to Azure DevOps via the Azure Resource Manager service connection.</li><li>Add this task to retrieve secrets during the pipeline:</li></ol><pre>- task: AzureKeyVault@2<br>  inputs:<br>    azureSubscription: &#39;Azure-Service-Connection&#39;<br>    KeyVaultName: &#39;myKeyVault&#39;<br>    SecretsFilter: &#39;*&#39;<br>    RunAsPreJob: true</pre><p>These secrets will be available as environment variables that your Java app can use.</p><h3>Step 5: Add Approvals and Environment Gates (Optional)</h3><ol><li>Go to your <strong>Release Stage</strong>.</li><li>Click on the lightning ⚡ icon in the environment section.</li><li>Enable <strong>Pre-deployment approvals</strong>.</li><li>Assign required team members to review and approve before production deployments.</li></ol><h3>✅ Final Result</h3><p>You now have a full CI/CD pipeline that:</p><ul><li>Builds a Java app with Maven</li><li>Packages it into a Docker image</li><li>Pushes the image to Azure Container Registry</li><li>Deploys it to Azure Kubernetes Service</li><li>Optionally fetches secrets from Azure Key Vault</li><li>Supports manual approval for production deployments</li></ul><h3>🧠 Tips &amp; Best Practices</h3><ul><li>Always tag Docker images uniquely (e.g., $(Build.BuildId) or Git SHA).</li><li>Set up alerting and logging using Azure Monitor or Prometheus for AKS.</li><li>Use Helm instead of raw YAML for complex deployments.</li><li>Use Dev/Test/Prod stages with separate AKS namespaces or clusters for better separation.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6df885268a0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying a Java Application on Azure using Terraform and Azure DevOps]]></title>
            <link>https://medium.com/@karthikmp9686/deploying-a-java-application-on-azure-using-terraform-and-azure-devops-a4dc8faf2c4e?source=rss-c1b51362229b------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4dc8faf2c4e</guid>
            <dc:creator><![CDATA[Karthikmp]]></dc:creator>
            <pubDate>Fri, 18 Jul 2025 11:15:21 GMT</pubDate>
            <atom:updated>2025-07-18T11:15:21.667Z</atom:updated>
            <content:encoded><![CDATA[<p>In today’s cloud-native world, automating infrastructure and deployments is critical for building scalable, secure, and efficient applications. In this blog, I’ll walk you through how to <strong>deploy a Java application on Azure</strong> using <strong>Terraform for infrastructure provisioning</strong> and <strong>Azure DevOps for CI/CD pipelines</strong>.</p><p>We’ll follow a complete <strong>Infrastructure-as-Code (IaC)</strong> approach by defining all necessary Azure resources — such as <strong>App Service</strong>, <strong>Key Vault</strong>, and <strong>PostgreSQL Database</strong> — in Terraform .tf files. This not only ensures consistency but also simplifies infrastructure management across different environments.</p><p>Our Java application will be built using <strong>Maven</strong>, then <strong>containerized with Docker</strong> using the <strong>OpenJDK 17</strong> base image. The Docker image will be pushed to a container registry like <strong>Docker Hub</strong> or <strong>Azure Container Registry (ACR)</strong>.</p><p>To automate the deployment lifecycle, we’ll configure a <strong>CI/CD pipeline in Azure DevOps</strong>. This pipeline will:</p><ul><li>Build the Java app,</li><li>Package it into a Docker image,</li><li>Push the image to the registry,</li><li>And deploy it to <strong>Azure App Service for Containers</strong>.</li></ul><p>For secure resource access, we’ll provision an <strong>Azure Database for PostgreSQL</strong> using Terraform, store the <strong>connection string securely in Azure Key Vault</strong>, and fetch it dynamically in the pipeline using AzureKeyVault@2 task. The Java application reads this at runtime to connect to PostgreSQL using the <strong>Azure SDK or a JDBC driver</strong>.</p><p>Finally, we’ll add a <strong>manual approval gate</strong> before deploying to production by assigning a named environment (like production) in the YAML pipeline and configuring manual approval via Azure DevOps Environments. This ensures every production release is <strong>auditable and authorized</strong>.</p><p>Now, Let’s move to the <strong>Provisioning Azure Resources using Terraform</strong> section.</p><h3>🏗 Provisioning Azure Resources using Terraform</h3><p>To deploy our Java app on Azure, we first need to provision the following resources:</p><ol><li><strong>Azure App Service Plan</strong> — Defines the compute resources.</li><li><strong>Azure App Service for Containers</strong> — Hosts the Dockerized Java application.</li><li><strong>Azure Key Vault</strong> — Stores secrets like the PostgreSQL connection string.</li><li><strong>Azure Database for PostgreSQL</strong> — Our backend database.</li></ol><h3>✅ Prerequisites</h3><ul><li>Azure CLI installed and authenticated</li><li>Terraform installed (&gt;= v1.0.0)</li><li>Azure DevOps project created</li></ul><h3>📁 Project Structure</h3><p>Here’s the basic Terraform directory layout:</p><pre>infra/<br>├── main.tf<br>├── variables.tf<br>├── outputs.tf<br>├── provider.tf</pre><h3>🧩 provider.tf</h3><pre>provider &quot;azurerm&quot; {<br>  features {}<br>}</pre><h3>🧱 main.tf</h3><pre>resource &quot;azurerm_resource_group&quot; &quot;rg&quot; {<br>  name     = &quot;javaapp-rg&quot;<br>  location = &quot;East US&quot;<br>}</pre><pre>resource &quot;azurerm_app_service_plan&quot; &quot;appplan&quot; {<br>  name                = &quot;javaapp-plan&quot;<br>  location            = azurerm_resource_group.rg.location<br>  resource_group_name = azurerm_resource_group.rg.name<br>  sku {<br>    tier = &quot;Standard&quot;<br>    size = &quot;S1&quot;<br>  }<br>}</pre><pre>resource &quot;azurerm_app_service&quot; &quot;appservice&quot; {<br>  name                = &quot;javaapp-service&quot;<br>  location            = azurerm_resource_group.rg.location<br>  resource_group_name = azurerm_resource_group.rg.name<br>  app_service_plan_id = azurerm_app_service_plan.appplan.id</pre><pre>  site_config {<br>    linux_fx_version = &quot;DOCKER|&lt;your-registry&gt;/&lt;your-image&gt;:latest&quot;<br>  }</pre><pre>  app_settings = {<br>    &quot;POSTGRES_CONN&quot; = &quot;@Microsoft.KeyVault(SecretUri=${azurerm_key_vault_secret.pg_connection.secret_uri})&quot;<br>  }<br>}</pre><pre>resource &quot;azurerm_key_vault&quot; &quot;kv&quot; {<br>  name                = &quot;javaapp-kv&quot;<br>  location            = azurerm_resource_group.rg.location<br>  resource_group_name = azurerm_resource_group.rg.name<br>  tenant_id           = data.azurerm_client_config.current.tenant_id<br>  sku_name            = &quot;standard&quot;<br>  soft_delete_enabled = true<br>  purge_protection_enabled = true<br>}</pre><pre>resource &quot;azurerm_postgresql_flexible_server&quot; &quot;postgres&quot; {<br>  name                   = &quot;javaapppgsql&quot;<br>  location               = azurerm_resource_group.rg.location<br>  resource_group_name    = azurerm_resource_group.rg.name<br>  administrator_login    = &quot;pgadmin&quot;<br>  administrator_password = &quot;StrongPassword123!&quot;<br>  sku_name               = &quot;Standard_B1ms&quot;<br>  version                = &quot;13&quot;<br>  storage_mb             = 32768<br>  delegated_subnet_id    = null<br>}</pre><pre>resource &quot;azurerm_key_vault_secret&quot; &quot;pg_connection&quot; {<br>  name         = &quot;POSTGRES-CONN&quot;<br>  value        = &quot;jdbc:postgresql://${azurerm_postgresql_flexible_server.postgres.fqdn}:5432/postgres?user=pgadmin&amp;password=StrongPassword123!&quot;<br>  key_vault_id = azurerm_key_vault.kv.id<br>}</pre><h3>📌 variables.tf</h3><pre>variable &quot;location&quot; {<br>  default = &quot;East US&quot;<br>}</pre><h3>🚀 Deployment Steps</h3><ol><li><strong>Initialize Terraform</strong></li></ol><pre>terraform init</pre><p><strong>2. Plan Infrastructure</strong></p><pre>terraform plan -out=tfplan</pre><p><strong>3. Apply Infrastructure</strong></p><pre>terraform apply tfplan</pre><p>This sets up the base infrastructure on Azure. Next, we’ll move on to <strong>containerizing the Java app and pushing to Docker Hub/ACR</strong>.</p><p>Perfect! Let’s now proceed to the next step:</p><h3>🐳 Containerizing the Java Application and Pushing to Docker Hub / Azure Container Registry</h3><p>Before we proceed with containerization and deployment, it’s important to follow a structured approach to ensure our application is running as expected at every stage. Below is the recommended step-by-step flow for deploying a Java (Spring Boot) application using Docker and GitHub, and eventually integrating it with Azure DevOps.</p><h3>🔁 Step-by-Step Workflow:</h3><ol><li><strong>✅ Run the Java Application Locally</strong><br> The first step is to ensure that the application is running correctly on your local development machine. We can use Maven to start the Spring Boot application:</li></ol><pre>./mvnw spring-boot:run</pre><ol><li>Once it starts, open a browser and verify the application is accessible at http://localhost:8080. This confirms there are no major issues with your code, dependencies, or configuration.</li><li><strong>🐳 Create a Dockerfile for the Application</strong><br> After verifying the app works, we create a Dockerfile at the root of the project. This file tells Docker how to package our application into a container. Here&#39;s a simple example:</li></ol><pre>FROM openjdk:17-jdk-alpine <br>COPY target/myapp.jar app.jar <br>ENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;, &quot;/app.jar&quot;]</pre><ol><li>Make sure your .jar file is generated by running:</li></ol><pre>./mvnw clean package</pre><ol><li><strong>🔨 Build the Docker Image</strong><br> We now build a Docker image from our project using the Dockerfile:</li></ol><pre>docker build -t yourusername/javaapp:latest .</pre><ol><li>This image contains everything needed to run our Java application in a containerized environment.</li><li><strong>🚀 Run the App Inside a Docker Container</strong><br> Before deploying to cloud, we test the image locally by running it as a container:</li></ol><pre>docker run -p 8080:8080 yourusername/javaapp:latest</pre><ol><li>Navigate to http://localhost:8080 again to verify the app works as expected inside the Docker container. This step ensures there are no runtime issues before deploying to production.</li><li><strong>🔁 Version Control with Git</strong><br> Now that the application runs properly inside Docker, we initialize a Git repository in the project folder:</li></ol><pre>git init <br>git add . <br>git commit -m &quot;Initial commit with Dockerized Java app&quot;</pre><ol><li>This allows us to track changes, collaborate with teams, and integrate with CI/CD pipelines.</li><li><strong>📦 Push the Project to GitHub</strong><br> Next, we create a repository on GitHub (if not done already) and push our local project to it:</li><li><strong>🔗 Integrate with Azure Repos or Pipelines</strong><br> Once the project is on GitHub, we connect it with Azure DevOps. You can either:</li></ol><ul><li>Import the GitHub repository directly into Azure Repos, or</li><li>Use the GitHub service connection in Azure Pipelines to pull code directly.</li></ul><p>This prepares your project for a full CI/CD pipeline where every code push can automatically trigger a build, test, and deployment process.</p><h3>Setting Up CI/CD Pipeline in Azure DevOps for Dockerized Java App</h3><p>Now that our code is pushed to GitHub and integrated with Azure DevOps, it’s time to set up an Azure Pipeline that will automate building the Docker image and deploying the application.</p><h3>📄 1. Create the Azure Pipeline YAML file</h3><p>Create a file named azure-pipelines.yml in the root of your repository. Below is a sample pipeline configuration:</p><pre>trigger:<br>- main  # Pipeline runs whenever code is pushed to the main branch<br><br>pool:<br>  vmImage: ubuntu-latest  # Use latest Ubuntu VM<br>variables:<br>  buildId: $(Build.BuildId)  # Automatically set build ID for versioning Docker image<br># ----------------------------------------------<br># ✅ STEP 1: Fetch PostgreSQL connection string from Azure Key Vault<br># ----------------------------------------------<br>steps:<br>- task: AzureKeyVault@2<br>  displayName: &#39;Fetch PostgreSQL connection string from Azure Key Vault&#39;<br>  inputs:<br>    azureSubscription: &#39;&lt;azureServiceConnection&gt;&#39;  # Service connection to Azure<br>    KeyVaultName: &#39;&lt;keyvault&gt;&#39;                     # Azure Key Vault name<br>    SecretsFilter: &#39;postgres-connection-string&#39;    # Secret name that holds PostgreSQL URL<br>    RunAsPreJob: true<br># ----------------------------------------------<br># ✅ STEP 2: Inject connection string into application.properties<br># ----------------------------------------------<br>- task: Bash@3<br>  displayName: &#39;Inject DB URL into application.properties&#39;<br>  inputs:<br>    targetType: &#39;inline&#39;<br>    script: |<br>      echo &quot;Injecting spring.datasource.url with the secret value&quot;<br>      sed -i &quot;s|spring.datasource.url=.*|spring.datasource.url=${POSTGRES_CONNECTION_STRING}|&quot; src/main/resources/application.properties<br># ----------------------------------------------<br># ✅ STEP 3: Build the Java app using Maven (skip tests)<br># ----------------------------------------------<br>- task: Maven@3<br>  displayName: &#39;Maven Clean Install (Skip Tests)&#39;<br>  inputs:<br>    mavenPomFile: &#39;pom.xml&#39;<br>    goals: &#39;clean install -DskipTests&#39;<br>    javaHomeOption: &#39;JDKVersion&#39;<br>    jdkVersionOption: &#39;17&#39;<br># ----------------------------------------------<br># ✅ STEP 4: Build Docker image and push to ACR<br># ----------------------------------------------<br>- task: Docker@2<br>  displayName: &#39;Build and Push Docker Image to ACR&#39;<br>  inputs:<br>    containerRegistry: &#39;&lt;azureContainerRegistryConnection&gt;&#39;  # ACR service connection<br>    repository: &#39;java-app&#39;                                   # Docker repo name<br>    command: &#39;buildAndPush&#39;<br>    Dockerfile: &#39;Dockerfile&#39;<br>    tags: &#39;$(buildId)&#39;                                       # Use build ID as tag<br># ----------------------------------------------<br># ✅ STEP 5: Deploy container image to Azure Web App (Linux)<br># ----------------------------------------------<br>- task: AzureRmWebAppDeployment@4<br>  displayName: &#39;Deploy to Azure Web App for Containers&#39;<br>  inputs:<br>    ConnectionType: &#39;AzureRM&#39;<br>    azureSubscription: &#39;&lt;azureServiceConnection&gt;&#39;<br>    appType: &#39;webAppContainer&#39;<br>    WebAppName: &#39;&lt;appServiceName&gt;&#39;                # Name of your Web App<br>    DockerNamespace: &#39;&lt;acr-name&gt;.azurecr.io&#39;      # Your ACR namespace<br>    DockerRepository: &#39;java-app&#39;                  # Docker repo inside ACR<br>    DockerImageTag: &#39;$(buildId)&#39;                  # Deploy the newly built image</pre><h3>🔧 What this pipeline does:</h3><ul><li><strong>Triggers on every push to the </strong><strong>main branch</strong>:<br> Ensures continuous integration by automatically starting the pipeline whenever a new commit is pushed to the main branch, allowing you to catch issues early and deploy updates faster.</li><li><strong>Builds the Java application using Maven</strong>:<br> Uses the Azure DevOps Maven@3 task to compile the source code, run tests, and package the application into a JAR file. This ensures that your Java app is correctly built and tested before deployment.</li><li><strong>Builds a Docker image with your Java application</strong>:<br> Uses the Dockerfile present in your project to create a container image that includes the JAR file. The Dockerfile is based on the OpenJDK 17 image, ensuring compatibility with your Java app.</li><li><strong>Pushes the Docker image to Azure Container Registry (ACR)</strong>:<br> Authenticates with your ACR and pushes the newly built Docker image with a specific tag (e.g., version or commit SHA). This makes the image available for deployment in the next stages.</li><li><strong>Fetches secrets securely from Azure Key Vault</strong>:<br> Uses the AzureKeyVault@2 task to retrieve sensitive information like the Azure PostgreSQL connection string, which is then passed as an environment variable to the app during deployment.</li><li><strong>Deploys the Docker image to Azure App Service for Containers</strong>:<br> Automatically deploys the image from ACR to your Azure Web App configured to run containerized workloads. This stage brings the latest code changes to a running environment.</li></ul><h3>🔐 Setting Up a Service Connection in Azure DevOps</h3><p>To allow your Azure DevOps pipeline to interact securely with Azure services — such as deploying to Azure App Service or pushing images to Azure Container Registry — you need to configure a <strong>Service Connection</strong>.</p><h4>✅ What is a Service Connection?</h4><p>A <strong>Service Connection</strong> in Azure DevOps is a secure credential that lets your pipeline access Azure resources without embedding credentials like passwords or secrets directly in your code. It uses Azure Active Directory (Azure AD) authentication to grant access.</p><p>Behind the scenes, Azure DevOps creates a <strong>Service Principal</strong> in your Azure AD tenant and assigns it proper roles (like Contributor) to manage resources within a specific subscription or resource group.</p><h4>🧰 How to Create a Service Connection</h4><p><strong>1.Open your Azure DevOps project</strong></p><ul><li>Go to your project in Azure DevOps.</li></ul><p><strong>2. Navigate to Project Settings</strong></p><ul><li>Scroll down in the left sidebar and click <strong>“Project settings”</strong> (usually at the bottom).</li></ul><p><strong>3. Select Service Connections</strong></p><ul><li>Under the “Pipelines” section, click on <strong>“Service connections”</strong>.</li></ul><p><strong>4. Click ‘New service connection’</strong></p><ul><li>Choose <strong>Azure Resource Manager</strong> (this is the most common choice for Azure services).</li></ul><p><strong>5. Choose the Authentication Method</strong></p><ul><li>Select <strong>Service principal (automatic)</strong> — Azure DevOps will create a service principal for you.</li><li>Choose your Azure subscription.</li><li>Select either the whole subscription or a specific resource group.</li><li>Name your service connection (e.g., azure-sp-connection).</li></ul><p><strong>6. Verify and Save</strong></p><ul><li>Click <strong>Verify and save</strong>. Azure DevOps will test the connection and store it securely.</li></ul><h4>🔄 How It Works in the Pipeline</h4><p>Once created, you use this service connection in your pipeline YAML or classic release pipeline. Here’s how it works:</p><ul><li><strong>Example usage in YAML:</strong></li></ul><pre>- task: AzureCLI@2   <br>inputs:     <br>azureSubscription: &#39;azure-sp-connection&#39;     <br>scriptType: &#39;bash&#39;     <br>scriptLocation: &#39;inlineScript&#39;     <br>inlineScript: |<br>       az webapp list</pre><ul><li>When the pipeline runs, Azure DevOps uses the <strong>service principal</strong> (created during service connection setup) to authenticate and execute commands in Azure.</li></ul><h4>🔒 Benefits</h4><ul><li><strong>Security</strong>: Secrets are stored securely and never exposed in plain text.</li><li><strong>Automation</strong>: No manual login needed; pipelines can run automatically.</li><li><strong>Role-based access</strong>: You can assign the service principal limited permissions using RBAC, like giving access to only specific resource groups.</li></ul><h3>🌐 Accessing Your Application in Azure App Service via Azure Portal</h3><p>Once your application is deployed successfully to <strong>Azure App Service</strong>, you can access and verify it directly from the Azure Portal. Here’s how to do it:</p><h4>🔹 Step 1: Go to Azure Portal</h4><p>Open your web browser and navigate to the Azure Portal:<br> 👉 <a href="https://portal.azure.com/">https://portal.azure.com</a></p><h4>🔹 Step 2: Navigate to App Services</h4><p>In the left sidebar (or search bar), type <strong>“App Services”</strong> and click on it.</p><p>This will list all the App Service instances running in your account.</p><h4>🔹 Step 3: Select Your App Service</h4><p>Click on the App Service where your application is deployed. It should have the name you specified in your Terraform or Azure pipeline setup.</p><h4>🔹 Step 4: Locate the Application URL</h4><p>Once you’re inside the App Service overview page, you’ll find a section called:</p><ul><li><strong>“Default domain”</strong><br> or</li><li><strong>“URL”</strong> under the <strong>Essentials</strong> section.</li></ul><p>This URL will look something like this:</p><pre>https://your-app-name.azurewebsites.net</pre><p>Click on this link — or open it in a new tab.</p><h4>🔹 Step 5: View Your Application</h4><p>You should now see your running web application live!</p><p>If you’ve deployed something like a React app, Flask API, or Node.js app, it should render or respond accordingly.</p><h4>🧠 Pro Tip:</h4><p>If your application doesn’t load, check these:</p><ul><li>Deployment logs in Azure DevOps.</li><li>App Service <strong>Logs</strong> (under <em>Monitoring &gt; Log stream</em>).</li><li>Ensure required ports and environment variables are set properly.</li></ul><h3>🛡️Connecting to Azure Database for PostgreSQL Securely Using Azure Key Vault</h3><p>Once your <strong>Azure Database for PostgreSQL</strong> instance is provisioned, the next critical step is securely storing and retrieving your database credentials — especially the <strong>connection string</strong>. We’ll use <strong>Azure Key Vault</strong> to manage secrets and fetch them into our app service environment safely.</p><h3>🔹 Step 1: Store the Connection String in Azure Key Vault</h3><ol><li>Go to <strong>Azure Portal</strong> &gt; search for <strong>Key Vaults</strong>.</li><li>Select the Key Vault you’ve already created (or create a new one).</li><li>In the left menu, go to <strong>Secrets</strong> &gt; <strong>Generate/Import</strong>.</li><li>Fill in:</li></ol><ul><li><strong>Name</strong>: e.g., PostgresConnectionString</li><li><strong>Value</strong>: The PostgreSQL connection string (e.g.)</li></ul><pre>Server=myserver.postgres.database.azure.com;Database=mydb;Port=5432;User Id=myuser@myserver;Password=YourPassword;Ssl Mode=Require;</pre><ul><li>Click <strong>Create</strong>.</li></ul><h3>🔹 Step 2: Give App Service Access to the Key Vault</h3><ol><li>Go to your <strong>Key Vault</strong> &gt; <strong>Access Configuration</strong> &gt; set to Vault access policy.</li><li>Click <strong>Access policies</strong> &gt; <strong>Add Access Policy</strong>.</li><li>Choose permissions:</li></ol><ul><li><strong>Secret permissions</strong>: Get, List</li></ul><ol><li>Select principal:</li></ol><ul><li>Choose your <strong>App Service’s managed identity</strong> (must be enabled on App Service &gt; Identity &gt; System-assigned).</li></ul><ol><li>Click <strong>Add</strong>, then <strong>Save</strong>.</li></ol><h3>Injecting PostgreSQL Connection String into application.properties via Azure DevOps Pipeline</h3><p>In many enterprise-grade Spring Boot or Java-based applications, connection strings are defined in application.properties (or application.yml) like:</p><pre>spring.datasource.url=jdbc:postgresql://&lt;host&gt;:5432/&lt;db-name&gt;<br>spring.datasource.username=&lt;user&gt;<br>spring.datasource.password=&lt;password&gt;</pre><p>⚠️ But hardcoding these credentials is a <strong>security risk</strong>. Instead, we’ll fetch secrets from <strong>Azure Key Vault in the pipeline</strong> and inject them dynamically.</p><h3>🔹 Step 1: Store Connection Details in Azure Key Vault</h3><p>Store the following secrets:</p><ul><li>postgresql-url</li><li>postgresql-username</li><li>postgresql-password</li></ul><p>You can use this naming convention or customize as needed.</p><h3>🔹 Step 2: Configure Key Vault in Azure DevOps Pipeline</h3><p>Here’s an example of a YAML pipeline that:</p><p>✅ Links Azure Key Vault<br> ✅ Retrieves the secrets<br> ✅ Injects them into application.properties during build</p><pre># ✅ Fetch secret from Azure Key Vault<br>- task: AzureKeyVault@2<br>  displayName: &#39;Fetch DB URL from Azure Key Vault&#39;<br>  inputs:<br>    azureSubscription: &#39;bankappconn&#39;<br>    KeyVaultName: &#39;java-vault&#39;<br>    SecretsFilter: &#39;bankdb10&#39;<br>    RunAsPreJob: true<br><br># ✅ Replace spring.datasource.url in application.properties with secret value<br>- task: Bash@3<br>  displayName: &#39;Inject spring.datasource.url into application.properties&#39;<br>  inputs:<br>    targetType: &#39;inline&#39;<br>    script: |<br>      echo &quot;Replacing spring.datasource.url in application.properties&quot;<br>      sed -i &quot;s|spring.datasource.url=.*|spring.datasource.url=${BANKDB10}|&quot; src/main/resources/application.properties</pre><h3>🔍 What This Does:</h3><ul><li>AzureKeyVault@2: Authenticates to Azure Key Vault using a service connection and pulls the secrets.</li><li>Bash: Writes those secrets to the application.properties file dynamically at build time.</li><li>Avoids storing credentials in source code or SCM</li><li>Ensures secrets are rotated easily from Key Vault without touching code.</li></ul><h3>🔐 Bonus Security Tips</h3><ul><li>Use <strong>RBAC</strong> or <strong>Access Policies</strong> to limit access to the Key Vault.</li><li>Set up secret rotation policies.</li><li>Keep application.properties in .gitignore if you&#39;re creating it dynamically.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4dc8faf2c4e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>