Дипломная работа: Автоматизация доставки программного обеспечения при помощи DevOps практик и инструментов в облаке AWS в компании ООО "Команда Лабс"

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам
131
}
resource "aws_security_group" "nginx" {
name = "nginx_sec_group_public"
description = "Security group for backend servers and private ELBs"
vpc_id = "${aws_vpc.vpc_main.id}"
# SSH access from anywhere
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTPS access from anywhere
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTP access from the anywhere
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/16"]
}
# Allow all from private subnet
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["${aws_subnet.private1.cidr_block}"]
}
# Outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
module "bastion" {
source = "bastion"
subnet_id = "${aws_subnet.public1.id}"
key_pair_id = "prod-eu-central-bastion-key"
security_group_id = "${aws_security_group.bastion.id}"
count = 1
group_name = "bastion"
132
}
module "nginx" {
source = "nginx"
subnet_id = "${aws_subnet.public1.id}"
key_pair_id = "prod-eu-central-bastion-keygit pu"
security_group_id = "${aws_security_group.nginx.id}"
// ami = "ami-0b4e8331acfc156ee"
count = 1
group_name = "nginx"
}
resource "aws_elb" "bastion" {
name = "elb-public-backend"
subnets = ["${aws_subnet.public1.id}", "${aws_subnet.public2.id}"]
security_groups = ["${aws_security_group.elb.id}"]
instances = ["${module.bastion.instance_ids}"]
listener {
instance_port = 22
instance_protocol = "TCP"
lb_port = 22
lb_protocol = "TCP"
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 3
target = "TCP:22"
interval = 30
}
}
# Public Frontend ELB
resource "aws_elb" "nginx" {
name = "elb-public-frontend"
subnets = ["${aws_subnet.public1.id}", "${aws_subnet.public2.id}"]
security_groups = ["${aws_security_group.elb.id}"]
instances = ["${module.nginx.instance_ids}"]
listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
//ssl_certificate_id = false
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
133
timeout = 3
target = "HTTP:80/healthcheck.php"
interval = 30
}
}
module "db" {
source = "rds"
identifier = "demodb"
engine = "postgres"
engine_version = "10.6"
instance_class = "db.t2.small"
allocated_storage = 5
storage_encrypted = false
multi_az = true
# kms_key_id = "arm:aws:kms:<region>:<account id>:key/<kms key id>"
name = "hapi"
username = "hapiuser"
password = "LongPassword"
port = "5432"
vpc_security_group_ids = ["${aws_security_group.elb.id}"]
maintenance_window = "Mon:00:00-Mon:03:00"
backup_window = "03:00-06:00"
# disable backups to create DB faster
backup_retention_period = 0
tags = {
Owner = "user"
Environment = "prod-eu"
Name = "${var.environment_name}-Postrges-DB-Instance"
VPC = "${aws_vpc.vpc_main.id}"
ManagedBy = "terraform"
Environment = "${var.environment_name}"
}
# DB subnet group
subnet_ids = ["${aws_subnet.private1.id}","${aws_subnet.private2.id}"]
# DB parameter group
family = "postgres10"
# DB option group
major_engine_version = "10.6"
# Snapshot name upon DB deletion
final_snapshot_identifier = "hapidb"
# Database Deletion Protection
deletion_protection = false
}
Модуль Terraform для Nginx сервера
resource "aws_instance" "instance" {
count = "${var.count}"
134
instance_type = "${var.instance_type}"
ami = "${lookup(var.aws_amis, var.aws_region)}"
key_name = "${var.key_pair_id}"
vpc_security_group_ids = ["${var.security_group_id}"]
subnet_id = "${var.subnet_id}"
root_block_device {
volume_size = "${var.disk_size}"
}
tags {
Name = "${format("%s%02d", var.group_name, count.index + 1)}" # -> "backend02"
Group = "${var.group_name}"
}
lifecycle {
create_before_destroy = true
}
# Provisioning
connection {
user = "ubuntu"
private_key = "${file(var.private_key_path)}"
agent = false
}
provisioner "remote-exec" {
inline = [
"sudo apt-get -y update",
]
}
}
Создание модуля Terraform для базы данных AWS RDS
locals {
db_subnet_group_name = "${coalesce(var.db_subnet_group_name,
module.db_subnet_group.this_db_subnet_group_id)}"
enable_create_db_subnet_group = "${var.db_subnet_group_name == "" ?
var.create_db_subnet_group : 0}"
parameter_group_name = "${coalesce(var.parameter_group_name,
module.db_parameter_group.this_db_parameter_group_id)}"
enable_create_db_parameter_group = "${var.parameter_group_name == "" ?
var.create_db_parameter_group : 0}"
option_group_name = "${coalesce(var.option_group_name,
module.db_option_group.this_db_option_group_id)}"
enable_create_db_option_group = "${var.option_group_name == "" && var.engine !=
"postgres" ? var.create_db_option_group : 0}"
}
module "db_subnet_group" {
135
source = "./modules/db_subnet_group"
create = "${local.enable_create_db_subnet_group}"
identifier = "${var.identifier}"
name_prefix = "${var.identifier}-"
subnet_ids = ["${var.subnet_ids}"]
tags = "${var.tags}"
}
module "db_parameter_group" {
source = "./modules/db_parameter_group"
create = "${local.enable_create_db_parameter_group}"
identifier = "${var.identifier}"
name_prefix = "${var.identifier}-"
family = "${var.family}"
parameters = ["${var.parameters}"]
tags = "${var.tags}"
}
module "db_option_group" {
source = "./modules/db_option_group"
create = "${local.enable_create_db_option_group}"
identifier = "${var.identifier}"
name_prefix = "${var.identifier}-"
option_group_description = "${var.option_group_description}"
engine_name = "${var.engine}"
major_engine_version = "${var.major_engine_version}"
options = ["${var.options}"]
tags = "${var.tags}"
}
module "db_instance" {
source = "./modules/db_instance"
create = "${var.create_db_instance}"
identifier = "${var.identifier}"
engine = "${var.engine}"
engine_version = "${var.engine_version}"
instance_class = "${var.instance_class}"
allocated_storage = "${var.allocated_storage}"
storage_type = "${var.storage_type}"
storage_encrypted = "${var.storage_encrypted}"
kms_key_id = "${var.kms_key_id}"
license_model = "${var.license_model}"
name = "${var.name}"
username = "${var.username}"
Источник: https://baza.diplomsite.ru/previewfile/1758