Using the Megaport Terraform Provider
The Megaport Terraform provider lets you create, manage, and update Megaport services through the Megaport API using Terraform tools.
The Megaport Terraform provider offers multicloud hybrid environments supported by the Megaport software-defined network (SDN). Using the Megaport Terraform provider, you can create and manage Ports, Virtual Cross Connects (VXCs), Megaport Cloud Routers (MCRs), Megaport Virtual Edges (MVEs), and partner VXCs.
Note
The Megaport Terraform provider is intended to be used with the Megaport API. It constitutes part of the official paid product and is eligible for support through customer channels. For more information, see Contacting Support.
Important
Usage of the Megaport Terraform provider constitutes your acceptance of the terms in the Megaport Acceptable Use Policy and Global Services Agreement.
Getting started
Before you start, we recommend that you read through the information in the Megaport Terraform Provider Registry.
It includes information such as links for getting started with Terraform, a link to the Megaport Terraform provider source code in GitHub, Megaport Terraform provider documentation including how to get started and various guides, resources and data sources, how to download and install the Megaport Terraform provider, and how to report an issue in GitHub.
This section includes the basic steps required to get working with the Megaport Terraform provider:
- Install Terraform
- Download and install the provider
- Configure the provider
- Start deploying services
Install Terraform
This first step is to install the Terraform CLI.
If you have not already installed Terraform, see the Terraform documentation for instructions on installing Terraform and verifying the installation.
Download and install the provider
To install the Megaport Terraform provider, copy and paste this code into your Terraform configuration (main.tf). Then run the terraform init
command.
terraform {
required_providers {
megaport = {
source = "megaport/megaport"
version = "1.1.8"
}
}
}
provider "megaport" {
# Configuration options
}
Configure the provider
There are some important fields and information you need to define to configure the Megaport Terraform provider.
-
Environment – This defines the environment in which you will be deploying services using Terraform. For example, if this is set to
staging
, then the services will be created in the https://portal-staging.megaport.com/ environment. When you have finished testing in the Staging environment and want to start deploying services in the Production environment, set this value toproduction
. For more information, see Environments. -
API key - You need to create an API key before you can use the Megaport Terraform provider. The API key consists of the
access_key
andsecret_key
values. The API key values can be defined in the terraform file in various ways, including as a variable, an environment variable, directly in the code (not recommended), and so on. For more information, see Creating an API Key. -
Accept purchase terms – To use the Megaport Terraform provider, you must accept the purchase terms. This will allow a billable event to occur in the Production environment, that is, create and deploy Megaport services in the Production environment that will incur costs.
provider "megaport" {
environment = "staging"
access_key = "access_key"
secret_key = "secret_Key"
accept_purchase_terms = true
}
Start deploying services
Now that the Megaport Terraform provider is installed and configured, you can start deploying Megaport services.
The following is an example configuration for deploying a single Port, taken from Megaport Single Port.
It shows a data source of megaport_location
, which is set to the data center NextDC BI
. This data center is referenced in the resource that creates the Port. The NextDC B1 data center is where the Port will be created.
terraform {
required_providers {
megaport = {
source = "megaport/megaport"
version = "1.1.8"
}
}
}
provider "megaport" {
environment = "staging"
access_key = "access_key"
secret_key = "secret_Key"
accept_purchase_terms = true
}
data "megaport_location" "bne_nxt1" {
name = "NextDC B1"
}
resource "megaport_port" "port" {
product_name = "Megaport Port Example"
port_speed = 1000
location_id = data.megaport_location.bne_nxt1.id
contract_term_months = 1
marketplace_visibility = false
cost_centre = "Megaport Single Port Example"
}
To build and deploy the configuration, run the terraform apply
command. This will generate a Terraform plan, ensuring that the configuration is valid. In the output, Terraform lists the actions that will be performed. The value known after apply
in the output means the value isn’t known until after the apply process has finished and the data is read from the Megaport API.
Type yes
to confirm.
After the apply process is complete, view the relevant Portal environment, for example, https://portal-staging.megaport.com/, to see the service(s) that have been created.
If you later want to remove the Terraform configuration, run the terraform destroy
command.
For more information, see Basic Terraform CLI Features and Commands.
Terraform FAQs
Do you support OpenTofu?
Previously named OpenTF, OpenTofu is a fork of Terraform that is open-source, community-driven, and managed by the Linux Foundation. OpenTofu is currently compatible with the Megaport Terraform provider and does work, however, OpenTofu is not officially supported and is not actively tested against, so this could change in the future. For more information, see https://opentofu.org/.
Where are the most current examples?
There are examples available for Ports, VXCs, MCRs, and MVEs in the examples directory of the provider. There is also a Guides section in the Megaport Terraform Provider Registry that has comments that go along with the examples.
How does terming work with the provider?
There should be no issues with putting a service on a contract term using Terraform.
Why is there a requested_product_uid field?
This is because when selecting a partner Port, we don’t allow the user to pick the specific Port, and the API will pick one dynamically for capacity reasons. This means that this field can be different to what the user requested, which would cause an error in the provider.
Troubleshooting
Action | Steps |
---|---|
Ensure you are using the latest version of Terraform and the Megaport Terraform provider | Make sure you are using a supported version of Terraform and the latest version of the Megaport Terraform provider, as indicated in the Megaport Terraform Provider Registry. Megaport tests against the last two major versions of Terraform. To check both, run the command: terraform version which should output the version information like this: Terraform v1.8.4 on darwin_arm64 + provider registry.terraform.io/megaport/megaport v1.1.8 |
Resolve Azure VXC deletion error | Deleting an Azure VXC leads to the error:Error: Error deleting resource ****: has an attached peering connection that must be removed in Azure first: To resolve this issue, the ExpressRoute peering should be managed using the Azure Terraform provider instead and it will then be torn down before the VXC. For more information, see the Azure provider documentation. |
Resolve VXC being deleted issue when associated service is deleted | The provider does not handle the situation when a Port is recreated, which then deletes the Port and its associated VXCs. For example, if you had a Terraform file with a Port with a speed of 1000: resource "megaport_port" "port_2" { product_name = "port2" port_speed = 1000 location_id = data.megaport_location.loc.id contract_term_months = 12 marketplace_visibility = false cost_centre = "test2" diversity_zone = "red" } and wanted to update the port_2 speed to 10000, it will trigger the Port to be recreated, which will first delete the Port and its associated VXCs. The provider does not currently handle this and will return an error like this: Error: Error Updating VXC Could not update VXC with ID a7b44cda-28bc-4d64-b05b-8703114a8fcd: PUT https://api-staging.megaport.com/v3/product/vxc/a7b44cda-28bc-4d64-b05b-8703114a8fcd: 400 (trace_id "89b4016d9f9d0b7545987092d0b17493") Product [a7b44cda-28bc-4d64-b05b-8703114a8fcd] Not Found To resolve this, you can specify a lifecycle modifier to the Port resource like below which will first create the new Port, perform the update on the VXC, then delete the old Port. lifecycle { create_before_destroy = true } |
Report the provider error message | If you see the following error message, it indicates a problem in the provider. Please contact support or create a GitHub issue and include details of the process that caused it, to have this error recorded and investigated."This is a bug in the provider, which should be reported in the provider's own issue tracker." |
Helpful references
- https://registry.terraform.io/providers/megaport/megaport/latest
- https://registry.terraform.io/providers/megaport/megaport/latest/docs
- https://github.com/megaport/terraform-provider-megaport
- https://github.com/megaport/terraform-provider-megaport/issues
- Basic Terraform CLI Features and Commands
- Automate Your Multicloud with the Megaport Terraform Provider blog
- 5-minute video on how to use Terraform to configure Megaport services
- 14-minute video on how to use Terraform to configure Megaport services