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