First implementation of terraform setup

This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-21 13:45:41 -03:00
commit 517a48f885
5 changed files with 184 additions and 0 deletions

View file

@ -0,0 +1,24 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "5.13.1"
hashes = [
"h1:T/p3Gp8uAvRk3BmBZI/B7JIUpxF+2DygvBsv2UvJK4w=",
"zh:0d107e410ecfbd5d2fb5ff9793f88e2ce03ae5b3bda4e3b772b5d146cdd859d8",
"zh:1080cf6a402939ec4ad393380f2ab2dfdc0e175903e08ed796aa22eb95868847",
"zh:300420d642c3ada48cfe633444eafa7bcd410cd6a8503de2384f14ac54dc3ce3",
"zh:4e0121014a8d6ef0b1ab4634877545737bb54e951340f1b67ffea8cd22b2d252",
"zh:59b401bbf95dc8c6bea58085ff286543380f176271251193eac09cb7fcf619b7",
"zh:5dfaf51e979131710ce8e1572e6012564e68c7c842e3d9caaaeb0fe6af15c351",
"zh:84bb75dafca056d7c3783be5185187fdd3294f902e9d72f7655f2efb5e066650",
"zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",
"zh:aa4e2b9f699d497041679bc05ca34ac21a5184298eb1813f35455b1883977910",
"zh:b51a4f08d84b071128df68a95cfa5114301a50bf8ab8e655dcf7e384e5bc6458",
"zh:bce284ac6ebb65053d9b703048e3157bf4175782ea9dbaec9f64dc2b6fcefb0c",
"zh:c748f78b79b354794098b06b18a02aefdb49be144dff8876c9204083980f7de0",
"zh:ee69d9aef5ca532392cdc26499096f3fa6e55f8622387f78194ebfaadb796aef",
"zh:ef561bee58e4976474bc056649095737fa3b2bcb74602032415d770bfc620c1f",
"zh:f696d8416c57c31f144d432779ba34764560a95937db3bb3dd2892a791a6d5a7",
]
}

View file

@ -0,0 +1,69 @@
provider "aws" {
region = "us-east-1" # Choose the region as needed
}
module "docker-swarm" {
source = "./modules/docker-swarm"
key_name = aws_key_pair.swarm-key.key_name
vpc_id = aws_vpc.swarm-vpc.id
subnet_id = aws_subnet.swarm-subnet.id
security_group = aws_security_group.swarm-sg.id
instance_type = "t2.micro" # Choose the instance type as needed
manager_count = 1
worker_count = 10 # This is the number of services in the docker-compose.yml file
}
resource "aws_key_pair" "swarm-key" {
key_name = "swarm-key"
public_key = file("~/.ssh/id_rsa.pub")
}
resource "aws_vpc" "swarm-vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
}
resource "aws_subnet" "swarm-subnet" {
vpc_id = aws_vpc.swarm-vpc.id
cidr_block = "10.0.1.0/24"
}
resource "aws_security_group" "swarm-sg" {
vpc_id = aws_vpc.swarm-vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 2376
to_port = 2377
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 7946
to_port = 7946
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 4789
to_port = 4789
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

View file

@ -0,0 +1,64 @@
variable "key_name" {}
variable "vpc_id" {}
variable "subnet_id" {}
variable "security_group" {}
variable "instance_type" {}
variable "manager_count" {}
variable "worker_count" {}
resource "aws_instance" "manager" {
count = var.manager_count
ami = "ami-08a52ddb321b32a8c" # Amazon Linux 2 LTS
instance_type = var.instance_type
key_name = var.key_name
vpc_security_group_ids = [var.security_group]
subnet_id = var.subnet_id
associate_public_ip_address = true
user_data = <<-EOT
#!/bin/bash
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo chkconfig docker on
docker swarm init --advertise-addr $(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
EOT
tags = {
Name = "manager-${count.index}"
}
}
resource "aws_instance" "worker" {
count = var.worker_count
ami = "ami-08a52ddb321b32a8c" # Amazon Linux 2 LTS
instance_type = var.instance_type
key_name = var.key_name
vpc_security_group_ids = [var.security_group]
subnet_id = var.subnet_id
associate_public_ip_address = true
user_data = <<-EOT
#!/bin/bash
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo chkconfig docker on
docker swarm join --token $(curl -s http://${aws_instance.manager.0.public_ip}:8080/token) ${aws_instance.manager.0.private_ip}:2377
EOT
tags = {
Name = "worker-${count.index}"
}
}
output "manager_public_ips" {
value = aws_instance.manager[*].public_ip
}
output "worker_public_ips" {
value = aws_instance.worker[*].public_ip
}

View file

@ -0,0 +1,8 @@
output "manager_public_ips" {
value = module.docker-swarm.manager_public_ips
}
output "worker_public_ips" {
value = module.docker-swarm.worker_public_ips
}

View file

@ -0,0 +1,19 @@
variable "region" {
description = "The AWS region"
default = "us-east-1"
}
variable "instance_type" {
description = "EC2 instance type"
default = "t2.micro"
}
variable "manager_count" {
description = "Number of manager nodes"
default = 1
}
variable "worker_count" {
description = "Number of worker nodes"
default = 3
}