Skip to content

Commit

Permalink
added terraform files
Browse files Browse the repository at this point in the history
  • Loading branch information
rafal-nowak authored and ogedo committed May 6, 2023
1 parent 7f9f560 commit ceb925b
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,14 @@ dist-ssr
*.sw?

**/venv

# Terraform
# local .terraform dir
**/.terraform/*

# tf state files
**/*.tfstate
**/*.tfstate.*

# tf variable files, may include sensitive data
**/*.tfvars
25 changes: 25 additions & 0 deletions devops/terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions devops/terraform/entry-template.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

sudo yum update -y && sudo yum install -y docker
sudo systemctl start docker
sudo usermod -aG docker ec2-user
public_ip=$(curl -s https://api.ipify.org)
mqtt_endpoint="tcp://$public_ip:1883"
export MQTT_ENDPOINT="$mqtt_endpoint"
echo "MQTT_ENDPOINT=$mqtt_endpoint" | sudo tee -a /etc/environment
docker run -d --name mqtt \
-p 1883:1883 \
-p 9001:9001 \
-e MQTT_USERNAME=${mqtt_username} \
-e MQTT_PASSWORD=${mqtt_password} \
-e MQTT_LISTENER=${mqtt_listener} \
rafalnowak444/dm-mosquitto:latest
docker run -d --name react \
-p 80:5173 \
rafalnowak444/device-management-react:latest
docker run -d --name device-management \
-p 8080:8080 \
-e MQTT_USERNAME=${mqtt_username} \
-e MQTT_PASSWORD=${mqtt_password} \
-e MQTT_ENDPOINT="$mqtt_endpoint" \
-e DYNAMO_ENDPOINT=${dynamo_endpoint} \
-e AWS_REGION=${aws_region} \
-e AWS_ACCESS_KEY_ID=${aws_access_key_id} \
-e AWS_SECRET_ACCESS_KEY=${aws_secret_access_key} \
rafalnowak444/device-management:latest
156 changes: 156 additions & 0 deletions devops/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
provider "aws" {
region = "eu-north-1"
}

variable "vpc_cidr_block" {}
variable "subnet_cidr_block" {}
variable "avail_zone" {}
variable "env_prefix" {}
variable "instance_type" {}
variable "public_key_location" {}
variable "mqtt_username" {}
variable "mqtt_password" {}
variable "mqtt_listener" {}
variable "dynamo_endpoint" {}
variable "aws_region" {}
variable "aws_access_key_id" {}
variable "aws_secret_access_key" {}

resource "aws_vpc" "myapp-vpc" {
cidr_block = var.vpc_cidr_block
tags = {
Name: "${var.env_prefix}-vpc"
}
}

resource "aws_subnet" "myapp-subnet-1" {
vpc_id = aws_vpc.myapp-vpc.id
cidr_block = var.subnet_cidr_block
availability_zone = var.avail_zone
tags = {
Name: "${var.env_prefix}-subnet-1"
}
}

resource "aws_route_table" "myapp-route-table" {
vpc_id = aws_vpc.myapp-vpc.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.myapp-igw.id
}
tags = {
Name: "${var.env_prefix}-rtb"
}
}

resource "aws_internet_gateway" "myapp-igw" {
vpc_id = aws_vpc.myapp-vpc.id
tags = {
Name: "${var.env_prefix}-igw"
}
}

resource "aws_route_table_association" "a-rtb-subnet" {
subnet_id = aws_subnet.myapp-subnet-1.id
route_table_id = aws_route_table.myapp-route-table.id
}

resource "aws_security_group" "myapp-sg" {
name = "myapp-sg"
vpc_id = aws_vpc.myapp-vpc.id

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 1883
to_port = 1883
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
prefix_list_ids = []
}

tags = {
Name: "${var.env_prefix}-sg"
}
}

data "aws_ami" "latest-amazon-linux-image" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["al2023-ami-ecs-hvm-*-arm64"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}

output "aws_ami_id" {
value = data.aws_ami.latest-amazon-linux-image.id
}

output "ec2_public_ip" {
value = aws_instance.myapp-server.public_ip
}

resource "aws_key_pair" "ssh-key" {
key_name = "server-key"
public_key = file(var.public_key_location)
}

resource "aws_instance" "myapp-server" {
ami = data.aws_ami.latest-amazon-linux-image.id
instance_type = var.instance_type

subnet_id = aws_subnet.myapp-subnet-1.id
vpc_security_group_ids = [aws_security_group.myapp-sg.id]
availability_zone = var.avail_zone

associate_public_ip_address = true
key_name = aws_key_pair.ssh-key.key_name

# user_data = file("entry-script.sh")

user_data = templatefile("entry-template.tpl", {
mqtt_username = var.mqtt_username
mqtt_password = var.mqtt_password
mqtt_listener = var.mqtt_listener
dynamo_endpoint = var.dynamo_endpoint
aws_region = var.aws_region
aws_access_key_id = var.aws_access_key_id
aws_secret_access_key = var.aws_secret_access_key
})

tags = {
Name: "${var.env_prefix}-server"
}
}
8 changes: 8 additions & 0 deletions devops/terraform/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.65.0"
}
}
}

0 comments on commit ceb925b

Please sign in to comment.