How to create and deploy Azure VM using Java Spring Boot and Azure SDK?

Amey Ambulgekar 0 Reputation points
2024-04-14T13:53:27.77+00:00

Hi,

Is there any way we can create Azure VM or any resources using Java Spring Boot Micro-service and AzureSDK ? Any sample code snippets available?

Is there any official MS documentation available?

Thanks

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,157 questions
Azure Spring Apps
Azure Spring Apps
An Azure platform as a service for running Spring Boot applications at cloud scale. Previously known as Azure Spring Cloud.
109 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anveshreddy Nimmala 2,465 Reputation points Microsoft Vendor
    2024-04-15T04:40:44.5266667+00:00

    Hello Amey Ambulgekar,

    Welcome to microsoft Q&A, Thankyou for posting your query here.

    you can create Azure VMs and other resources using Java Spring Boot microservices and the Azure SDK.

    Include Azure SDK Dependencies

    Configure Azure Credentials

    Create a VM in Azure

    Integrate with Spring Boot

    <dependencies>
        <!-- Azure Management Libraries -->
        <dependency>
            <groupId>com.azure.resourcemanager</groupId>
            <artifactId>azure-resourcemanager-compute</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.azure.resourcemanager</groupId>
            <artifactId>azure-resourcemanager-network</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.azure.resourcemanager</groupId>
            <artifactId>azure-resourcemanager-resources</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>
    
    import com.azure.identity.DefaultAzureCredentialBuilder;
    import com.azure.resourcemanager.AzureResourceManager;
    import com.azure.core.management.profile.AzureProfile;
    import com.azure.core.management.Region;
    import com.azure.resourcemanager.AzureResourceManager;
    import com.azure.resourcemanager.compute.models.KnownLinuxVirtualMachineImage;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    public class AzureConfiguration {
        public AzureResourceManager authenticateAzure() {
            AzureProfile profile = new AzureProfile(AzureProfile.Environment.AZURE);
            return AzureResourceManager.configure()
                .authenticate(new DefaultAzureCredentialBuilder().build(), profile)
                .withDefaultSubscription();
        }
    }
    public class AzureVmService {
        private final AzureResourceManager azureResourceManager;
        public AzureVmService(AzureResourceManager azureResourceManager) {
            this.azureResourceManager = azureResourceManager;
        }
        public void createVm() {
            azureResourceManager.virtualMachines().define("myVM")
                .withRegion(Region.US_EAST)
                .withNewResourceGroup("myResourceGroup")
                .withNewPrimaryNetwork("10.0.0.0/28")
                .withPrimaryPrivateIPAddressDynamic()
                .withoutPrimaryPublicIPAddress()
                .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_18_04_LTS)
                .withRootUsername("adminuser")
                .withSsh("ssh-rsa AAAAB3Nza...")
                .withSize("Standard_D2as_v4")
                .create();
        }
    }
    public class AzureVmController {
        private final AzureVmService azureVmService;
        public AzureVmController(AzureVmService azureVmService) {
            this.azureVmService = azureVmService;
        }
        @GetMapping("/create-vm")
        public String createVm() {
            azureVmService.createVm();
            return "VM creation started!";
        }
    }
    

    Hope this helps you

    If an answer has been helpful, please consider accepting the answer to help increase visibility of this question for other members of the Microsoft Q&A community. If not, please let us know what is still needed in the comments so the question can be answered. Thank you for helping to improve Microsoft Q&A!

    7446a2b3-8937-494a-ad3c-0a8dad27574f

     

    0 comments No comments