Skip to content

Instantly share code, notes, and snippets.

@deepybee
Last active November 24, 2021 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepybee/201469356f7cf943ea4d6aa9700b6001 to your computer and use it in GitHub Desktop.
Save deepybee/201469356f7cf943ea4d6aa9700b6001 to your computer and use it in GitHub Desktop.
// Every root module needs a terraform block
terraform {
// Anything after version 1.0.0 maintains backward compatibility
required_version = ">=1.0.0"
// Here we specify which providers Terraform should use, in this case the Elastic Cloud Provider, ec
required_providers {
/*
Unqualified names will pull from the Github backed Terraform Registry;
https://registry.terraform.io/providers/elastic/ec
https://github.com/elastic/terraform-provider-ec
*/
ec = {
source = "elastic/ec"
version = "0.3.0" // Pinning to a specific version ensures compatibility and avoids nasty surprises
}
}
}
// Here we pass parameters to the provider. In this case we're injecting a variable, api_key into the apikey parameter
provider "ec" {
apikey = var.api_key
}
// Resources require a type (ec_deployment) and an arbitrary name (workshop).
resource "ec_deployment" "workshop" {
name = join("-", [var.my_name, "terraform-workshop"])
version = "7.15.2"
region = "gcp-europe-west4"
deployment_template_id = "gcp-io-optimized"
elasticsearch {}
kibana {}
}
output "deployment_id" {
value = ec_deployment.workshop.id
}
output "deployment_version" {
value = ec_deployment.workshop.version
}
output "cloud_id" {
value = ec_deployment.workshop.elasticsearch[0].cloud_id
}
// Outputs marked sensitive do not display the value in console output, and must be explicitly requested with
// terraform output <output_name>
output "elastic_password" {
value = ec_deployment.workshop.elasticsearch_password
sensitive = true
}
// With a few of Terraform's functions, we can create a useful output for the alias endpoints
// With a few of Terraform's functions, we can create a useful output for the Kibana alias endpoint
output "elasticsearch_friendly_endpoint" {
value = join(" ", [
replace(ec_deployment.workshop.kibana[0].https_endpoint,
split(".", ec_deployment.workshop.kibana[0].https_endpoint)[0],
"https://${ec_deployment.workshop.name}.es")
])
}
output "kibana_friendly_endpoint" {
value = join(" ", [
replace(ec_deployment.workshop.kibana[0].https_endpoint,
split(".", ec_deployment.workshop.kibana[0].https_endpoint)[0],
"https://${ec_deployment.workshop.name}.kb")
])
}
variable api_key {
description = "The API key to connect to Elastic Cloud with"
type = string
sensitive = true
}
variable my_name {
description = "Your name (without spaces)"
type = string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment