Terraform
Overview
The Aptible Terraform provider allows you to manage your Aptible resources directly from Terraform - enabling infrastructure as code (IaC) instead of manually initiating Operations from the Aptible Dashboard of Aptible CLI. You can use the Aptible Terraform to automate the process of setting up new Environments, including:
-
Creating, scaling, modifying, and deprovisioning Apps and Databases
-
Creating and deprovisioning Log Drains and Metric Drains (including the Aptible Terraform Metrics Module, which provisions built Grafana dashboards with alerting)
-
Creating, modifying, and provisioning App Endpoints and Database Endpoints
For an overview of what actions the Aptible Terraform Provider supports, see the Feature Support Matrix.
Using the Aptible Terraform Provider
Environment definition
Environments are defined as data sources and should point toward a pre-existing environment that the Terraform user already has access to:
Deployment and managing Docker images
Direct Docker Image Deployment is currently the only deployment method supported with Terraform. If you’d like to use Terraform to deploy your Apps and you’re currently using Dockerfile Deployment you’ll need to switch. See Migrating from Dockerfile Deploy for tips on how to do so.
If you’re already using Direct Docker Image Deployment, managing this is pretty easy. Set your Docker repo, registry username, and registry password as the configuration variables APTIBLE_DOCKER_IMAGE
, APTIBLE_PRIVATE_REGISTRY_USERNAME
, and APTIBLE_PRIVATE_REGISTRY_PASSWORD
.
terraform apply
. If you are deploying outside of Terraform, you will also need to keep your Terraform configuration up to date. See Terraform’s refresh Terraform configuration documentation for more information.Managing Services
Managing Services
The service process_type
should match what’s contained in your Procfile. Otherwise, service container sizes and container counts cannot be defined and managed individually.
Referencing Resources in Configurations
Resources can easily be referenced in configurations when using Terraform. Here is an example of an App configuration that references Databases:
Some apps use the port, hostname, username, and password broken apart rather than as a standalone connection URL. Terraform can break those apart, or you can add some logic in your app or container entry point to achieve this. This also works with endpoints. For example:
The value aptible_endpoint.example-endpoint.virtual_domain
will be the domain used to access the Endpoint (so app-0000.on-aptible.com
or www.example.com
).
virtual_domain
would be something like *.example.com
which is not a valid domain name. Therefore, when using a wildcard domain, you should provide the subdomain you want your application to use to access the Endpoint, like www.example.com
, rather than relying solely on the Endpoint’s virtual_domain
.Circular Dependencies
One potential risk of relying on URLs to be set in App configurations is circular dependencies. This happens when your App uses the Endpoint URL in its configuration, but the Endpoint cannot be created until the App exists. Terraform does not have a graceful way of handling circular dependencies. While this approach won’t work for default domains, the easiest option is to define a variable that can be referenced in both the Endpoint resource and the App configuration:
Managing DNS
While Aptible does not directly manage your DNS, we do provide you the information you need to manage DNS. For example, if you are using Cloudflare for your DNS, and you have an endpoint called example-endpoint
, you would be able to create the record:
And for the Managed HTTPS dns-01 verification record:
Secure/Sensitive Values
You can use Terraform to mark values as secure. These values are redacted in the output of terraform plan
and terraform apply
.
When you run terraform state show
these values will also be marked as sensitive. For example:
Spinning down Terraform Resources
Resources created using Terraform should not be deleted through the Dashboard or CLI. Deleting through the Dashboard or CLI does not update the Terraform state which will result in errors the next time you run terraform plan or terraform apply. Instead, use terraform plan -destroy to see which resources will be destroyed and then use terraform destroy to destroy those resources.
If a Terraform-created resource is deleted through the Dashboard or CLI, use the terraform state rm command to remove the deleted resource from the Terraform state file. The next time you run terraform apply, the resource will be recreated to reflect the configuration.
Was this page helpful?