From 867d757aa7c0b689b3c32465ab917dee4a2656c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Dziurzy=C5=84ski?= Date: Fri, 6 Dec 2024 06:45:46 +0100 Subject: [PATCH] ksynth on ec2 instance (#61) * ksynth on ec2 instance * Implemented suggestions from review --- synthetics_AWS/terraform/README.md | 37 +++++++++++++++++++++++++ synthetics_AWS/terraform/ec2.tf | 40 +++++++++++++++++++++++++++ synthetics_AWS/terraform/main.tf | 12 ++++++++ synthetics_AWS/terraform/variables.tf | 35 +++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 synthetics_AWS/terraform/README.md create mode 100644 synthetics_AWS/terraform/ec2.tf create mode 100644 synthetics_AWS/terraform/main.tf create mode 100644 synthetics_AWS/terraform/variables.tf diff --git a/synthetics_AWS/terraform/README.md b/synthetics_AWS/terraform/README.md new file mode 100644 index 0000000..9b6ccd4 --- /dev/null +++ b/synthetics_AWS/terraform/README.md @@ -0,0 +1,37 @@ +# Synthetics agent deployment on AWS ec2 instance + +The module creates ec2 instance with provided configuration and deploys `ksynth` agent. + +## Requirements + +| Name | Version | +|--------------|---------| +| terraform | ~> 1.0 | +| aws provider | ~> 4.0 | +| AWS CLI | ~> 4.0 | + + +## Inputs + +| Name | Description | Type | Default | Required | +|------------------------|--------------------------------------------------|--------------|---------|----------| +| vpc_security_group_ids | List of security groups IDs | list(string) | | true | +| plan_id | Kentik plan ID | string | | true | +| region | Specifies AWS provider region | string | | false | +| key_name | Key name of the Key Pair to use for the instance | string | | false | +| iam_instance_profile | IAM role ID | string | | false | +| subnet_id | Subnet ID | string | | false | + + +## Usage + + ```bash + terraform init + terraform apply \ + -var='vpc_security_group_ids=[""]' \ + -var='plan_id=' \ + -var='region=' \ + -var='key_name=' \ + -var='iam_instance_profile=' \ + -var='subnet_id=' + ``` \ No newline at end of file diff --git a/synthetics_AWS/terraform/ec2.tf b/synthetics_AWS/terraform/ec2.tf new file mode 100644 index 0000000..6789fe8 --- /dev/null +++ b/synthetics_AWS/terraform/ec2.tf @@ -0,0 +1,40 @@ +data "aws_ami" "ubuntu" { + most_recent = true + + filter { + name = "name" + # Make sure that given ami is not deprecated. + values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = ["099720109477"] +} + +resource "aws_instance" "instance" { + ami = data.aws_ami.ubuntu.id + instance_type = "t2.micro" + + iam_instance_profile = var.iam_instance_profile + key_name = var.key_name + vpc_security_group_ids = var.vpc_security_group_ids + subnet_id = var.subnet_id + + user_data = <> /etc/default/ksynth +systemctl start ksynth +# Restart agent after setting variables +systemctl stop ksynth && systemctl start ksynth +EOF + + tags = { + Name = "ksynth_agent" + } +} \ No newline at end of file diff --git a/synthetics_AWS/terraform/main.tf b/synthetics_AWS/terraform/main.tf new file mode 100644 index 0000000..6e34dbf --- /dev/null +++ b/synthetics_AWS/terraform/main.tf @@ -0,0 +1,12 @@ +terraform { + required_version = "~> 1.0" + required_providers { + aws = { + version = "~> 4.0" + } + } +} + +provider "aws" { + region = var.region +} \ No newline at end of file diff --git a/synthetics_AWS/terraform/variables.tf b/synthetics_AWS/terraform/variables.tf new file mode 100644 index 0000000..8d3bac8 --- /dev/null +++ b/synthetics_AWS/terraform/variables.tf @@ -0,0 +1,35 @@ +variable "key_name" { + description = "Key name of the Key Pair to use for the instance" + type = string + default = "" +} + +variable "iam_instance_profile" { + description = "IAM role ID" + type = string + default = "" +} + +variable "subnet_id" { + description = "Subnet ID" + type = string + default = "" +} + +variable "vpc_security_group_ids" { + description = "List of security groups IDs" + type = list(string) + default = [""] +} + +variable "plan_id" { + description = "Kentik plan ID" + type = string + default = "" +} + +variable "region" { + description = "Specifies AWS provider region" + type = string + default = "" +} \ No newline at end of file