Adds Project EC2 Instances to Terraform

This commit is contained in:
2023-09-22 00:51:39 -03:00
parent a037a3a3f1
commit f4e2044058
2 changed files with 109 additions and 6 deletions

1
setup_key.sh Normal file
View File

@@ -0,0 +1 @@
echo -e "\n${extra_key}" >> /home/ubuntu/.ssh/authorized_keys

View File

@@ -1,14 +1,15 @@
### SET VARIABLES ### SET VARIABLES
variable "project_name" {
type = string
default = "hideyoshi-portifolio"
}
variable "aws_region" { variable "aws_region" {
type = string type = string
default = "sa-east-1" default = "sa-east-1"
} }
variable "s3_bucket_name" {
type = string
}
variable "aws_access_key" { variable "aws_access_key" {
type = string type = string
} }
@@ -21,6 +22,20 @@ variable "project_domain" {
type = string type = string
} }
variable "ssh_public_key_main" {
type = string
}
variable "ssh_public_key_ci_cd" {
type = string
}
variable "number_of_workers" {
type = number
default = 2
}
### PROVIDER ### PROVIDER
provider "aws" { provider "aws" {
@@ -35,7 +50,7 @@ provider "aws" {
# S3 Bucket # S3 Bucket
resource "aws_s3_bucket" "default" { resource "aws_s3_bucket" "default" {
bucket = var.s3_bucket_name bucket = "${var.project_name}-bucket"
} }
resource "aws_s3_bucket_public_access_block" "bucket_public_disabled" { resource "aws_s3_bucket_public_access_block" "bucket_public_disabled" {
@@ -74,7 +89,6 @@ resource "aws_s3_bucket_policy" "default" {
POLICY POLICY
} }
resource "aws_s3_bucket_cors_configuration" "default" { resource "aws_s3_bucket_cors_configuration" "default" {
bucket = aws_s3_bucket.default.bucket bucket = aws_s3_bucket.default.bucket
@@ -86,6 +100,94 @@ resource "aws_s3_bucket_cors_configuration" "default" {
} }
# EC2 Instances
resource "aws_key_pair" "ssh_key_main" {
key_name = "ssh_key_main"
public_key = var.ssh_public_key_main
}
resource "aws_key_pair" "ssh_key_ci_cd" {
key_name = "ssh_key_ci_cd"
public_key = var.ssh_public_key_ci_cd
}
locals {
ports_in = [
22,
80,
443,
6443,
10250
]
ports_out = [
0,
]
}
resource "aws_security_group" "project_pool" {
name = "${var.project_name}_pool_security_group"
description = "Security group for project pool"
dynamic "egress" {
for_each = toset(local.ports_out)
content {
from_port = egress.value
to_port = egress.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
dynamic "ingress" {
for_each = toset(local.ports_in)
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
}
resource "aws_instance" "main" {
ami = "ami-0af6e9042ea5a4e3e"
instance_type = "t2.micro"
vpc_security_group_ids = [ aws_security_group.project_pool.id ]
count = 1
key_name = aws_key_pair.ssh_key_main.key_name
user_data = templatefile("${path.module}/setup_key.sh", {
extra_key = aws_key_pair.ssh_key_ci_cd.public_key
})
tags = {
Name = "${var.project_name}-main"
}
}
resource "aws_instance" "worker" {
ami = "ami-0af6e9042ea5a4e3e"
instance_type = "t2.micro"
vpc_security_group_ids = [ aws_security_group.project_pool.id ]
count = 1
key_name = aws_key_pair.ssh_key_main.key_name
user_data = templatefile("${path.module}/setup_key.sh", {
extra_key = aws_key_pair.ssh_key_ci_cd.public_key
})
tags = {
Name = "${var.project_name}-worker"
}
}
### OUTPUTS
output "bucker_domain_name" { output "bucker_domain_name" {
value = aws_s3_bucket.default.bucket_domain_name value = aws_s3_bucket.default.bucket_domain_name
} }