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-2 #6

Merged
merged 2 commits into from
Dec 14, 2023
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,24 @@ testapp_port = 9292

**Создан балансировщик lb.tf
**Созданы инстансы через Count

#Домашнее задание №7
1.Удалены из папки terraform файлы main.tf, outputs.tf,
terraform.tfvars, variables.tf, так как они теперь перенесены
в stage и prod
2.Параметризирована конфигурация модулей
нужным
3.Отформатированы конфигурационные файлы, используя команду
terraform fmt

*
1.Настроено храниение стрейт файла в удаленном бекенде для stage и prod, использован Yandex Object Stprage в качестве бекенда.
Описание бекенда вынесено в файл beckend.tf
2.Terraform видит текущее состояние, если перенести конфигурационные файлы.
3.Проверена работа блокировок при одновременном запуске.
4.Добавлено описание.

**
1.Добавлены необходимые provisioner в модули для деплоя и работы приложения. Файлы находятся в директории модуля.
2.Отключение provisioner не реализовано.
3.Добавлено описание.
23 changes: 23 additions & 0 deletions packer/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"builders": [
{
"type": "yandex",
"service_account_key_file": "{{user `service_account_key_file`}}",
"folder_id": "{{user `folder_id`}}",
"source_image_family": "{{user `source_image`}}",
"image_name": "reddit-app-base-{{timestamp}}",
"image_family": "reddit-app-base",
"ssh_username": "{{user `ssh_username`}}",
"platform_id": "{{user `platform_id`}}",
"image_description" : "{user `image_description`}}",
"use_ipv4_nat": true
}
],
"provisioners": [
{
"type": "shell",
"script": "scripts/install_ruby.sh",
"execute_command": "sudo {{.Path}}"
}
]
}
23 changes: 23 additions & 0 deletions packer/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"builders": [
{
"type": "yandex",
"service_account_key_file": "{{user `service_account_key_file`}}",
"folder_id": "{{user `folder_id`}}",
"source_image_family": "{{user `source_image`}}",
"image_name": "reddit-db-base-{{timestamp}}",
"image_family": "reddit-db-base",
"ssh_username": "{{user `ssh_username`}}",
"platform_id": "{{user `platform_id`}}",
"image_description" : "{user `image_description`}}",
"use_ipv4_nat": true
}
],
"provisioners": [
{
"type": "shell",
"script": "scripts/install_mongodb.sh",
"execute_command": "sudo {{.Path}}"
}
]
}
6 changes: 3 additions & 3 deletions terraform/.terraform.lock.hcl

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

52 changes: 52 additions & 0 deletions terraform/bucket.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#Создание бакета в Яндекс
terraform {
required_version = ">= 0.13.0"
required_providers {
yandex = {
source = "yandex-cloud/yandex"
version = ">= 0.35.0"
}
}
backend "s3" {
endpoint = "storage.yandexcloud.net"
bucket = "otus-bucket77"
region = "ru-central1"
key = "terraform.tfstate"

skip_region_validation = true
skip_credentials_validation = true
}
}

provider "yandex" {
token = var.token
cloud_id = var.cloud_id
folder_id = var.folder_id
zone = var.zone
}
#Создание сервисного аккаунта для бакета
#resource "yandex_iam_service_account" "sa" {
# name = "bucket-usr"
#}

# Назначение роли сервисному аккаунту
#resource "yandex_resourcemanager_folder_iam_member" "sa-editor" {
# folder_id = var.folder_id
# role = "storage.editor"
# member = "serviceAccount:${yandex_iam_service_account.sa.id}"
#}

# Создание статического ключа доступа
#resource "yandex_iam_service_account_static_access_key" "sa-static-key" {
# service_account_id = "ajemb8vlrh354jgttdj7"
# description = "static access key for object storage"
#}

