Creating a Megaport Terraform Provider Configuration File
This topic provides step-by-step instructions for preparing a Megaport Terraform Provider configuration file, based on common and effective methods identified by Megaport Solution Architects through customer engagements.
Before you start, perform the prerequisites described in Getting Started with the Megaport Terraform Provider.
Creating a configuration file
When you are ready with your network design, you can create executable Terraform configuration files to deploy the design using these steps.
To create a configuration file
-
Design your network with Megaport services. Sample Megaport Terraform Provider configuration files are available in the Megaport Terraform Provider GitHub repository.
Megaport also provides explanations about common multicloud connectivity scenarios. Sample Megaport Terraform Provider configuration files of common multicloud connectivity scenarios can be found in the Megaport Terraform Provider GitHub examples page.
-
Identify the Megaport enabled location name, site code, or ID in the Portal. This is the easiest way to find a location.
However, data center location names and site codes can sometimes change in the API. For reliable and stable Megaport Terraform Provider configurations, we strongly recommended you use the location ID instead of names or codes. The most up-to-date listing of Megaport data center locations can be accessed through the GET /v3/locations Megaport API.
-
Define the Megaport Terraform Provider environment.
There are a few ways to manage Terraform directories and configuration files (.tf files). You need to establish the most effective way to manage your Terraform resources to meet your needs.
The following example shows how you could keep Terraform configuration files in an organized manner.
-
Create a directory to keep a set of Terraform configuration files to manage a certain set of Megaport services, such as Port, MCR, VXC.
-
Create a
provider.tf
file which declares the Megaport Terraform Provider environment.Note
You can define settings of other providers, such as AWS, Azure, and GCP in the
provider.tf
file. For more information, see Build Infrastructure (HashiCorp). -
Keep the
provider.tf
file in the same directory that other Terraform configuration files are stored. The following example shows theprovider.tf
file containing the Megaport Terraform Provider related configurations:
terraform { required_providers { megaport = { source = "megaport/megaport" version = "1.3.6" } } } provider "megaport" { environment = "staging" access_key = "access_key" secret_key = "secret_Key" accept_purchase_terms = true }
Note
There are some important fields and information you need to define to configure the Megaport Terraform Provider configuration file.
-
Version – Megaport tests against the two latest major versions of Terraform. You can find the latest version of the Megaport Terraform Provider in the Megaport Terraform Provider Registry documentation. We highly recommend that you keep the
provider.tf
file updated with the latest version number. -
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.API keys are valid only in the environment in which they were created. If you need an API key to access a different environment, log in to the Megaport Portal in that environment and create the API key there. For more information, see Creating an API Key.
-
Accept purchase terms – To order Megaport services using Terraform, 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.
-
-
Create the
main.tf
file, which declares your infrastructure. Then copy the sample configuration that implements a network design closest to yours and modify it as required in themain.tf
file.You can find sample configurations in the Megaport Terraform Provider GitHub repository.
The following example shows a Megaport Terraform Provider configuration to order a single Port.
When you deploy Terraform,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" }
provider.tf
is executed first. Thenmain.tf
will be executed automatically along with any other Terraform configuration files saved in the same directory. -
If this is your first time using Terraform, run the
terraform init
command in your directory to prepare the working directory containing Terraform configuration files.This command performs several setup tasks such as downloading the Megaport Terraform Provider and other backend plugins to prepare your working directory for use with Terraform.
To run this command, the directory must first have Terraform configuration files. It is safe to run this command multiple times to bring the working directory up to date with changes in the configuration.
The following image shows an example of the output of the
terraform init
command.For more information, see terraform init command (HashiCorp).
Next steps
After you have prepared the Terraform configuration files, you can deploy them in the staging environment for initial testing before applying them to the production environment.
For more information, see Creating and Managing Services using the Megaport Terraform Provider.
Helpful references
- Megaport Terraform Provider Registry documentation
- Megaport Terraform Provider GitHub repository
- Megaport Terraform Provider GitHub multicloud scenarios examples
- Build infrastructure (HashiCorp)
- Provider Requirements (HashiCorp)
- terraform init command (HashiCorp)
- Megaport Live: Tech Drop-in: APAC 2024/09/27 - Megaport Overview & Terraform Overview