Skip to content

Commit 0b5f053

Browse files
committed
Create test mks with TF
1 parent 4eac036 commit 0b5f053

File tree

7 files changed

+351
-0
lines changed

7 files changed

+351
-0
lines changed

scripts/mks/.gitignore

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*.tar
2+
/*.tgz
3+
4+
# Intellij
5+
.idea/
6+
out/
7+
/*.iml
8+
9+
# Swap
10+
[._]*.s[a-v][a-z]
11+
[._]*.sw[a-p]
12+
[._]s[a-v][a-z]
13+
[._]sw[a-p]
14+
15+
# Session
16+
Session.vim
17+
18+
# Temporary
19+
.netrwhist
20+
*~
21+
# Auto-generated tag files
22+
tags
23+
24+
### Terraform.gitignore
25+
26+
# Local .terraform directories
27+
**/.terraform/*
28+
**/.terraform*/*
29+
30+
# .tfstate files
31+
*.tfstate
32+
*.tfstate.*
33+
34+
# Crash log files
35+
crash.log
36+
37+
# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
38+
# .tfvars files are managed as part of configuration and so should be included in
39+
# version control.
40+
#
41+
# example.tfvars
42+
43+
# Ignore override files as they are usually used to override resources locally and so
44+
# are not checked in
45+
override.tf
46+
override.tf.json
47+
*_override.tf
48+
*_override.tf.json
49+
50+
# Include override files you do wish to add to version control using negated pattern
51+
#
52+
# !example_override.tf
53+
54+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
55+
# example: *tfplan*

scripts/mks/Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.DEFAULT_GOAL := info
2+
.PHONY: info
3+
4+
SSO_PROFILE=default
5+
6+
info:
7+
@echo "################################################################################"
8+
@echo "### Usage:"
9+
@echo "### make <target>"
10+
@echo "################################################################################"
11+
12+
13+
.PHONY: tf-init
14+
tf-init:
15+
terraform init -input=false
16+
17+
.PHONY: tf-plan
18+
tf-plan: tf-init
19+
terraform plan -input=false
20+
21+
.PHONY: tf-apply
22+
tf-apply: tf-init
23+
terraform apply -input=false -auto-approve
24+
25+
.PHONY: tf-destroy
26+
tf-destroy:
27+
terraform destroy -input=false -auto-approve
28+
29+
.PHONY: sso-login
30+
sso-login:
31+
aws sso login --profile $(SSO_PROFILE)
32+
# install tool from https://github.com/grepplabs/aws-sso/releases
33+
@echo "Store temporary credentials in the ~/.aws/credentials"
34+
aws-sso credentials refresh --profile $(SSO_PROFILE)

scripts/mks/all.auto.tfvars

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# ec2 with mqtt proxy
2+
kafka_proxy_version = "v0.2.2"
3+
mqtt_proxy_enable = true
4+
mqtt_proxy_version = "v0.0.1"
5+
mqtt_proxy_ec2_public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCgqvfLvcrNypxNXDV4wmKnrzWbYlHLPvCK8gVrd3+9Ji093yxVJYn7PgJzbbiBHt6dmtglFfqMaOMowUf++T21n6JWj2nfrXSaO+VhM823D/9i787ZQkHpoiPqbyXvIxaqEAiMmwCRdnz5nr+jAjlWU0rg81JbNz1Tj56TD80a7L7CKWxLzBhELaqpflNLkJy3+uNRQHs70u/7uAA7pQxAJGHWtHr+PWgPBajz4u8YYm9yTmXsNaLDWeuRkpaNs01BgblER7tycN2DykFJbi80LguxtdNcScjPcISPEgWJeRLgtI4CgnaB9cwfFNVMb2qUFjcp5an/mtZhyPcgcosJ mqtt-proxy@ubuntu"
6+
mqtt_proxy_ec2_instance_type = "t3.small"
7+
8+
kafka_version = "2.4.1"
9+
# kafka.t3.small, kafka.m5.large, kafka.m5.xlarge, kafka.m5.2xlarge, ...
10+
kafka_broker_instance_type = "kafka.t3.small"
11+
kafka_broker_ebs_volume_size = 20

scripts/mks/all.vars.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
variable "region" {
2+
type = string
3+
default = "eu-central-1"
4+
}
5+
6+
variable "mqtt_proxy_version" {
7+
type = string
8+
}
9+
10+
variable "mqtt_proxy_ec2_public_key" {
11+
type = string
12+
}
13+
14+
variable "mqtt_proxy_ec2_instance_type" {
15+
type = string
16+
}
17+
18+
variable "mqtt_proxy_enable" {
19+
type = bool
20+
default = true
21+
}
22+
23+
variable "kafka_proxy_version" {
24+
type = string
25+
}
26+
27+
variable "kafka_version" {
28+
type = string
29+
default = "2.4.1"
30+
}
31+
32+
variable "kafka_number_of_broker_nodes" {
33+
type = number
34+
default = 3
35+
}
36+
37+
variable "kafka_broker_instance_type" {
38+
type = string
39+
}
40+
41+
variable "kafka_broker_ebs_volume_size" {
42+
type = number
43+
}

scripts/mks/aws.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
terraform {
2+
required_version = ">= 0.12.18"
3+
}
4+
5+
provider "aws" {
6+
region = var.region
7+
version = ">= 2.45.0"
8+
}
9+
10+
data "aws_caller_identity" "current" {}
11+
12+
data "aws_region" "current" {}
13+
14+
data "aws_availability_zones" "available" {}
15+
16+
data "aws_vpc" "vpc" {
17+
filter {
18+
name = "tag:Name"
19+
values = [
20+
"default"
21+
]
22+
}
23+
}
24+
25+
data "aws_subnet_ids" "subnets" {
26+
vpc_id = data.aws_vpc.vpc.id
27+
}
28+
29+
30+
data "aws_subnet" "subnets" {
31+
count = length(data.aws_subnet_ids.subnets.ids)
32+
id = tolist(data.aws_subnet_ids.subnets.ids)[count.index]
33+
}

scripts/mks/mks.tf

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
resource "aws_security_group" "mqtt-proxy-cluster-security-group" {
2+
vpc_id = data.aws_vpc.vpc.id
3+
4+
ingress {
5+
from_port = 9092
6+
to_port = 9092
7+
protocol = "tcp"
8+
security_groups = [
9+
aws_security_group.mqtt-proxy-security-group.id]
10+
}
11+
ingress {
12+
from_port = 9094
13+
to_port = 9094
14+
protocol = "tcp"
15+
security_groups = [
16+
aws_security_group.mqtt-proxy-security-group.id]
17+
}
18+
egress {
19+
from_port = 0
20+
to_port = 0
21+
protocol = "-1"
22+
cidr_blocks = [
23+
"0.0.0.0/0"]
24+
}
25+
}
26+
27+
resource "aws_msk_cluster" "mqtt-proxy-cluster" {
28+
cluster_name = "mqtt-proxy-cluster"
29+
kafka_version = var.kafka_version
30+
number_of_broker_nodes = var.kafka_number_of_broker_nodes
31+
32+
broker_node_group_info {
33+
instance_type = var.kafka_broker_instance_type
34+
client_subnets = data.aws_subnet.subnets.*.id
35+
security_groups = [ aws_security_group.mqtt-proxy-cluster-security-group.id]
36+
ebs_volume_size = var.kafka_broker_ebs_volume_size
37+
}
38+
# https://docs.aws.amazon.com/msk/latest/developerguide/msk-authentication.html
39+
client_authentication {
40+
tls {
41+
certificate_authority_arns = [
42+
43+
]
44+
}
45+
}
46+
}
47+
48+
output "zookeeper_connect_string" {
49+
value = aws_msk_cluster.mqtt-proxy-cluster.zookeeper_connect_string
50+
}
51+
52+
output "bootstrap_brokers" {
53+
value = aws_msk_cluster.mqtt-proxy-cluster.bootstrap_brokers
54+
}
55+
56+
output "bootstrap_brokers_tls" {
57+
value = aws_msk_cluster.mqtt-proxy-cluster.bootstrap_brokers_tls
58+
}

scripts/mks/proxy.tf

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
resource "aws_instance" "mqtt-proxy" {
2+
count = var.mqtt_proxy_enable ? 1 : 0
3+
ami = data.aws_ami.ubuntu-focal.id
4+
instance_type = var.mqtt_proxy_ec2_instance_type
5+
subnet_id = data.aws_subnet.subnets.0.id
6+
iam_instance_profile = aws_iam_instance_profile.mqtt-proxy-profile.id
7+
vpc_security_group_ids = [aws_security_group.mqtt-proxy-security-group.id]
8+
key_name = aws_key_pair.mqtt-proxy-key-pair.key_name
9+
user_data = <<EOF
10+
#!/usr/bin/env bash
11+
curl -Ls https://github.com/grepplabs/mqtt-proxy/releases/download/${var.mqtt_proxy_version}/mqtt-proxy-${var.mqtt_proxy_version}-linux-amd64.tar.gz | tar xz
12+
mv ./mqtt-proxy /usr/local/bin/mqtt-proxy
13+
14+
# kafka-proxy is not required by mqtt-proxy
15+
curl -Ls https://github.com/grepplabs/kafka-proxy/releases/download/${var.kafka_proxy_version}/kafka-proxy-${var.kafka_proxy_version}-linux-amd64.tar.gz | tar xz
16+
mv ./kafka-proxy /usr/local/bin/kafka-proxy
17+
18+
EOF
19+
}
20+
21+
data "aws_ami" "ubuntu-focal" {
22+
most_recent = true
23+
24+
filter {
25+
name = "name"
26+
values = [
27+
"*ubuntu-focal-*"]
28+
}
29+
30+
filter {
31+
name = "virtualization-type"
32+
values = [
33+
"hvm"]
34+
}
35+
filter {
36+
name = "root-device-type"
37+
values = [
38+
"ebs"]
39+
}
40+
owners = [
41+
"099720109477"]
42+
}
43+
44+
resource "aws_key_pair" "mqtt-proxy-key-pair" {
45+
key_name = "mqtt-proxy-key"
46+
public_key = var.mqtt_proxy_ec2_public_key
47+
}
48+
49+
resource "aws_iam_instance_profile" "mqtt-proxy-profile" {
50+
name = "mqtt-proxy-instance-profile"
51+
role = aws_iam_role.mqtt-proxy-role.name
52+
}
53+
54+
resource "aws_iam_role" "mqtt-proxy-role" {
55+
name = "mqtt-proxy-role"
56+
57+
assume_role_policy = <<EOF
58+
{
59+
"Version": "2012-10-17",
60+
"Statement": [
61+
{
62+
"Action": "sts:AssumeRole",
63+
"Principal": {
64+
"Service": "ec2.amazonaws.com"
65+
},
66+
"Effect": "Allow"
67+
}
68+
]
69+
}
70+
EOF
71+
}
72+
73+
resource "aws_security_group" "mqtt-proxy-security-group" {
74+
name = "mqtt-proxy-security-group"
75+
vpc_id = data.aws_vpc.vpc.id
76+
77+
ingress {
78+
from_port = 1883
79+
to_port = 1883
80+
protocol = "tcp"
81+
cidr_blocks = [
82+
"0.0.0.0/0"]
83+
}
84+
ingress {
85+
from_port = 8883
86+
to_port = 8883
87+
protocol = "tcp"
88+
cidr_blocks = [
89+
"0.0.0.0/0"]
90+
}
91+
ingress {
92+
from_port = 9090
93+
to_port = 9090
94+
protocol = "tcp"
95+
cidr_blocks = [
96+
"0.0.0.0/0"]
97+
}
98+
ingress {
99+
from_port = 22
100+
to_port = 22
101+
protocol = "tcp"
102+
cidr_blocks = [
103+
"0.0.0.0/0"]
104+
}
105+
106+
egress {
107+
from_port = 0
108+
to_port = 0
109+
protocol = "-1"
110+
cidr_blocks = [
111+
"0.0.0.0/0"]
112+
}
113+
}
114+
115+
output "mqtt_proxy_ip" {
116+
value = var.mqtt_proxy_enable ? aws_instance.mqtt-proxy.0.public_ip : ""
117+
}

0 commit comments

Comments
 (0)