#Создание бакета с использованием ключа
#resource "yandex_storage_bucket" "otus-bucket77" {
# access_key = "YCAJEuzaiGtOQqhmCFtn9qvJ_"
# secret_key = "YCOk7MScJuPR8m8St2uErvVWxNsWlzmmR3RvnKL4"
# bucket = "otus-bucket77"
# key = "terraform.tfstate"
# source = "/home/alex/otus/AlexUnderGo_infra/terraform/"
#}
File renamed without changes.
73 changes: 0 additions & 73 deletions terraform/main.tf

This file was deleted.

11 changes: 11 additions & 0 deletions terraform/modules/app/files/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
sleep 1m
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
13 changes: 13 additions & 0 deletions terraform/modules/app/files/puma.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[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
54 changes: 54 additions & 0 deletions terraform/modules/app/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
resource "yandex_compute_instance" "app" {

name = "reddit-app"

labels = {
tags = "reddit-app"
}
resources {
cores = 2
memory = 2
core_fraction = 20
}

boot_disk {
initialize_params {
image_id = var.app_disk_image
}
}

network_interface {
subnet_id = var.subnet_id
nat = true
}

metadata = {
ssh-keys = "ubuntu:${file(var.public_key_path)}"
}

connection {
type = "ssh"
host = self.network_interface[0].nat_ip_address
user = "ubuntu"
agent = true
# путь до приватного ключа (Не используется. Подключаюсь через agent ssh)
# private_key = var.private_key
}

provisioner "remote-exec" {
inline = [
# "echo \"export DATABASE_URL=${var.db_url}\" >> /home/ubuntu/.bashrc",
"sudo systemctl set-environment DATABASE_URL=${var.db_url}",
# "source /home/ubuntu/.bashrc"
]
}

provisioner "file" {
source = "${path.module}/files/puma.service"
destination = "/tmp/puma.service"
}
provisioner "remote-exec" {
script = "${path.module}/files/deploy.sh"
}

}
3 changes: 3 additions & 0 deletions terraform/modules/app/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
}
14 changes: 14 additions & 0 deletions terraform/modules/app/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
variable "public_key_path" {
description = "Path to the public key used for ssh access"
}
variable "app_disk_image" {
description = "Disk image for reddit app"
default = "reddit-app-base"
}
variable "subnet_id" {
description = "Subnets for modules"
}
variable "db_url" {
description = "Url for DB mongo"
}

9 changes: 9 additions & 0 deletions terraform/modules/app/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 0.13.0"
required_providers {
yandex = {
source = "yandex-cloud/yandex"
version = ">= 0.35.0"
}
}
}
46 changes: 46 additions & 0 deletions terraform/modules/db/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
resource "yandex_compute_instance" "db" {
# provider = yandex

name = "reddit-db"
labels = {
tags = "reddit-db"
}

resources {
cores = 2
memory = 2
core_fraction = 20
}

boot_disk {
initialize_params {
image_id = var.db_disk_image
}
}

network_interface {
subnet_id = var.subnet_id
nat = true
}

metadata = {
ssh-keys = "ubuntu:${file(var.public_key_path)}"
}
#Connect to VM
connection {
type = "ssh"
host = self.network_interface[0].nat_ip_address
user = "ubuntu"
agent = true
# путь до приватного ключа (Не используется. Подключаюсь через agent ssh)
# private_key = var.private_key
}
#Change mongod conf for use external IP
provisioner "remote-exec" {
inline = [
"sudo sed -i 's/bindIp: 127.0.0.1/bindIp: 0.0.0.0/' /etc/mongod.conf",
"sudo systemctl restart mongod",
]
}

}
3 changes: 3 additions & 0 deletions terraform/modules/db/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "external_ip_address_db" {
value = yandex_compute_instance.db.network_interface.0.nat_ip_address
}
10 changes: 10 additions & 0 deletions terraform/modules/db/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable public_key_path {
description = "Path to the public key used for ssh access"
}
variable db_disk_image {
description = "Disk image for reddit db"
default = "reddit-db-base"
}
variable subnet_id {
description = "Subnets for modules"
}
9 changes: 9 additions & 0 deletions terraform/modules/db/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 0.13.0"
required_providers {
yandex = {
source = "yandex-cloud/yandex"
version = ">= 0.35.0"
}
}
}
Loading
Loading