This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# use this resource to check that the grafana server is accessible, otherwise the grafana provider will fail | |
resource "null_resource" "check_grafana_server_is_accessible" { | |
triggers = { | |
always_run = timestamp() | |
} | |
provisioner "local-exec" { | |
command = "bash -c 'until curl --output /dev/null --silent --head --fail $${URL}; do sleep 10; done'" | |
environment = { | |
URL = "http://${aws_instance.grafana_server.public_ip}:3000" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this needs to be declared inside this module as well as the root module | |
terraform { | |
required_providers { | |
grafana = { | |
source = "grafana/grafana" | |
version = ">= 2.13.0" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "grafana_server_details" { | |
description = "Configuration details for the Grafana Server" | |
type = object({ | |
ami_id = string | |
instance_type = string | |
key_name = string | |
vpc_id = string | |
subnet_id = string | |
admin_username = string | |
admin_password = string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create the Grafana server | |
resource "aws_instance" "grafana_server" { | |
ami = var.grafana_server_details["ami_id"] | |
instance_type = var.grafana_server_details["instance_type"] | |
key_name = var.grafana_server_details["key_name"] | |
subnet_id = var.grafana_server_details["subnet_id"] | |
vpc_security_group_ids = [aws_security_group.grafana_sg.id] | |
user_data = templatefile("${path.module}/scripts/user-data.tpl.sh", { | |
admin_username = var.grafana_server_details["admin_username"], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a security group that will be attached to the Grafana ec2 instance | |
resource "aws_security_group" "grafana_sg" { | |
name = "${var.grafana_server_details["tags"]["Name"]}-sg" | |
description = "Security group for grafana server" | |
vpc_id = var.grafana_server_details["vpc_id"] | |
tags = { | |
Name = "${var.grafana_server_details["tags"]["Name"]}-sg" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
output "public_ip" { | |
description = "The public ip of the grafana server" | |
value = aws_instance.grafana_server.public_ip | |
depends_on = [ | |
aws_instance.grafana_server | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
locals { | |
account_id = data.aws_caller_identity.current.account_id | |
region = data.aws_region.current.name | |
loggroup_name = var.grafana_server_details["data_source"]["default_log_group_name"] | |
cloudwatch_ds_uid = var.grafana_server_details["data_source"]["uid"] | |
cloudwatch_org_id = var.grafana_server_details["data_source"]["org_id"] | |
traffic_dest_cidr = var.grafana_server_details["data_source"]["traffic_dest_cidr"] | |
traffic_dest_prefix = join(".", [split(".", local.traffic_dest_cidr)[0], split(".", local.traffic_dest_cidr)[1], split(".", local.traffic_dest_cidr)[2]]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data "aws_region" "current" {} | |
data "aws_caller_identity" "current" {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create an IAM role for the Grafana ec2 instance. | |
resource "aws_iam_role" "grafana_role" { | |
name = format("%s-role", var.grafana_server_details["tags"]["Name"]) | |
assume_role_policy = <<EOF | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Principal": { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create the vpc | |
module "vpc" { | |
source = "./vpc" | |
vpc = local.vpc | |
private_subnet = local.subnets["private"] | |
public_subnet = local.subnets["public"] | |
} |
NewerOlder