-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding Azure Container Instances module (#593)
* adding Azure Container Instances module * Update Azure ACI example * Update ACI example readme * Update Containers module to support ACI * remove unused aci.go helpers * Add ACI helpers unit tests * Update ACI sample test Co-authored-by: Hadwa Abdelhalem <[email protected]>
- Loading branch information
1 parent
c0bf11e
commit 850b50f
Showing
8 changed files
with
309 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Terraform Azure Example | ||
|
||
This folder contains a Terraform module that deploys resources in [Azure](https://azure.microsoft.com/) to demonstrate | ||
how you can use Terratest to write automated tests for your Azure Terraform code. This module deploys an [Azure Container Instance](https://azure.microsoft.com/en-us/services/container-instances/). | ||
|
||
Check out [test/azure/terraform_azure_aci_example_test.go](/test/azure/terraform_azure_aci_example_test.go) to see how you can write | ||
automated tests for this module. | ||
|
||
**WARNING**: This module and the automated tests for it deploy real resources into your Azure account which can cost you | ||
money. The resources are all part of the [Azure Free Account](https://azure.microsoft.com/en-us/free/), so if you haven't used that up, | ||
it should be free, but you are completely responsible for all Azure charges. | ||
|
||
## Running this module manually | ||
|
||
1. Sign up for [Azure](https://azure.microsoft.com/) | ||
1. Configure your Azure credentials using one of the [supported methods for Azure CLI | ||
tools](https://docs.microsoft.com/cli/azure/azure-cli-configuration?view=azure-cli-latest) | ||
1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH` | ||
1. Ensure [environment variables](../README.md#review-environment-variables) are available | ||
1. Run `terraform init` | ||
1. Run `terraform apply` | ||
1. When you're done, run `terraform destroy` | ||
|
||
## Running automated tests against this module | ||
|
||
1. Sign up for [Azure](https://azure.microsoft.com/) | ||
1. Configure your Azure credentials using one of the [supported methods for Azure CLI | ||
tools](https://docs.microsoft.com/en-us/cli/azure/azure-cli-configuration?view=azure-cli-latest) | ||
1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH` | ||
1. Configure your Terratest [Go test environment](../README.md) | ||
1. `cd test/azure` | ||
1. `go build terraform_azure_aci_example_test.go` | ||
1. `go test -v -timeout 60m -tags azure -run TestTerraformAzureACIExample` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
# DEPLOY AN AZURE CONTAINER Instance | ||
# This is an example of how to deploy an Azure Container Instance | ||
# See test/terraform_azure_aci_example_test.go for how to write automated tests for this code. | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
# ------------------------------------------------------------------------------ | ||
# CONFIGURE OUR AZURE CONNECTION | ||
# ------------------------------------------------------------------------------ | ||
|
||
provider "azurerm" { | ||
version = "~>2.29.0" | ||
features {} | ||
} | ||
|
||
# --------------------------------------------------------------------------------------------------------------------- | ||
# DEPLOY A RESOURCE GROUP | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
resource "azurerm_resource_group" "rg" { | ||
name = "terratest-aci-rg-${var.postfix}" | ||
location = var.location | ||
} | ||
|
||
# --------------------------------------------------------------------------------------------------------------------- | ||
# DEPLOY AN AZURE CONTAINER INSTANCE | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
resource "azurerm_container_group" "aci" { | ||
name = "aci${var.postfix}" | ||
location = azurerm_resource_group.rg.location | ||
resource_group_name = azurerm_resource_group.rg.name | ||
|
||
ip_address_type = "public" | ||
dns_name_label = "aci${var.postfix}" | ||
os_type = "Linux" | ||
|
||
container { | ||
name = "hello-world" | ||
image = "microsoft/aci-helloworld:latest" | ||
cpu = "0.5" | ||
memory = "1.5" | ||
|
||
ports { | ||
port = 443 | ||
protocol = "TCP" | ||
} | ||
} | ||
|
||
tags = { | ||
Environment = "Development" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
output "resource_group_name" { | ||
value = azurerm_resource_group.rg.name | ||
} | ||
|
||
output "ip_address" { | ||
value = azurerm_container_group.aci.ip_address | ||
} | ||
|
||
output "fqdn" { | ||
value = azurerm_container_group.aci.fqdn | ||
} | ||
|
||
output "container_instance_name" { | ||
value = azurerm_container_group.aci.name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
# ENVIRONMENT VARIABLES | ||
# Define these secrets as environment variables | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
# ARM_CLIENT_ID | ||
# ARM_CLIENT_SECRET | ||
# ARM_SUBSCRIPTION_ID | ||
# ARM_TENANT_ID | ||
|
||
# --------------------------------------------------------------------------------------------------------------------- | ||
# REQUIRED PARAMETERS | ||
# You must provide a value for each of these parameters. | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
# --------------------------------------------------------------------------------------------------------------------- | ||
# OPTIONAL PARAMETERS | ||
# These parameters have reasonable defaults. | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
variable "location" { | ||
description = "The supported azure location where the resource exists" | ||
type = string | ||
default = "West US2" | ||
} | ||
|
||
variable "postfix" { | ||
description = "A postfix string to centrally mitigate resource name collisions." | ||
type = string | ||
default = "1276" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// +build azure | ||
|
||
// NOTE: We use build tags to differentiate azure testing because we currently do not have azure access setup for | ||
// CircleCI. | ||
|
||
package test | ||
|
||
import ( | ||
"strings" | ||
|
||
"testing" | ||
|
||
"github.com/gruntwork-io/terratest/modules/azure" | ||
"github.com/gruntwork-io/terratest/modules/random" | ||
"github.com/gruntwork-io/terratest/modules/terraform" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTerraformAzureACIExample(t *testing.T) { | ||
t.Parallel() | ||
|
||
uniquePostfix := strings.ToLower(random.UniqueId()) | ||
|
||
// website::tag::1:: Configure Terraform setting up a path to Terraform code. | ||
terraformOptions := &terraform.Options{ | ||
TerraformDir: "../../examples/azure/terraform-azure-aci-example", | ||
Vars: map[string]interface{}{ | ||
"postfix": uniquePostfix, | ||
}, | ||
} | ||
|
||
// website::tag::5:: At the end of the test, run `terraform destroy` to clean up any resources that were created | ||
defer terraform.Destroy(t, terraformOptions) | ||
|
||
// website::tag::2:: Run `terraform init` and `terraform apply`. Fail the test if there are any errors. | ||
terraform.InitAndApply(t, terraformOptions) | ||
|
||
// website::tag::3:: Run `terraform output` to get the values of output variables | ||
resourceGroupName := terraform.Output(t, terraformOptions, "resource_group_name") | ||
aciName := terraform.Output(t, terraformOptions, "container_instance_name") | ||
ipAddress := terraform.Output(t, terraformOptions, "ip_address") | ||
fqdn := terraform.Output(t, terraformOptions, "fqdn") | ||
|
||
// website::tag::4:: Assert | ||
assert.True(t, azure.ContainerInstanceExists(t, aciName, resourceGroupName, "")) | ||
|
||
actualInstance := azure.GetContainerInstance(t, aciName, resourceGroupName, "") | ||
|
||
assert.Equal(t, ipAddress, *actualInstance.ContainerGroupProperties.IPAddress.IP) | ||
assert.Equal(t, fqdn, *actualInstance.ContainerGroupProperties.IPAddress.Fqdn) | ||
} |