Create GCP alerting policy for uptime check using terraform - terraform

Is there any way to create GCP alerting policy for uptime check using terraform and filter value of metric.label.check_id of already deployed resource?
Provided examples in the terraform docs show only alerting policy for metrics not for uptime check for already deployed resource so I’m not sure if that is even possible with the terraform.

I have figure out a solution which works in my case.
I have create uptime check and uptime check alert by two separate terraform modules.
Terrraform uptime check module looks like:
resource "google_monitoring_uptime_check_config" "uptime-check" {
project = var.project_id
display_name = var.display_name
timeout = "10s"
period = "60s"
http_check {
path = var.path
port = var.port
use_ssl = true
validate_ssl = true
}
monitored_resource {
type = "uptime_url"
labels = {
host = var.hostname,
project_id = var.project_id
}
}
content_matchers {
content = "\"status\":\"UP\""
}
}
Then for the outputs.tf for that module I have:
output "uptime_check_id" {
value = google_monitoring_uptime_check_config.uptime-check.uptime_check_id
}
Then in the alerts module I have follow terraform docs but modified them to code which looks like:
module "medallies-common-alerts" {
source = "./modules/alerts"
project_id = var.project_id
uptime_check_depends_on = [module.uptime-check]
check_id = module.uptime-check.uptime_check_id
}
...
resource "google_monitoring_alert_policy" "alert_policy_uptime_check" {
project = var.project_id
enabled = true
depends_on = [var.uptime_check_depends_on]
....
condition_threshold {
filter = format("metric.type=\"monitoring.googleapis.com/uptime_check/check_passed\" AND metric.label.\"check_id\"=\"%s\" AND resource.type=\"uptime_url\"",var.check_id)
duration = "300s"
comparison = "COMPARISON_GT"
threshold_value = "1"
trigger {
count = 1
}
...
}
Hope it will help someone too.

Related

3.13.0 New Relic Provider Crashing on Terraform

I am running into an issue with a terraform provider, the new relic plugin keeps crashing for some reason and I don't know why. I'm trying to build a simple alerting script on terraform to create an alerting policy + conditions on the new relic UI. Here is the code below that I'm trying to run;
`terraform {
required_version = "~> 1.3.7"
required_providers{
newrelic = {
source = "newrelic/newrelic"
version = "~> 3.13"
}
}
}
locals{
splitList = [for url in var.urlList: split(".", url)[1]]
finishedList = [for split in local.splitList: join("-", [split, "Cert Check"])]
}
resource "newrelic_alert_policy" "certChecks" {
name = "SSL Cert Check Expirations"
incident_preference = "PER_POLICY"
}
resource "newrelic_alert_channel" "SSL_Alert" {
name = "SSL Expiration Alert"
type = "email"
config {
recipients = "foo.com"
include_json_attachment = "true"
}
}
resource "newrelic_synthetics_alert_condition" "foo" {
policy_id = newrelic_alert_policy.certChecks.id
count = length(var.urlList)
name = "SSL Expiration"
monitor_id = local.finishedList[count.index]
}
resource "newrelic_synthetics_cert_check_monitor" "monitor"{
count = length(var.urlList)
name = local.finishedList[count.index]
domain = var.urlList[count.index]
locations_public = ["US_EAST_1"]
certificate_expiration = "350"
period = "EVERY_DAY"
status = "ENABLED"
}`
It plans but won't apply, it errors out right before. Here is my error message:
Any help would be useful, thank you!
Honestly much hasn't been tried, I tried looking for more information on the terraform community but that search pulled up no results. The only thing I found was changing the location the test would be running from, but I was already in the location needed.

Terraform- GCP Data Proc Component Gateway Enable Issue

I’m trying to create data proc cluster in GCP using terraform resource google_dataproc_cluster. I would like to create Component gateway along with that. Upon seeing the documentation, it has been stated as to use the below snippet for creation:
cluster_config {
endpoint_config {
enable_http_port_access = "true"
}
}
Upon running the terraform plan, i see the error as " Error: Unsupported block type". And also tried using the override_properties and in the GCP data proc, i could see that the property is enabled, but still the Gateway Component is disabled. Wanted to understand, is there an issue upon calling the one given in the Terraform documentation and also is there an alternate for me to use it what?
software_config {
image_version = "${var.image_version}"
override_properties = {
"dataproc:dataproc.allow.zero.workers" = "true"
"dataproc:dataproc.enable_component_gateway" = "true"
}
}
The below is the error while running the terraform apply.
Error: Unsupported block type
on main.tf line 35, in resource "google_dataproc_cluster" "dataproc_cluster":
35: endpoint_config {
Blocks of type "endpoint_config" are not expected here.
RESOURCE BLOCK:
resource "google_dataproc_cluster" "dataproc_cluster" {
name = "${var.cluster_name}"
region = "${var.region}"
graceful_decommission_timeout = "120s"
labels = "${var.labels}"
cluster_config {
staging_bucket = "${var.staging_bucket}"
/*endpoint_config {
enable_http_port_access = "true"
}*/
software_config {
image_version = "${var.image_version}"
override_properties = {
"dataproc:dataproc.allow.zero.workers" = "true"
"dataproc:dataproc.enable_component_gateway" = "true" /* Has Been Added as part of Component Gateway Enabled which is already enabled in the endpoint_config*/
}
}
gce_cluster_config {
// network = "${var.network}"
subnetwork = "${var.subnetwork}"
zone = "${var.zone}"
//internal_ip_only = true
tags = "${var.network_tags}"
service_account_scopes = [
"cloud-platform"
]
}
master_config {
num_instances = "${var.master_num_instances}"
machine_type = "${var.master_machine_type}"
disk_config {
boot_disk_type = "${var.master_boot_disk_type}"
boot_disk_size_gb = "${var.master_boot_disk_size_gb}"
num_local_ssds = "${var.master_num_local_ssds}"
}
}
}
depends_on = [google_storage_bucket.dataproc_cluster_storage_bucket]
timeouts {
create = "30m"
delete = "30m"
}
}
Below is the snippet that worked for me to enable component gateway in GCP
provider "google-beta" {
project = "project_id"
}
resource "google_dataproc_cluster" "dataproc_cluster" {
name = "clustername"
provider = google-beta
region = us-east1
graceful_decommission_timeout = "120s"
cluster_config {
endpoint_config {
enable_http_port_access = "true"
}
}
This issue is discussed in this Git thread.
You can enable the component gateways in Cloud Dataproc by using google-beta provider in the Dataproc cluster and root configuration of terraform.
sample configuration:
# Terraform configuration goes here
provider "google-beta" {
project = "my-project"
}
resource "google_dataproc_cluster" "mycluster" {
provider = "google-beta"
name = "mycluster"
region = "us-central1"
graceful_decommission_timeout = "120s"
labels = {
foo = "bar"
}
...
...
}

Using multiple providers with one resource in Terraform

I'm new to Terraform and I have an issue I can't seem to find a solution on.
I am using the Oneview provider to connect to two Oneview instances. On each one, I am configuring an NTP server (which is the Oneview IP; this is for testing). My (currently functional) provider code looks like this:
terraform {
required_providers {
oneview = {
source = "HewlettPackard/oneview"
version = "6.5.0-13"
}
}
}
provider "oneview" { #These can be replaced with the variables in the variables.tf file
ov_username = "administrator"
ov_password = "mypassword"
ov_endpoint = "https://10.50.0.10/"
ov_sslverify = false
ov_apiversion = 2400
ov_domain = "local"
ov_ifmatch = "*"
}
provider "oneview" {
ov_username = "administrator"
ov_password = "mypassword"
ov_endpoint = "https://10.50.0.50/"
ov_sslverify = false
ov_apiversion = 3200
ov_domain = "local"
ov_ifmatch = "*"
alias = "houston2"
}
and I have the resources in another file:
data "oneview_appliance_time_and_locale" "timelocale" {
}
output "locale_value" {
value = data.oneview_appliance_time_and_locale.timelocale.locale
}
resource "oneview_appliance_time_and_locale" "timelocale" {
locale = "en_US.UTF-8"
timezone = "UTC"
ntp_servers = ["10.50.0.10"]
}
data "oneview_appliance_time_and_locale" "timelocale2" {
}
output "locale_value2" {
value = data.oneview_appliance_time_and_locale.timelocale.locale
}
resource "oneview_appliance_time_and_locale" "timelocale2" {
locale = "en_US.UTF-8"
timezone = "UTC"
ntp_servers = ["10.50.0.50"]
provider = oneview.houston2
}
What I'd like to do is set it up in a way that I can do some sort of "for each provider, run the resource with the correct ntp_server variable", instead of writing a resource for every provider. So for each loop of the resource, it would use the right provider and also grab the right variable for the ntp server.
From what I've read, Terraform doesn't really use traditional for_each statements in a way that I'm used to, and I'm kind of stumped as to how to accomplish this. Does anyone have any suggestions?
Thank you very much for all your help!
resource "oneview_appliance_time_and_locale" "timelocale2" {
for_each = var.provider_list // List contain provider and its alias
locale = "en_US.UTF-8"
timezone = "UTC"
ntp_servers = ["10.50.0.50"]
provider = each.alias
}
Can we try this way, loop through the provider list.. Terraform is supporting the same.

For loop in a Terraform plan?

$ terraform -v
Terraform v0.14.6
I have a Terraform plan that sets up alarms for some of my AWS ECS services, and it looks like this
module "ecs_high_cpu_service_aaa_alarm" {
source = "../modules/cw_alarm"
alarm_name = "ecs-high-cpu-service-aaa-alarm"
service_name = "service-aaa"
// Other parameters
}
module "ecs_high_cpu_service_bbb_alarm" {
source = "../modules/cw_alarm"
alarm_name = "ecs-high-cpu-service-bbb-alarm"
service_name = "service-bbb"
// Other parameters
}
module "ecs_high_cpu_service_123_alarm" {
source = "../modules/cw_alarm"
alarm_name = "ecs-high-cpu-service-123-alarm"
service_name = "service-123"
// Other parameters
}
// More alarms with similar setup as above
As you can see, the modules are all set up similarly, differing only in name, alarm_name and service_name parameters. Is there a way to setup a for-loop that will loop over a map to set the modules up for the plan?
From Mark B's (thanks!) comment, this works.
variables.tf
------------
variable "service_map" {
type = map
default = {
service-aaa = "ecs-high-cpu-service-aaa-alarm"
service-bbb = "ecs-high-cpu-service-bbb-alarm"
service-123 = "ecs-high-cpu-service-123-alarm"
}
description = "Service map; key = service name, value = alarm name"
}
main.tf
-------
module "alarms" {
for_each = var.service_map
source = "../modules/cw_alarm"
service_name = each.key
alarm_name = each.value
// Other parameters
}

terraform plan error : unsupported argument : An argument named "point_in_time_recovery_enabled" is not expected here

I'am trying to create a google cloud sql instance via terraform and i have to enable point in time recovery option but I have the following error :
Error: Unsupported argument
on cloud-sql.tf line 39, in resource "google_sql_database_instance" "si_geny_postgres_logfaces":
39: point_in_time_recovery_enabled = true
An argument named "point_in_time_recovery_enabled" is not expected here.
here is my terraform file :
resource "google_sql_database_instance" "si_geny_postgres_logfaces" {
project = google_project.current_project.project_id
region = var.region
name = "si-sql-instance"
database_version = "POSTGRES_12"
lifecycle {
prevent_destroy = true
ignore_changes = [
settings[0].disk_size, name
]
}
settings {
tier = "db-custom-2-7680"
availability_type = "REGIONAL"
ip_configuration {
ipv4_enabled = false
private_network = data.google_compute_network.si_shared_vpc.self_link
}
location_preference {
zone = var.gce_zone
}
#disk
disk_type = "PD_SSD"
disk_autoresize = true
disk_size = 10 #GB
backup_configuration {
binary_log_enabled = false
point_in_time_recovery_enabled = true
enabled = true
start_time = "00:00" // backup at midnight (GMT)
location = var.region // Custom Location for backups => BACKUP REGION
}
maintenance_window {
day = 1
hour = 3
update_track = "stable"
}
}
}
main.tf
terraform {
required_version = ">0.12.18"
}
provider "google" {
version = "=3.20.0"
project = var.project_id
region = var.region
zone = var.gce_zone
}
provider "google-beta" {
version = "=3.20.0"
project = var.project_id
region = var.region
zone = var.gce_zone
}
Any idea please?
Typically when you get these:
An argument named "..." is not expected here.
issues on terraform. First thing to check is that your file is correct and the property in the error is actually listed in the docs (which this one is).
Next thing is to check that your using the latest version of the provider. As properties are introduced they get added to the documentation but it's not always obvious which version of the provider they were added. You can check to see whichever is the latest provider from the release notes.
So you should upgrade your provider version to the latest (3.40.0) as of time of writing:
provider "google" {
version = "=3.40.0"
project = var.project_id
region = var.region
zone = var.gce_zone
}

Resources