# Manage CloudAMQP resources with Terraform

By Patrik and Tobias. Published 20 February 2023.

Create, update, and configure CloudAMQP instances, VPCs, and more features with a simple configuration file. Here’s an introduction to how to use Terraform to manage your CloudAMQP resources through code.

## What is Terraform?

[Terraform](https://www.terraform.io/) is a software tool built by HashiCorp to automate and handle infrastructure as code. The tool makes managing infrastructure resources or API backends easier by provisioning and teardown of specified resources.

Terraform relies on plugins called providers that have been built to support different types of infrastructure. The provider generally contains a configuration, a set of resources, and/or data sources that the provider can manage. The [Terraform Registry](https://registry.terraform.io/) is a repository for many official, partner, and community providers to choose from.

## CloudAMQP Terraform provider

The [CloudAMQP Terraform provider](https://registry.terraform.io/providers/cloudamqp/cloudamqp/latest) allows you to manage your CloudAMQP instances, as well as create, configure, and deploy RabbitMQ to different cloud platforms. You can also manage standalone VPC, configure firewalls and alarms, and add integrations. [See our documentation in the Registry](https://registry.terraform.io/providers/cloudamqp/cloudamqp/latest/docs) for a full list of resources available.

## Getting started

Start by creating an account at&nbsp;[CloudAMQP](https://www.cloudamqp.com/), then create your [account API key](https://customer.cloudamqp.com/apikeys) to manage a CloudAMQP instance. Create an configuration file (you can name it&nbsp;`main.tf`) with the below content and run `terraform init` in the same folder as your configuration file. Terraform will download the latest version of the CloudAMQP provider.

```hcl
terraform {
  required_providers {
    cloudamqp = {
      source = "cloudamqp/cloudamqp"
      version = "~>1.0"
    }
  }
}
```

## Standalone VPC and CloudAMQP instance

The following configuration creates a standalone VPC and a CloudAMQP instance running RabbitMQ.

```hcl
resource "cloudamqp_vpc" "vpc" {
  name    = "Test VPC"
  region  = "amazon-web-services::us-west-1"
  subnet  = "10.56.72.0/24"
  tags    = ["terraform"]
}

resource "cloudamqp_instance" "instance" {
  name                  = "Test instance"
  plan                  = "squirrel-1"
  region                = "amazon-web-services::us-west-1"
  tags                  = ["terraform"]
  rmq_version           = "3.11.5"
  vpc_id                = cloudamqp_vpc.vpc.id
  no_default_alarms     = true
  keep_associated_vpc   = true
}
```

To add the CloudAMQP instance to the standalone VPC, we set the `vpc_id` in the instance resource to depend on the VPC resource: `vpc_id = clooudamqp_vpc.vpc.id`

To check the configuration used, use `terraform plan` to see what resources will be provisioned. If the changes look good, run `terraform apply` to apply the changes. You need to configure your shell with `CLOUDAMQP_APIKEY` or supply it when running the commands.

```bash
# To see what changes Terraform will make
CLOUDAMQP_APIKEY=changeme terraform plan

# To apply the changes
CLOUDAMQP_APIKEY=changeme terraform apply

# To destroy any resources created by Terraform
CLOUDAMQP_APIKEY=changeme terraform destroy
```

If run into any problems or are missing some feature in the provider, don’t hesitate to reach out to us either via the support email or on GitHub issues.

The source code for the provider is located on GitHub at [cloudamqp/terraform-provider-cloudamqp](https://github.com/cloudamqp/terraform-provider-cloudamqp) and external contributions are welcome.
