Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Terraform_Azure_SelfHosted

Greetings, this repository showcases how to setup a Ubuntu 22.04 Virtual Machine in Microsoft Azure using Terraform.

NOTE: This Infrastucture as Code file (IaC) simply showcases how to go about creating the Virtual Machine in Azure using Terraform. You will be required to have the following: Resource Group, Virtual Network and Subnet. The reason for this is that we are simply testing the deployment of the machine. I will add the code showing how you can pre-create those resources at a later-point, thus you don't lose time with having to constantly tear-down your Virtual Network, Subnet, etc

With this project we will accomplish the following:

  • Deploying Ubuntu 22.04 to Microsoft Azure using Terraform
  • Automating the process of deploying the Self-Hosted Agent for Azure DevOps during the creation of our Ubuntu 22.04 server

One of my recent goals was to streamline the process of having the Self-Hosted Agent for Azure DevOps available the moment the Linux machine was deployed since I wanted to do more extensive testing with Azure Pipelines. This process will save us all a lot of time without having to deal with the self-host agent setup. I've also included the service file so you will be able to see your agent up and running following the completion of the Terraform execution.

Below I will break-down what you will need to do prior to the Terraform execution so you will be ready to go.

Part1: Creating Key-Pair for SSH Session

The first objective is for you to proceed with creating your own key-pair set to be able to SSH into the newly deployed server.

Apply the following:

  • change into the "keys" directory of this project. (Ex: cd keys)

  • Run this command .\create_ssh_keys.ps1 NAME_OF_KEYPAIR_SET (Ex: create_ssh_keys.ps1 TestKey)

Once you have your key-pair set created, you can proceed to the next step.

Part2: Acquiring a Personal Access Token via Azure DevOps

Within the "tmp" folder there is a "vm_execution.sh" script. This script performs a few operations:

  • It downloads and unpacks the associated files for the Azure Agent tool.
  • Runs the unattended session to configure the self-hosted Azure Agent
  • Installs Docker onto the machine (This component was added for docker testing, you can remove that portion if you like)
  • Following Docker installation it proceeds with copying over the "azureagent.service" file and then enables and starts the service.

Below is the only thing you will need to edit within the Bash Script file:

# Executing the Unattended session to setup self hosted Azure Agent
./config.sh --unattended \
--accceptteeula \
--pool "AGENT_POOL_NAME" \
--agent "AGENT_NAME" \
--url "AZURE_DEVOPS_URL" \
--auth PAT \
--token "PERSONAL_ACCESS_TOKEN" \
--work _work --projectname 'PROJECT_NAME'

You will add the information within that section of the "vm_execution.sh". You will need to generate your personal access token via your Azure DevOps Org and create a new agent pool. You can omit the "--pool" switch in which case it will just use the "Default" pool available within your Org. The agent_name can be whatever you like since that will be newly created upon the execution of the script. Once you fill out that section, proceed with moving on to the next step.

Part3: Editing the variables.tf file for the Terraform Project

The last phase prior to executing the Terraform project files will be editing the "variables.tf" file.

Let's start with this section of the file:

variable az_get_data {
    type = map(any)
    default = {
        rg_name = "EXISTING_RESOURCE_GROUP_NAME"
        vnetwork_name = "EXISTING_VIRTUAL_NETWORK_NAME"
        subnet_name = "EXISTING_SUBNET_NAME"
    }
}

variable public_ip_name {
    type = string
    default = "NAME_OF_PUBLIC_IP_ADDRESS"
}

variable "nic_name" {
   type = string
   default = "NIC_NAME"
}

For the variables "public_ip_name" and "nic_name" the default values can be whatever name you like.
However for the variable "az_get_data" the values will have to already exist within your Azure Environment.

variable az_vm_info {
    type = map(any)
    default = {
        az_vm_name = "ENTER_NAME_OF_SERVER"
        az_vm_size = "Standard_D2s_v3"
        az_vm_admin = "ubuntu" # Defaulted to ubuntu for the azureagent.service file
        az_os_disk_name = "ENTER_NAME_FOR_DISK"
        az_os_disk_cache = "ReadWrite"
        az_os_disk_storage = "Standard_LRS"
        az_image_publisher = "Canonical"
        az_image_offer = "0001-com-ubuntu-server-jammy"
        az_image_sku = "22_04-lts-gen2"
        az_image_ver = "latest"
        az_vm_conn = "ssh"
    }
}

For the variable "az_vm_info" you will just modify the "az_vm_name" and "az_os_disk_name".

NOTE: Please be advised, the az_vm_admin value is defaulted to "ubuntu". You're more than welcome to change that, however you must remember to edit the service file azureagent to match the same username otherwise the operation applied on the service file will fail!

Azure Agent Service file details:

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/myagent/
ExecStart=/bin/bash -c 'cd /home/ubuntu/myagent/ && ./run.sh'
 
[Install]
WantedBy=multi-user.target

Lastly, you will just have to add your full paths for the following:

  • key_pair_name
  • azureagent.service
  • vm_execution.sh

I've added examples within the variables.tf on showing how the paths would appears via Windows machine.

Once you finish applying your updates, you are now ready to begin using the Terraform project.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages