Работа с AWS ELB и Terraform в Unix/Linux

Работа с AWS ELB и Terraform в Unix/Linux

Elastic Load Balancing — «резиновый лоад балансер», который  автоматически распределяет входящий трафик приложений на несколько нод ( например, на экземпляры EC2). Он контролирует хелз_чеки хостов и направляет трафик только на «здоровые» ноды. ELB поддерживает три типа балансиров нагрузки:

  • Application Load Balancers (ALB).
  • Network Load Balancers (NLB).
  • Classic Load Balancers (CLB или ELB).

Установка terraform в Unix/Linux

Установка крайне примитивная и я описал как это можно сделать тут:

Установка terraform в Unix/Linux

Так же, в данной статье, я создал скрипт для автоматической установки данного ПО. Он был протестирован на CentOS 6/7, Debian 8 и на Mac OS X. Все работает должным образом!

Чтобы получить помощь по использованию команд, выполните:

$ terraform
Usage: terraform [--version] [--help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    push               Upload this Terraform module to Atlas to run
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    state              Advanced state management

Приступим к использованию!

Работа с AWS ELB и Terraform в Unix/Linux

У меня есть папка terraform, в ней у меня будут лежать провайдеры с которыми я буду работать. Т.к в этом примере я буду использовать AWS, то создам данную папку и перейду в нее. Далее, в этой папке, стоит создать:

$ mkdir examples modules

В папке examples, я буду хранить так званые «плейбуки» для разварачивания различных служб, например — zabbix-server, grafana, web-серверы и так далее. В modules директории, я буду хранить все необходимые модули.

Начнем писать модуль, но для этой задачи, я создам папку:

$ mkdir modules/elb

Переходим в нее:

$ cd modules/elb

Открываем файл:

$ vim elb.tf

В данный файл, вставляем:

#---------------------------------------------------
# Create AWS ELB
#---------------------------------------------------
resource "aws_elb" "elb" {
    name                = "${lower(var.name)}-elb-${lower(var.environment)}"
    #availability_zones  = ["${split(",", (lookup(var.availability_zones, var.region)))}"] #["us-east-1a", "us-east-1b"]
    security_groups     = ["${var.security_groups}"]
    subnets             = ["${var.subnets}"]
    internal            = "${var.elb_internal}"

    cross_zone_load_balancing   = "${var.cross_zone_load_balancing}"
    idle_timeout                = "${var.idle_timeout}"
    connection_draining         = "${var.connection_draining}"
    connection_draining_timeout = "${var.connection_draining_timeout}"

    access_logs                 = ["${var.access_logs}"]
    listener                    = ["${var.listener}"]
    health_check                = ["${var.health_check}"]

    tags {
        Name            = "${lower(var.name)}-elb-${lower(var.environment)}"
        Environment     = "${var.environment}"
        Orchestration   = "${var.orchestration}"
        Createdby       = "${var.createdby}"
    }
}
#---------------------------------------------------
# ELB attachment
#---------------------------------------------------
resource "aws_elb_attachment" "elb_attachment" {
    count       = "${length(var.instances)}"

    elb         = "${aws_elb.elb.name}"
    instance    = "${element(var.instances, count.index)}"

    depends_on  = ["aws_elb.elb"]
}
#---------------------------------------------------
# Add LB cookie stickiness policy
#---------------------------------------------------
resource "aws_lb_cookie_stickiness_policy" "lb_cookie_stickiness_policy_http" {
    count                       = "${var.enable_lb_cookie_stickiness_policy_http ? 1 : 0}"

    name                        = "${lower(var.name)}-lb-cookie-stickiness-policy-http-${lower(var.environment)}"
    load_balancer               = "${aws_elb.elb.id}"
    lb_port                     = "${var.http_lb_port}"
    cookie_expiration_period    = "${var.cookie_expiration_period}"

    depends_on                  = ["aws_elb.elb"]
}
resource "aws_lb_cookie_stickiness_policy" "lb_cookie_stickiness_policy_https" {
    count                       = "${var.enable_lb_cookie_stickiness_policy_http ? 0 : 1}"

    name                        = "${lower(var.name)}-lb_cookie-stickiness-policy-https-${lower(var.environment)}"
    load_balancer               = "${aws_elb.elb.id}"
    lb_port                     = "${var.https_lb_port}"
    cookie_expiration_period    = "${var.cookie_expiration_period}"

    depends_on                  = ["aws_elb.elb"]
}
#---------------------------------------------------
#Add APP cookie stickiness policy
#---------------------------------------------------
resource "aws_app_cookie_stickiness_policy" "app_cookie_stickiness_policy_http" {
    count                    = "${var.enable_app_cookie_stickiness_policy_http ? 1 : 0}"

    name                     = "${lower(var.name)}-app-cookie-stickiness-policy-http-${lower(var.environment)}"
    load_balancer            = "${aws_elb.elb.id}"
    lb_port                  = "${var.http_lb_port}"
    cookie_name              = "${var.cookie_name}"

    depends_on               = ["aws_elb.elb"]
}
resource "aws_app_cookie_stickiness_policy" "app_cookie_stickiness_policy_https" {
    count                    = "${var.enable_app_cookie_stickiness_policy_http ? 0 : 1}"

    name                     = "${lower(var.name)}-app-cookie-stickiness-policy-https-${lower(var.environment)}"
    load_balancer            = "${aws_elb.elb.id}"
    lb_port                  = "${var.https_lb_port}"
    cookie_name              = "${var.cookie_name}"

    depends_on               = ["aws_elb.elb"]
}

Открываем файл:

$ vim variables.tf

И прописываем:

#-----------------------------------------------------------
# Global or/and default variables
#-----------------------------------------------------------
variable "name" {
  description = "Name to be used on all resources as prefix"
  default     = "TEST-ELB"
}

variable "region" {
  description = "The region where to deploy this code (e.g. us-east-1)."
  default     = "us-east-1"
}

variable "environment" {
    description = "Environment for service"
    default     = "STAGE"
}

variable "orchestration" {
    description = "Type of orchestration"
    default     = "Terraform"
}

variable "createdby" {
    description = "Created by"
    default     = "Vitaliy Natarov"
}

# Create ELB
variable "availability_zones" {
    description = "Availability zones for AWS ASG"
    type        = "map"
    default     = {
        us-east-1      = "us-east-1b,us-east-1c,us-east-1d,us-east-1e"
        us-east-2      = "us-east-2a,eu-east-2b,eu-east-2c"
        us-west-1      = "us-west-1a,us-west-1c"
        us-west-2      = "us-west-2a,us-west-2b,us-west-2c"
        ca-central-1   = "ca-central-1a,ca-central-1b"
        eu-west-1      = "eu-west-1a,eu-west-1b,eu-west-1c"
        eu-west-2      = "eu-west-2a,eu-west-2b"
        eu-central-1   = "eu-central-1a,eu-central-1b,eu-central-1c"
        ap-south-1     = "ap-south-1a,ap-south-1b"
        sa-east-1      = "sa-east-1a,sa-east-1c"
        ap-northeast-1 = "ap-northeast-1a,ap-northeast-1c"
        ap-southeast-1 = "ap-southeast-1a,ap-southeast-1b"
        ap-southeast-2 = "ap-southeast-2a,ap-southeast-2b,ap-southeast-2c"
        ap-northeast-1 = "ap-northeast-1a,ap-northeast-1c"
        ap-northeast-2 = "ap-northeast-2a,ap-northeast-2c"
    }
}

variable "security_groups" {
    description = "A list of security group IDs to assign to the ELB. Only valid if creating an ELB within a VPC"
    type        = "list"
    #default     = []
}

variable "subnets" {
    description = "A list of subnet IDs to attach to the ELB"
    type        = "list"
    default     = []
}

variable "instances" {
    description = " Instances ID to add them to ELB"
    type        = "list"
    default     = []
}

variable "elb_internal" {
    description = "If true, ELB will be an internal ELB"
    default     = false
}

variable "cross_zone_load_balancing" {
    description = "Enable cross-zone load balancing. Default: true"
    default     = true
}

variable "idle_timeout" {
    description = "The time in seconds that the connection is allowed to be idle. Default: 60"
    default     = "60"
}

variable "connection_draining" {
    description = "Boolean to enable connection draining. Default: false"
    default     = false
}

variable "connection_draining_timeout" {
    description = "The time in seconds to allow for connections to drain. Default: 300"
    default     = 300
}

# Access logs
variable "access_logs" {
    description = "An access logs block. Uploads access logs to S3 bucket"
    type        = "list"
    default     = []
}

# Listener
variable "listener" {
    description = "A list of Listener block"
    type        = "list"
}

# Health Check
variable "health_check" {
    description = " Health check"
    type        = "list"
}

variable "enable_lb_cookie_stickiness_policy_http" {
    description = "Enable lb cookie stickiness policy http. If set true, will add it, else will use https"
    default     = "true"
}

variable "enable_app_cookie_stickiness_policy_http" {
    description = "Enable app cookie stickiness policy http. If set true, will add it, else will use https"
    default     = "true"
}

variable "http_lb_port" {
    description = "Set http lb port for lb_cookie_stickiness_policy_http|app_cookie_stickiness_policy_http policies"
    default     = "80"
}

variable "https_lb_port" {
    description = "Set https lb port for lb_cookie_stickiness_policy_http|app_cookie_stickiness_policy_http policies"
    default     = "443"
}

variable "cookie_expiration_period" {
    description = "Set cookie expiration period"
    default     = 600
}

variable "cookie_name" {
    description = "Set cookie name"
    default     = "SessionID"
}

Собственно в этом файле храняться все переменные. Спасибо кэп!

Открываем последний файл:

$ vim outputs.tf

и в него вставить нужно следующие строки:

# ELB
output "elb_id" {
  description = "The name of the ELB"
  value       = "${aws_elb.elb.id}"
}

output "elb_name" {
  description = "The name of the ELB"
  value       = "${aws_elb.elb.name}"
}

output "elb_dns_name" {
  description = "The DNS name of the ELB"
  value       = "${aws_elb.elb.dns_name}"
}

output "elb_instances" {
  description = "The list of instances in the ELB"
  value       = ["${aws_elb.elb.instances}"]
}

output "elb_source_security_group_id" {
  description = "The ID of the security group that you can use as part of your inbound rules for your load balancer's back-end application instances"
  value       = "${aws_elb.elb.source_security_group_id}"
}

output "elb_zone_id" {
  description = "The canonical hosted zone ID of the ELB (to be used in a Route 53 Alias record)"
  value       = "${aws_elb.elb.zone_id}"
}

Переходим теперь в папку aws/examples и создадим еще одну папку для проверки написанного чуда:

$ mkdir elb && cd $_

Внутри созданной папки открываем файл:

$ vim main.tf

И вставим в него следующий код:

#
# MAINTAINER Vitaliy Natarov "vitaliy.natarov@yahoo.com"
#
terraform {
  required_version = "> 0.9.0"
}
provider "aws" {
    region  = "us-east-1"
    # Make it faster by skipping something
    #skip_get_ec2_platforms      = true
    #skip_metadata_api_check     = true
    #skip_region_validation      = true
    #skip_credentials_validation = true
    #skip_requesting_account_id  = true
}
module "iam" {
    source                          = "../../modules/iam"
    name                            = "TEST-AIM"
    region                          = "us-east-1"
    environment                     = "PROD"

    aws_iam_role-principals         = [
        "ec2.amazonaws.com",
    ]
    aws_iam_policy-actions           = [
        "cloudwatch:GetMetricStatistics",
        "logs:DescribeLogStreams",
        "logs:GetLogEvents",
        "elasticache:Describe*",
        "rds:Describe*",
        "rds:ListTagsForResource",
        "ec2:DescribeAccountAttributes",
        "ec2:DescribeAvailabilityZones",
        "ec2:DescribeSecurityGroups",
        "ec2:DescribeVpcs",
        "ec2:Owner",
    ]
}
module "vpc" {
    source                              = "../../modules/vpc"
    name                                = "TEST-VPC"
    environment                         = "PROD"
    # VPC
    instance_tenancy                    = "default"
    enable_dns_support                  = "true"
    enable_dns_hostnames                = "true"
    assign_generated_ipv6_cidr_block    = "false"
    enable_classiclink                  = "false"
    vpc_cidr                            = "172.31.0.0/16"
    private_subnet_cidrs                = ["172.31.64.0/20"]
    public_subnet_cidrs                 = ["172.31.0.0/20"]
    availability_zones                  = ["us-east-1a", "us-east-1b"]
    enable_all_egress_ports             = "true"
    allowed_ports                       = ["9300", "3272", "8888", "8444"]

    map_public_ip_on_launch             = "true"

    #Internet-GateWay
    enable_internet_gateway             = "true"
    #NAT
    enable_nat_gateway                  = "false"
    single_nat_gateway                  = "false"
    #VPN
    enable_vpn_gateway                  = "false"
    #DHCP
    enable_dhcp_options                 = "false"
    # EIP
    enable_eip                          = "false"
}
module "elb" {
    source                              = "../../modules/elb"
    name                                = "TEST-ELB"
    region                              = "us-east-1"
    environment                         = "PROD"

    security_groups                     = ["${module.vpc.security_group_id}"]

    # Need to choose subnets or availability_zones. The subnets has been chosen.
    subnets                             = ["${element(module.vpc.vpc-publicsubnet-ids, 0)}"]

    #access_logs = [
    #    {
    #        bucket = "my-access-logs-bucket"
    #        bucket_prefix = "bar"
    #        interval = 60
    #    },
    #]
    listener = [
        {
            instance_port     = "80"
            instance_protocol = "HTTP"
            lb_port           = "80"
            lb_protocol       = "HTTP"
        },
    #    {
    #        instance_port      = 443
    #        instance_protocol  = "https"
    #        lb_port            = 443
    #        lb_protocol        = "https"
    #        ssl_certificate_id = "${var.elb_certificate}"
    #    },
    ]
    health_check = [
        {
            target              = "HTTP:80/"
            interval            = 30
            healthy_threshold   = 2
            unhealthy_threshold = 2
            timeout             = 5
        }
    ]
    # You could use ONE health_check!
    #health_check = [
    #    {
    #        target              = "HTTP:443/"
    #        interval            = 30
    #        healthy_threshold   = 2
    #        unhealthy_threshold = 2
    #        timeout             = 5
    #    }
    #]
    # Enable
    enable_lb_cookie_stickiness_policy_http  = true
    # Enable
    enable_app_cookie_stickiness_policy_http = "true"
}

PS: Тут имеются вспомогательные модули. Я описывал работу тут:

Работа с AWS IAM и Terraform в Unix/Linux

Работа с AWS VPC и Terraform в Unix/Linux

Работа с AWS EC2 и Terraform в Unix/Linux

Вот может еще пригодится:

Работа с AWS ASG(auto scaling group) и Terraform в Unix/Linux

Работа с AWS S3 и Terraform в Unix/Linux

Работа с AWS RDS и Terraform в Unix/Linux

Работа с AWS Route53 и Terraform в Unix/Linux

Все уже написано и готово к использованию. Ну что, начнем тестирование. В папке с вашим плейбуком, выполняем:

$ terraform init

Этим действием я инициализирую проект. Затем, подтягиваю модуль:

$ terraform get

PS: Для обновление изменений в самом модуле, можно выполнять:

$ terraform get -update

Запускем прогон:

$ terraform plan

Мне вывело что все у меня хорошо и можно запускать деплой:

$ terraform apply

Как видно с вывода, — все прошло гладко! Чтобы удалить созданное творение, можно выполнить:

$ terraform destroy

Весь материал аплоаджу в github аккаунт для удобства использования:

$ git clone https://github.com/SebastianUA/terraform.git

Вот и все на этом. Данная статья «Работа с AWS ELB и Terraform в Unix/Linux» завершена.

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *