Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform 1 #5

Merged
merged 6 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
packer/variables.json
*.tfstate
*.tfstate.*.backup
*.tfstate.backup
*.tfvars
.terraform/
5 changes: 5 additions & 0 deletions terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.tfstate
*.tfstate.*.backup
*.tfstate.backup
*.tfvars
.terraform/
9 changes: 9 additions & 0 deletions terraform/.terraform.lock.hcl

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

10 changes: 10 additions & 0 deletions terraform/files/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e
APP_DIR=${1:-$HOME}
sudo apt-get install -y git
git clone -b monolith https://github.com/express42/reddit.git $APP_DIR/reddit
cd $APP_DIR/reddit
bundle install
sudo mv /tmp/puma.service /etc/systemd/system/puma.service
sudo systemctl start puma
sudo systemctl enable puma
8 changes: 8 additions & 0 deletions terraform/files/key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "ajegg8175h1q90thaise",
"service_account_id": "ajeo9k17dj25iuj6h5au",
"created_at": "2023-12-07T20:04:07.729402726Z",
"key_algorithm": "RSA_2048",
"public_key": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtQ6f1kCIJbJFcDeYXRnQ\n61/1j7RbT3OsFUaetHP3WEZUx4Tr2RzjnnOjLUVkRIjjU/0Y8VWGLQx2b3bmBZ4P\nbngq+WtySVei+jLVk8o+gpK4AriuX5J15G/CfuOBgrCaXZok/YSi7h8ni7TU1YF6\nkjmUV/3JRZPG08xuiZh5pvATcpTlLUMopWCdhhGoci4B0QjX2pPl1d27u/thdu0O\nLe5RZX0k9jhC6v3T/FMPNpMPxUxnctejxpz+OWGuOdW0MPJPKL50C5WlB58M2EPI\nMahfmnRLkLZV4Yr4fR6Si+cvZIYQNKWeUURTYRxlqLCcnf+9We6hf9MuylJP24rd\n/QIDAQAB\n-----END PUBLIC KEY-----\n",
"private_key": "PLEASE DO NOT REMOVE THIS LINE! Yandex.Cloud SA Key ID \u003cajegg8175h1q90thaise\u003e\n-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC1Dp/WQIglskVw\nN5hdGdDrX/WPtFtPc6wVRp60c/dYRlTHhOvZHOOec6MtRWREiONT/RjxVYYtDHZv\nduYFng9ueCr5a3JJV6L6MtWTyj6CkrgCuK5fknXkb8J+44GCsJpdmiT9hKLuHyeL\ntNTVgXqSOZRX/clFk8bTzG6JmHmm8BNylOUtQyilYJ2GEahyLgHRCNfak+XV3bu7\n+2F27Q4t7lFlfST2OELq/dP8Uw82kw/FTGdy16PGnP45Ya451bQw8k8ovnQLlaUH\nnwzYQ8gxqF+adEuQtlXhivh9HpKL5y9khhA0pZ5RRFNhHGWosJyd/71Z7qF/0y7K\nUk/bit39AgMBAAECggEAElaQsNoouqyzj29KGC/rsv4Co1NQpkZLKSfVyOWFVA8M\n5N0dKDM0CLyh9o1fNPkqZpDYLGVi/tVoJNewP48SoA09c7VtcL6WwWxRqTOO9kRX\nAouNucdd980BWum/GIC7pHxe1RX7N063f3/Jc3XhohrWirz9MaC16YYnXKSxZEt5\nU16N++Dxn1yIzqY3+guTRuURFD0hHtB2CqplLXrlu4dcDSmwL3yzvE63LyzqpgFn\nxIu5QYqRlEZdqVmSjmvVdL/+5hISmpVKOaQ3fyMIoO/gWj+y4+UcshM/3S9Mmd2J\nZShie3Vi2o7ttQCwe3bXzlNIc6ARlAwAN1onhMuqXwKBgQDOUVblOJJ4o/jKrbiN\nKUX8V5i1sfovbGpesOXqGLNm4+0U8942e29eeOu4GaX8ohT1Uu6eXb5MTwOEdoZR\nEczyL2PFAXHDw7d84iUhvxcl6bt9Ss67qm1Rf/FpMhTG5UevJ6aeQUTi6U/vlf5b\nUAIUD4qcn6iDGYv/oIkOyL004wKBgQDgqBHl3jIXigW8POWr2UQrtunBPcWh+UNa\ndFXIT84Ep93PNXxmnWjBitHUzFXn0aNfvyqBdcelZ2QGRV7rOvSZOzdNYEh6Y/Yd\nPBDWeIKj91b9SkpW/z6JS2TXXTj/JoZH8s6utLSYrCE+4ZfKOserJpzsrcitxglQ\nHILk4AD3nwKBgQDJI5tnlSOW4aTnUz2cwSQtwuhqGr0iBrwc3LEI7FPad1LnHmkB\nDI2iuJh1vuuHxCxSjyt60UF4+38eYL7JwmTfpzvJEINsqx3zbjVtFPLoT1ot6ARs\nCNbSob0lJWIEFfsw06MondEetv7rQHvdYb7k99HWqACM7xcBe+yisSlkqQKBgAV0\nLxMMhNzLbf424EB+wsfo5d3aGQ0olvkAMADv5wl35cuc+csn8R4Zt6RZ3ZAGJ9gw\nNOD1sgYS73uzfW1qffumU15egwPtn1DwX1xFiq5eV89IWWupQKRmnykXS0Xucn4W\np+vX2TaHnzr+kQfIy4YdJtwcD+4gC6PVPVw/D+bzAoGBAIG4hoYliH4zpZPoeu5p\nkGAvWtgL3N5iZ8A62Bwy1qb6U5QFyUuCYKOHfL/zB4OJzpg1cmzMJQgI8pkyCDAo\nVvYpQ5DvB571rBbzeRDBKbZmJIpiGW74ELEqJSlPCnnfApbZDflRaE/L7URTXB9f\n5FeFGlxGhsg5Mmwd0eBaSPHu\n-----END PRIVATE KEY-----\n"
}
14 changes: 14 additions & 0 deletions terraform/files/puma.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

[Unit]
Description=Puma HTTP Server
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/reddit
ExecStart=/bin/bash -lc 'puma'
Restart=always

[Install]
WantedBy=multi-user.target
55 changes: 55 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

#terraform {
# required_providers {
# yandex = {
# source = "yandex-cloud/yandex"
# }
# }
# required_version = ">= 0.13"
#}

provider "yandex" {
service_account_key_file = var.service_account_key_file
cloud_id = var.cloud_id
folder_id = var.folder_id
zone = var.zone
}
resource "yandex_compute_instance" "app" {
name = "reddit-app"
metadata = {
ssh-keys = "ubuntu:${file(var.public_key_path)}"
}
resources {
cores = 2
memory = 2
}

boot_disk {
initialize_params {
# Указать id образа созданного в предыдущем домашем задании
image_id = var.image_id
}
}

network_interface {
# Указан id подсети default-ru-central1-a
subnet_id = var.subnet_id
nat = true
}

connection {
type = "ssh"
host = yandex_compute_instance.app.network_interface.0.nat_ip_address
user = "ubuntu"
agent = false
# путь до приватного ключа
private_key = file(var.private_key_path)
}
provisioner "file" {
source = "files/puma.service"
destination = "/tmp/puma.service"
}
provisioner "remote-exec" {
script = "files/deploy.sh"
}
}
3 changes: 3 additions & 0 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "external_ip_address_app" {
value = yandex_compute_instance.app.network_interface.0.nat_ip_address
}
8 changes: 8 additions & 0 deletions terraform/terraform.tfvars.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cloud_id = "b1gmc6mhdddnpnljfrds"
folder_id = "b1getl3tudddfgfffogh"
zone = "ru-central1-a"
image_id = "fd8va10dftttt8sttttt"
public_key_path = "ubuntu.pub"
private_key_path = "ubuntu"
service_account_key_file = "files/key.json"
subnet_id = "e9bf8hprrrulk8tttttt"
38 changes: 38 additions & 0 deletions terraform/ubuntu
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1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WR9FR0J7ds23EklCthPmk0wl2wST
zlPUHeW9rvfSzj6DVR2m6Jf6rmazjNAAAAwQDlaKbSKA9/+2SDF6iSkkqvIFhkl6EHkHkz
WMxd7xpptJNVhw5O6LdK8qpG+0uM14TjG6GFycqsoc0rOOTzgryJVLAtUIREa3Rpi19ql0
M5tp5dPKZ4QAlagevAMmMvoKIzcAq+ejdB5V1EyV42XByzRufdj+xs6TrEMrZbxNIiefF5
VlgLTReu43A4uzE9r8sbZFbZewUZB5FPH6JQPvWt/dwaHv+lDlh7zL8/i5mQ1s2diS3fu3
4zt+PysMhi6DcAAAALdXNlckBkZXZvcHM=
-----END OPENSSH PRIVATE KEY-----
1 change: 1 addition & 0 deletions terraform/ubuntu.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCiwb4xSbNXh+SnS6DEoK5jWlXHMUcmcu8K4b/rWkNBmYyrSLvnzo1lUXtesKv6GMT82oElhShfNrz6BDUbasT61D4Ynlekhz726epzt8cE3oreI35ePpfvRUokhum3xxYTwtyOeZTX4k6dH+sRPXFpfFjv55EWa34t1FppUSqyrjoKBSvUtCCJH/Ho8MXfwWeNyfUG3R6GSdBTIcn5upOEmkYnBni805T6uZZJJLWs/8thfeZvzW01jQV40VShEBBH2KCUtejDrVLo7veZDjQNlWafqyEylYGhZ7Tfe2ZDale1OVYk+vcQzjgLwej1D9tPxXUlabx/YZ5DP9l8UQGWF0GCVh6Esj+lHfvbigz9W/Cwf9czobx+gsKK9IdlunBJakjTb8XbkpeKwNO7FgAkwaSv1AqWU0jb0wrWtAQ+Bk8YGpaiI7RXqYroqOwGbYPSJv8H8kXhIUtKke9Hw0Eq3CYeYK5kK+lhV7mhfS0klJq4wOdIJMdPbN0e5JL8/As= user@devops
27 changes: 27 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
variable "cloud_id" {
description = "Cloud"
}
variable "folder_id" {
description = "Folder"
}
variable "zone" {
description = "Zone"
# Значение по умолчанию
default = "ru-central1-a"
}
variable "public_key_path" {
# Описание переменной
description = "Path to the public key used for ssh access"
}
variable "private_key_path" {
description = "Path to the private key used for ssh access"
}
variable "image_id" {
description = "Disk image"
}
variable "subnet_id" {
description = "Subnet"
}
variable "service_account_key_file" {
description = "key .json"
}
Loading