Getting Started with Azure SDK for Go
Microsoft Azure provides vast services from basic computing to AI/ML. Azure Resource Manager (ARM) is our entry point to access and manage these services and provides a consistent management layer (APIs) that enables you to work with resources in Azure.
Source: Azure Resource Manager overview - Azure Resource Manager | Microsoft Learn
Any resource management request lands at the ARM, get authenticated, and enacted. As the diagram above indicates, there are many ways to interact with ARM. You can choose one or more ways to create and manage your Azure resources based on your preference. Azure Portal provides a click-to-manage experience which is good for managing fewer resources. You can use REST API to access the ARM management API layer directly and perform the CRUD operations against Azure resources. Azure PowerShell and Azure CLI allow you to automate the management tasks at the command-line. These are my preferred methods for working with Azure. These two methods use Azure SDKs to interact with the ARM APIs. Microsoft provides SDKs for different programming languages.
I will write about Azure SDK for Go and my experience working with it in this series of articles. Microsoft has excellent documentation to get you started. This series of articles is about my notes and experiments as I learn to use Azure SDK for Go.
Several packages are available as a part of the Azure SDK for Go. These packages are classified into two categories – management plane and data plane. The management plane packages are used to manage Azure resources. The data plane packages are used to interact with Azure resources. For example, the armresources package can be used to create resources and is a management plane package. Whereas the azcontainerregistry package, a data plane package, can be used to manage images in an Azure Container Registry. The minimum Go version you need for using these packages is 1.18.
All code samples presented in this series will be available at rchaganti/azure-go: Learning Azure SDK for Go (github.com)
The Development Container
To help you start with Azure SDK for Go, I suggest using a development environment that can be easily built or rebuilt. I will use a VS code dev container for all examples in this series of articles. Here is the devcontainer.json I am using.
|
|
This dev container gives you the Go development environment along with Azure CLI. You will also get the Go and Azure account VS Code extensions.
All code samples from this series of articles will be available at https://github.com/rchaganti/azure-go. You can clone the repository and start playing with the code samples.
The First Step
Once the development environment is ready, you can create a project to implement the management or the data plane operations using the Azure SDK for Go.
|
|
The above set of commands creates a directory and initializes a Go module called azstart
. Once this folder structure is ready, you can download your project’s necessary Azure Go packages. This can be done using the go get
command. For this quick start, you’ll need the azidentity
and armresources
packages.
|
|
You must use
go get
and not just do ago mod tidy
. There may be intellisense issues within VS Code otherwise.
Within main.go
program; you can place the following code. This program lists all resource groups in an Azure subscription. Don’t bother about all the details for now. In the subsequent parts of this series, you will learn about different authentication methods and using different packages from the Azure SDK for Go.
|
|
The NewAzureCLICredential
method in the azidentity
package enables you to access locally available Azure credentials within your Go program. You will learn more about this in the next article.
If you are using the VS Code dev environment provided at the beginning of this article, you will have Azure CLI within the environment. Before you run this program, you must use az login
to authenticate with the Azure cloud.
|
|
If Azure authentication is successful, this program must return a list of all resource groups in JSON format. Within the dev container, you can use jq
to filter the output.
This is just a quick start to get you started with the Azure SDK for Go. The next article will teach you about different authentication methods for your Go programs.
Share on: