> ## Documentation Index
> Fetch the complete documentation index at: https://docs.megaport.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Creating and Managing Services using the Megaport Terraform Provider

> This help topic describes steps to execute a terraform configuration file.

export const Image = ({src, alt = '', caption, width, height, shadow}) => {
  const errorPrefix = '\x1b[31;1;4mERROR\x1b[0m: Image -';
  if (typeof src !== 'string') {
    src = String(src);
  }
  if (!src || src === 'undefined') {
    console.error(`${errorPrefix} \`src\` is required — rendering an empty string instead`);
    return <></>;
  }
  const img = <img className={['mega-image-style', shadow ? 'shadow' : ''].join(' ')} src={src} alt={alt} width={width} height={height} style={{
    display: 'block'
  }} />;
  return caption ? <figure>
      {img}
      <figcaption style={{
    textAlign: 'center',
    width
  }}>{caption}</figcaption>
    </figure> : img;
};

This topic describes the steps to create and manage Megaport services using the Megaport Terraform Provider.

Having learnt the basic knowledge for preparing Terraform configuration files, you are now ready to use the Megaport Terraform Provider to create and manage your Megaport services.

<Callout icon="triangle-exclamation" color="red">
  To prevent accidental modification or deletion of production services, we recommend that you test Terraform files in the staging environment first before applying them to the production environment.
</Callout>

## Preparing to deploy services with Terraform

To apply changes in the staging environment, confirm that your `provider.tf` file contains the following configuration.

```terraform theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
environment = "staging"
```

We recommend that you run the `terraform plan` command before applying changes, to review your execution plan.

The `terraform plan` command creates an execution plan, which lets you preview the actions that Terraform will make to your infrastructure.

This plan details exactly what Terraform will do to reach the state defined in your configuration files. You can preview which resources will be created, changed, or destroyed. This allows you to review the proposed changes before they are applied, preventing unexpected or unwanted modifications to your infrastructure.

The image below shows an example output running the `terraform plan` command. In this example, the Terraform configuration file will create a new month-to-month 1 Gpbs Port, named *Megaport Port A-End* at NextDC B2.

<img src="https://mintcdn.com/megaport-b7713936/pyW7zaB5dtPD-GSa/images/terraform/tf-plan-sample-output.png?fit=max&auto=format&n=pyW7zaB5dtPD-GSa&q=85&s=7806ad794ddf424e2a8c57c8cfb55766" alt="Sample output of terraform plan" width="556" height="580" data-path="images/terraform/tf-plan-sample-output.png" />

For more information, see [terraform plan command (HashiCorp)](https://developer.hashicorp.com/terraform/cli/commands/plan).

## Creating test services

After you are satisfied with the changes in the Terraform plan, you can create services in the staging environment using the following command in your terminal.

```shell theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
terraform apply
```

This command creates new services based on the definition in the Terraform configuration file.  See the following example output.

<Image src="/images/terraform/tf-apply-sample-outcome.png" alt="Sample outcome of applying the Terraform configuration file to create two Ports and one VXC." />

Type `yes` to confirm.

After you type `yes`, the Terraform configuration file creates services and an *Apply complete!* message appears.

<img src="https://mintcdn.com/megaport-b7713936/pyW7zaB5dtPD-GSa/images/terraform/tf-apply-complete.png?fit=max&auto=format&n=pyW7zaB5dtPD-GSa&q=85&s=5b45e11ab0a8ac709f464b40dbbbc58c" alt="Sample output of Apply Complete" width="503" height="48" data-path="images/terraform/tf-apply-complete.png" />

After the apply process is complete, view the [staging Portal](https://portal-staging.megaport.com/) to confirm that the service(s) have been created as designed in the Terraform configuration file.

## Changing test services

To modify your services (for example, service name, VXC speed), update the Terraform configuration file ( `main.tf` ) you initially used. After making the required changes, apply them using the following command. For testing, ensure that you first apply these changes in the staging environment.

```shell theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
terraform apply
```

## Removing test services

After testing, clean up the staging environment by removing the services you created for the test using the following command:

```shell theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
terraform destroy
```

Make sure you are removing services in the staging environment, not in the production environment.  See the example output below.

<img src="https://mintcdn.com/megaport-b7713936/pyW7zaB5dtPD-GSa/images/terraform/tf-destroy.png?fit=max&auto=format&n=pyW7zaB5dtPD-GSa&q=85&s=ceb7d70744869912f7d663aeef76e709" alt="Sample output of terraform destroy" width="597" height="116" data-path="images/terraform/tf-destroy.png" />

Type `yes` to confirm.

After you type `yes`, services defined in the Terraform configuration file will be removed, and a *Destroy complete!* message appears.

After the destroy process is complete, view the [staging Portal](https://portal-staging.megaport.com/) to confirm that the services are removed.

<img src="https://mintcdn.com/megaport-b7713936/pyW7zaB5dtPD-GSa/images/terraform/tf-destroy-complete.png?fit=max&auto=format&n=pyW7zaB5dtPD-GSa&q=85&s=c36435e07fc6708d6c3d56c613cf688d" alt="Sample output of destroy complete" width="398" height="45" data-path="images/terraform/tf-destroy-complete.png" />

<Tip>
  You need to refresh the staging Portal to check that the test services have been removed.
</Tip>

## Creating services in production

After you complete testing in the staging environment and are satisfied with the outcome, you can run the ` terraform apply` command in the production environment by specifying the production environment in the `provider.tf` file.

```terraform theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
environment = "production"
```

You need to create an API key in the production environment before running `terraform apply`. For more information, see \[API key]\(/terraform/megaport-terraform-configuration#API key).

After running the `terraform apply` command, refresh the Production Portal to check that the services have been created.

## Helpful references

* [Megaport Terraform Provider GitHub repository](https://github.com/megaport/terraform-provider-megaport)
* [Megaport Terraform Provider Registry documentation](https://registry.terraform.io/providers/megaport/megaport/latest/docs)
* [terraform apply command (HashiCorp)](https://developer.hashicorp.com/terraform/cli/commands/apply)
* [Terraform CLI Overview (HashiCorp)](https://developer.hashicorp.com/terraform/cli/commands)
* [terraform destroy command (HashiCorp)](https://developer.hashicorp.com/terraform/cli/commands/destroy)
* [terraform plan command (HashiCorp)](https://developer.hashicorp.com/terraform/cli/commands/plan)
