How to use Terraform in a cloud agnostic way

I have seen many examples of how to use Terraform to run AWS resources. I have also seen many allegations that Terraform is an agnostic cloudy.

What I have not seen is an example of how I can run VPC with some subnets, some instances, some ELBs and several databases in AWS or Azure using a single file tf.

Does anyone have an example?

+4
source share
3 answers

While Terraform as a tool is a cloud agnostic (in it it will support everything that its API provides and has sufficient developer support to create a “provider” for it), Terraform alone will not be able to distract this at all, and I would seriously consider whether this is really a good idea if you do not have a really good use case.

If you need it, you need to put together a bunch of modules on top of things that abstract the cloud layer from the users of the module and just let them specify the cloud provider as a variable (potentially controlled by an external script).

As a basic example for abstract DNS, you might have something like this (untested):

modules / google / dns / record / main.tf

variable "count" = {}

variable "domain_name_record" = {}
variable "domain_name_zone" = {}
variable "domain_name_target" = {}

resource "google_dns_record_set" "frontend" {
  count = "${variable.count}"
  name  = "${var.domain_name_record}.${var.domain_name_zone}"
  type  = "CNAME"
  ttl   = 300

  managed_zone = "${var.domain_name_zone}"

  rrdatas = ["${var.domain_name_target}"]
}

Modules / AWS / DNS / Record / main.tf

variable "count" = {}

variable "domain_name_record" = {}
variable "domain_name_zone" = {}
variable "domain_name_target" = {}

data "aws_route53_zone" "selected" {
  count = "${variable.count}"
  name  = "${var.domain_name_zone}"
}

resource "aws_route53_record" "www" {
  count   = "${variable.count}"
  zone_id = "${data.aws_route53_zone.selected.zone_id}"
  name    = "${var.domain_name_record}.${data.aws_route53_zone.selected.name}"
  type    = "CNAME"
  ttl     = "60"
  records = [${var.domain_name_target}]
}

modules / general / dns / record / main.tf

variable "cloud_provider" = { default = "aws" }

variable "domain_name_record" = {}
variable "domain_name_zone" = {}
variable "domain_name_target" = {}

module "aws_dns_record" {
  source             = "../../aws/dns/record"
  count              = "${var.cloud_provider == "aws" ? 1 : 0}"
  domain_name_record = "${var.domain_name_record}"
  domain_name_zone   = "${var.domain_name_zone}"
  domain_name_target = "${var.domain_name_target}"
}

module "google_dns_record" {
  source             = "../../google/dns/record"
  count              = "${var.cloud_provider == "google" ? 1 : 0}"
  domain_name_record = "${var.domain_name_record}"
  domain_name_zone   = "${var.domain_name_zone}"
  domain_name_target = "${var.domain_name_target}"
}

, , , "" , . , , , , StackOverflow.

+5

.

Terraform terraform apply.

.

, terraform aws_instance azurerm_virtual_machine aws, azure.

0

@ydaetskcoR ( ). , , , main.tf , .

, :
/Google/DNS//main.tf
/aws/dns/record/main.tf

modules/generic/dns/record/main.tf

:
main.tf

variable "cloud_provider" = { default = "aws" }  
module "dns" {  
  source = "modules/${var.cloud_provider}/dns/record/main.tf"  
  domain_name_record = "${var.domain_name_record}"  
  domain_name_zone   = "${var.domain_name_zone}"  
  domain_name_target = "${var.domain_name_target}"  
}

, Terraform .
.: https://github.com/hashicorp/terraform/issues/1439

.

0

Source: https://habr.com/ru/post/1672264/


All Articles