Skip to content

Instantly share code, notes, and snippets.

@nivleshc
nivleshc / visualise-network-traffic-grafana-grafana.tf
Created March 30, 2024 04:43
Contents of grafana/grafana.tf from the visualise-network-traffic repository.
# 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"
@nivleshc
nivleshc / visualise-network-traffic-grafana-providers.tf
Created March 30, 2024 04:35
Contents of grafana/providers.tf from the visualise-network-traffic repository.
# 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"
}
}
}
@nivleshc
nivleshc / visualise-network-traffic-grafana-variables.tf
Created March 30, 2024 04:31
Contents of grafana/variables.tf from the visualise-network-traffic repository.
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
@nivleshc
nivleshc / visualise-network-traffic-grafana-main.tf
Created March 30, 2024 04:28
Contents of grafana/main.tf from the visualise-network-traffic repository.
# 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"],
@nivleshc
nivleshc / visualise-network-traffic-grafana-securitygroups.tf
Created March 30, 2024 02:48
Contents of grafana/securitygroups.tf from the visualise-network-traffic repository.
# 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"
}
}
@nivleshc
nivleshc / visualise-network-traffic-grafana-outputs.tf
Created March 30, 2024 02:40
Contents of grafana/outputs.tf from the visualise-network-traffic repository.
output "public_ip" {
description = "The public ip of the grafana server"
value = aws_instance.grafana_server.public_ip
depends_on = [
aws_instance.grafana_server
]
}
@nivleshc
nivleshc / visualise-network-traffic-grafana-locals.tf
Created March 30, 2024 02:33
Contents of grafana/locals.tf from the visualise-network-traffic repository.
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]])
}
@nivleshc
nivleshc / visualise-network-traffic-grafana-data.tf
Created March 30, 2024 02:32
Contents of grafana/data.tf from the visualise-network-traffic repository.
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
@nivleshc
nivleshc / visualise-network-traffic-grafana-iam.tf
Created March 30, 2024 02:30
Contents of grafana/iam.tf from the visualise-network-traffic repository.
# 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": {
@nivleshc
nivleshc / visualise-network-traffic-main-02.yaml
Created March 27, 2024 13:32
contents of the main.tf file from the blog-visualise-network-traffic repository
# create the vpc
module "vpc" {
source = "./vpc"
vpc = local.vpc
private_subnet = local.subnets["private"]
public_subnet = local.subnets["public"]
}