diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 index a0dd221..8c17ca4 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,6 @@ -# Compiled files +# ignored files *.tfstate *.tfstate.backup - -# Module directory .terraform .idea -*.iml +*.iml \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..aa6e42c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,13 @@ +repos: + - repo: git://github.com/antonbabenko/pre-commit-terraform + rev: v1.12.0 + hooks: + - id: terraform_fmt + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.0.0 + hooks: + - id: check-merge-conflict + - id: trailing-whitespace + - id: check-yaml + - id: check-added-large-files diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..136d77d --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Cloud Drove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d9e69c6 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +export GENIE_PATH ?= $(shell 'pwd')/../../../genie + +include $(GENIE_PATH)/Makefile \ No newline at end of file diff --git a/README.md b/README.md old mode 100755 new mode 100644 index c4c1c26..3637ddb --- a/README.md +++ b/README.md @@ -1,24 +1,193 @@ -AWS ALB -============== + -This module is for create alb with related resources. +

-###### main.tf -contain resource code for alb. -###### variables.tf -contain variables declaration. all variables are empty by default. we will define or put value to variables in equls.tf (main file for infrastructure). these are input vars. +

+ Terraform AWS ALB +

-###### outputs.tf -is alse contain variables but we will these variables for output like ec2 instance id. +

+ This terraform module is used to create ALB on AWS. +

-## Infrastructure creats by this module +

-1. Alb -2. Alb listener -3. Alb target group -4. Alb target group attachment. - + + Terraform + + + Licence + +

+

+ + + + + + + + + + +

+
+ + +We eat, drink, sleep and most importantly love **DevOps**. We are working towards stratergies for standardizing architecture while ensuring security for the infrastructure. We are strong believer of the philosophy Bigger problems are always solved by breaking them into smaller manageable problems. Resonating with microservices architecture, it is considered best-practice to run database, cluster, storage in smaller connected yet manageable pieces within the infrastructure. + +This module is basically combination of [Terraform open source](https://www.terraform.io/) and includes automatation tests and examples. It also helps to create and improve your infrastructure with minimalistic code instead of maintaining the whole infrastructure code yourself. + +We have [*fifty plus terraform modules*][terraform_modules]. A few of them are comepleted and are available for open source usage while a few others are in progress. + + + + +## Prerequisites + +This module has a few dependencies: + +- [Terraform 0.12](https://learn.hashicorp.com/terraform/getting-started/install.html) +- [Go](https://golang.org/doc/install) +- [github.com/stretchr/testify/assert](https://github.com/stretchr/testify) +- [github.com/gruntwork-io/terratest/modules/terraform](https://github.com/gruntwork-io/terratest) + + + + + + +## Examples + +**IMPORTANT:** Since the `master` branch used in `source` varies based on new modifications, we suggest that you use the release versions [here](https://github.com/clouddrove/terraform-aws-alb/releases). + + +### Simple Example +Here is an example of how you can use this module in your inventory structure: +```hcl + module "alb" { + source = "git::https://github.com/clouddrove/terraform-aws-alb.git" + name = "alb" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] + internal = false + load_balancer_type = "application" + instance_count = 2 + security_groups = ["sg-xxxxxxx"] + subnets = "subnet-xxxxxxx" + enable_deletion_protection = false + target_id = "i-xxxxxxxxxx" + vpc_id = "vpc-xxxxxxxxx" + target_group_protocol = "HTTP" + target_group_port = 80 + listener_certificate_arn = "arn:aws:acm:eu-west-1:xxxxxxxxxxxx:certificate/xxxxxx-xxxx-xxxxx-xxxx" + https_enabled = true + http_enabled = true +} +``` + + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| access_logs | Access logs Enable or Disable. | bool | `false` | no | +| alb_environment | A mapping of tags to assign to the resource. | string | `` | no | +| alb_name | The name of the LB. This name must be unique within your AWS account, can have a maximum of 32 characters, must contain only alphanumeric characters or hyphens, and must not begin or end with a hyphen. If not specified, Terraform will autogenerate a name beginning with tf-lb. | string | `` | no | +| allocation_id | The allocation ID of the Elastic IP address. | string | `` | no | +| application | Application (e.g. `cd` or `clouddrove`). | string | `` | no | +| attributes | Additional attributes (e.g. `1`). | list | `` | no | +| delimiter | Delimiter to be used between `organization`, `environment`, `name` and `attributes`. | string | `-` | no | +| deregistration_delay | The amount time for Elastic Load Balancing to wait before changing the state of a deregistering target from draining to unused. The range is 0-3600 seconds. The default value is 300 seconds. | number | `300` | no | +| enable_cross_zone_load_balancing | Indicates whether cross zone load balancing should be enabled in application load balancers. | bool | `false` | no | +| enable_deletion_protection | If true, deletion of the load balancer will be disabled via the AWS API. This will prevent Terraform from deleting the load balancer. Defaults to false. | string | `` | no | +| enable_http2 | Indicates whether HTTP/2 is enabled in application load balancers. | bool | `true` | no | +| environment | Environment (e.g. `prod`, `dev`, `staging`). | string | `` | no | +| health_check_healthy_threshold | The number of consecutive health checks successes required before considering an unhealthy target healthy. | number | `2` | no | +| health_check_interval | The duration in seconds in between health checks. | number | `15` | no | +| health_check_matcher | The HTTP response codes to indicate a healthy check. | string | `200-399` | no | +| health_check_path | The destination for the health check request. | string | `/` | no | +| health_check_timeout | The amount of time to wait in seconds before failing a health check request. | number | `10` | no | +| health_check_unhealthy_threshold | The number of consecutive health check failures required before considering the target unhealthy. | number | `2` | no | +| http_enabled | A boolean flag to enable/disable HTTP listener. | bool | `true` | no | +| http_port | The port on which the load balancer is listening. like 80 or 443. | number | `80` | no | +| https_enabled | A boolean flag to enable/disable HTTPS listener. | bool | `true` | no | +| https_port | The port on which the load balancer is listening. like 80 or 443. | number | - | yes | +| idle_timeout | The time in seconds that the connection is allowed to be idle. | number | `60` | no | +| instance_count | The count of instances. | number | `0` | no | +| internal | If true, the LB will be internal. | string | `` | no | +| ip_address_type | The type of IP addresses used by the subnets for your load balancer. The possible values are ipv4 and dualstack. | string | `ipv4` | no | +| label_order | Label order, e.g. `name`,`application`. | list | `` | no | +| listener_certificate_arn | The ARN of the SSL server certificate. Exactly one certificate is required if the protocol is HTTPS. | string | `` | no | +| listener_protocol | The protocol for connections from clients to the load balancer. Valid values are TCP, HTTP and HTTPS. Defaults to HTTP. | string | `HTTPS` | no | +| listener_ssl_policy | The security policy if using HTTPS externally on the load balancer. [See](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-security-policy-table.html). | string | `ELBSecurityPolicy-2016-08` | no | +| listener_type | The type of routing action. Valid values are forward, redirect, fixed-response, authenticate-cognito and authenticate-oidc. | string | - | yes | +| load_balancer_create_timeout | Timeout value when creating the ALB. | string | `10m` | no | +| load_balancer_delete_timeout | Timeout value when deleting the ALB. | string | `10m` | no | +| load_balancer_type | The type of load balancer to create. Possible values are application or network. The default value is application. | string | `` | no | +| load_balancer_update_timeout | Timeout value when updating the ALB. | string | `10m` | no | +| log_bucket_name | S3 bucket (externally created) for storing load balancer access logs. Required if logging_enabled is true. | string | `` | no | +| name | Name (e.g. `app` or `cluster`). | string | `` | no | +| security_groups | A list of security group IDs to assign to the LB. Only valid for Load Balancers of type application. | list | `` | no | +| subnet_id | The id of the subnet of which to attach to the load balancer. You can specify only one subnet per Availability Zone. | string | `` | no | +| subnets | A list of subnet IDs to attach to the LB. Subnets cannot be updated for Load Balancers of type network. Changing this value will for load balancers of type network will force a recreation of the resource. | list | `` | no | +| tags | Additional tags (e.g. map(`BusinessUnit`,`XYZ`). | map | `` | no | +| target_group_port | The port on which targets receive traffic, unless overridden when registering a specific target. | string | `` | no | +| target_group_protocol | The protocol to use for routing traffic to the targets. | string | `` | no | +| target_id | The ID of the target. This is the Instance ID for an instance, or the container ID for an ECS container. If the target type is ip, specify an IP address. | list | - | yes | +| target_type | The type of target that you must specify when registering targets with this target group. The possible values are instance (targets are specified by instance ID) or ip (targets are specified by IP address) or lambda (targets are specified by lambda arn). The default is instance. | string | `instance` | no | +| vpc_id | The identifier of the VPC in which to create the target group. | string | `` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| arn | The ARN of the ALB. | +| arn_suffix | The ARN suffix of the ALB. | +| dns_name | DNS name of ALB. | +| http_listener_arn | The ARN of the HTTP listener. | +| https_listener_arn | The ARN of the HTTPS listener. | +| listener_arns | A list of all the listener ARNs. | +| main_target_group_arn | The main target group ARN. | +| name | The ARN suffix of the ALB. | +| tags | A mapping of tags to assign to the resource. | +| zone_id | The ID of the zone which ALB is provisioned. | + + + +## Testing + +In this module testing is performed with [terratest](https://github.com/gruntwork-io/terratest) and it creates a small piece of infrastructure, matches the output like ARN, ID and Tags name etc and destroy infrastructure in your AWS account. This testing is written in GO, so you need a [GO environment](https://golang.org/doc/install) in your system. + +You need to run the following command in the testing folder: +```hcl + go test -run Test +``` + + + +## Feedback +If you come accross a bug or have any feedback, please log it in our [issue tracker](https://github.com/clouddrove/terraform-aws-alb/issues), or feel free to drop us an email at [hello@clouddrove.com](mailto:hello@clouddrove.com). + +If you have found it worth your time, go ahead and give us a ★ on [our GitHub](https://github.com/clouddrove/terraform-aws-alb)! + +## About us + +At [CloudDrove][website], we offer expert guidance, implementation support and services to help organisations accelerate their journey to the cloud. Our services include docker and container orchestration, cloud migration and adoption, infrastructure automation, application modernisation and remediation, and performance engineering. + +

We are The Cloud Experts!

+
+

We ❤️ Open Source and you can check out our other modules to get help with your new Cloud ideas.

+ + [website]: https://clouddrove.com + [github]: https://github.com/clouddrove + [linkedin]: https://cpco.io/linkedin + [twitter]: https://twitter.com/clouddrove/ + [email]: https://clouddrove.com/contact-us.html + [terraform_modules]: https://github.com/clouddrove?utf8=%E2%9C%93&q=terraform-&type=&language= \ No newline at end of file diff --git a/README.yaml b/README.yaml new file mode 100644 index 0000000..50ae030 --- /dev/null +++ b/README.yaml @@ -0,0 +1,58 @@ +--- +# +# This is the canonical configuration for the `README.md` +# Run `make readme` to rebuild the `README.md` +# + +# Name of this project +name : Terraform AWS ALB + +# License of this project +license: "MIT" + +# Canonical GitHub repo +github_repo: clouddrove/terraform-aws-alb + +# Badges to display +badges: + - name: "Terraform" + image: "https://img.shields.io/badge/Terraform-v0.12-green" + url: "https://www.terraform.io" + - name: "Licence" + image: "https://img.shields.io/badge/License-MIT-blue.svg" + url: "LICENSE.md" + +# description of this project +description: |- + This terraform module is used to create ALB on AWS. + +# extra content +include: + - "terraform.md" + +# How to use this project +usage : |- + ### Simple Example + Here is an example of how you can use this module in your inventory structure: + ```hcl + module "alb" { + source = "git::https://github.com/clouddrove/terraform-aws-alb.git" + name = "alb" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] + internal = false + load_balancer_type = "application" + instance_count = 2 + security_groups = ["sg-xxxxxxx"] + subnets = "subnet-xxxxxxx" + enable_deletion_protection = false + target_id = "i-xxxxxxxxxx" + vpc_id = "vpc-xxxxxxxxx" + target_group_protocol = "HTTP" + target_group_port = 80 + listener_certificate_arn = "arn:aws:acm:eu-west-1:xxxxxxxxxxxx:certificate/xxxxxx-xxxx-xxxxx-xxxx" + https_enabled = true + http_enabled = true + } + ``` \ No newline at end of file diff --git a/_example/example.tf b/_example/example.tf new file mode 100644 index 0000000..d3dda24 --- /dev/null +++ b/_example/example.tf @@ -0,0 +1,129 @@ +provider "aws" { + region = "eu-west-1" +} + +module "keypair" { + source = "git::https://github.com/clouddrove/terraform-aws-keypair.git" + + key_path = "~/.ssh/id_rsa.pub" + key_name = "main-key" + enable_key_pair = true +} + +module "vpc" { + source = "git::https://github.com/clouddrove/terraform-aws-vpc.git" + + name = "vpc" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] + + cidr_block = "172.16.0.0/16" +} + +module "public_subnets" { + source = "git::https://github.com/clouddrove/terraform-aws-subnet.git" + + name = "public-subnet" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] + + availability_zones = ["eu-west-1a", "eu-west-1c"] + vpc_id = module.vpc.vpc_id + cidr_block = module.vpc.vpc_cidr_block + type = "public" + igw_id = module.vpc.igw_id +} + +module "http-https" { + source = "git::https://github.com/clouddrove/terraform-aws-security-group.git" + name = "http-https" + application = "clouddrove" + label_order = ["environment", "name", "application"] + + environment = "test" + vpc_id = module.vpc.vpc_id + allowed_ip = ["0.0.0.0/0"] + allowed_ports = [80, 443] +} + +module "ssh" { + source = "git::https://github.com/clouddrove/terraform-aws-security-group.git" + name = "ssh" + application = "clouddrove" + label_order = ["environment", "name", "application"] + + environment = "test" + vpc_id = module.vpc.vpc_id + allowed_ip = [module.vpc.vpc_cidr_block] + allowed_ports = [22] +} + +module "ec2" { + source = "git::https://github.com/clouddrove/terraform-aws-ec2.git" + + name = "ec2-instance" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] + + instance_count = 2 + ami = "ami-08d658f84a6d84a80" + ebs_optimized = false + instance_type = "t2.nano" + key_name = module.keypair.name + monitoring = false + associate_public_ip_address = true + tenancy = "default" + disk_size = 8 + vpc_security_group_ids_list = [module.ssh.security_group_ids, module.http-https.security_group_ids] + subnet_ids = tolist(module.public_subnets.public_subnet_id) + + assign_eip_address = true + + ebs_volume_enabled = true + ebs_volume_type = "gp2" + ebs_volume_size = 30 +} + +module "acm" { + source = "git::https://github.com/clouddrove/terraform-aws-acm.git" + + name = "certificate" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] + + domain_name = "clouddrove.com" + validation_method = "EMAIL" + validate_certificate = true +} + +module "alb" { + source = "git::https://github.com/clouddrove/terraform-aws-alb.git" + + name = "alb" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] + + internal = false + load_balancer_type = "application" + instance_count = module.ec2.instance_count + security_groups = [module.ssh.security_group_ids, module.http-https.security_group_ids] + subnets = module.public_subnets.public_subnet_id + enable_deletion_protection = false + + target_id = module.ec2.instance_id + vpc_id = module.vpc.vpc_id + target_group_protocol = "HTTP" + target_group_port = 80 + + listener_certificate_arn = module.acm.arn + https_enabled = true + http_enabled = true + https_port = 443 + listener_type = "forward" + +} \ No newline at end of file diff --git a/_example/outputs.tf b/_example/outputs.tf new file mode 100644 index 0000000..771ac3b --- /dev/null +++ b/_example/outputs.tf @@ -0,0 +1,9 @@ +output "arn" { + value = module.alb.*.arn + description = "The ARN suffix of the ALB" +} + +output "tags" { + value = module.alb.tags + description = "A mapping of tags to assign to the alb." +} \ No newline at end of file diff --git a/_test/alb_test.go b/_test/alb_test.go new file mode 100644 index 0000000..280ffe4 --- /dev/null +++ b/_test/alb_test.go @@ -0,0 +1,34 @@ +// Managed By : CloudDrove +// Description : This Terratest is used to test the Terraform VPC module. +// Copyright @ CloudDrove. All Right Reserved. +package test + +import ( + "testing" + + "github.com/gruntwork-io/terratest/modules/terraform" + "github.com/stretchr/testify/assert" +) + +func Test(t *testing.T) { + t.Parallel() + + terraformOptions := &terraform.Options{ + // Source path of Terraform directory. + TerraformDir: "../_example", + } + + // This will run 'terraform init' and 'terraform application' and will fail the test if any errors occur + terraform.InitAndApply(t, terraformOptions) + + // To clean up any resources that have been created, run 'terraform destroy' towards the end of the test + defer terraform.Destroy(t, terraformOptions) + + // To get the value of an output variable, run 'terraform output' + Arn := terraform.Output(t, terraformOptions, "arn") + Tags := terraform.OutputMap(t, terraformOptions, "tags") + + // Check that we get back the outputs that we expect + assert.Equal(t, "test-alb-clouddrove", Tags["Name"]) + assert.Contains(t, Arn, "arn:aws:elasticloadbalancing") +} \ No newline at end of file diff --git a/examples/example.tf b/examples/example.tf deleted file mode 100755 index fa002b5..0000000 --- a/examples/example.tf +++ /dev/null @@ -1,81 +0,0 @@ -provider "aws" { - profile = "default" - region = "${var.region}" -} - -locals { - public_cidr_block = "${cidrsubnet("10.0.0.0/16", 1, 0)}" -} - -module "vpc" { - source = "git::https://github.com/clouddrove/terraform-aws-vpc.git?ref=tags/0.11.0" - cidr_block = "10.0.0.0/16" - name = "vpc" - application = "clouddrove" - environment = "test" -} - - -module "public_subnets" { - source = "git::https://github.com/clouddrove/terraform-aws-public-subnet.git?ref=tags/0.11.0" - name = "name" - application = "cloudDrove" - environment = "test" - availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c"] - vpc_id = "${module.vpc.vpc_id}" - cidr_block = "${local.public_cidr_block}" - type = "public" - igw_id = "${module.vpc.igw_id}" - nat_gateway_enabled = "false" -} -module "bastion" { - source = "../../terraform-aws-ec2" - name = "${var.name}" - instance_count = 2 - - application = "${var.application}" - environment = "${var.environment}" - ami = "${var.ami_id}" - instance_type = "${var.instance_type}" - key_name = "${var.key_name}" - monitoring = false - vpc_security_group_ids_list = ["${module.sg.security_group_ids}"] - subnet = "${var.subnet}" - ebs_volume_count = "0" - disk_size = 10 - user_data_base64 = "${base64encode("${file("../_bin/user_data.sh")}")}" -} - -module "sg" { - source = "git::https://github.com/clouddrove/terraform-aws-security-group.git?ref=tags/0.11.0" - name = "${var.name}" - application = "${var.application}" - environment = "${var.environment}" - vpc_id = "${var.vpc_id}" - cidr_blocks = "${var.source_ip}" - allowed_ports = [ - 22] - -} -module "alb" { - source = "./../" - application = "dev" - environment = "${var.environment}" - - name = "backend" - internal = false - load_balancer_type = "application" - security_groups = ["${module.sg.security_group_ids}"] - subnets = ["subnet-0399c34a","subnet-18d8ad35"] - enable_deletion_protection = false - listener_port = 443 - listener_protocol = "HTTPS" - vpc_id = "vpc-7478c912" - instance_count = 1 - target_id = ["i-08da6f78390d2a8c6",] - target_group_protocol = "HTTP" - target_group_port = 80 - target_group_attachment_port = 80 - listener_certificate_arn = "arn:aws:acm:us-east-1:946010253026:certificate/17247909-1d93-4dbb-80a6-32a31a9a4b9f" - log_bucket_name = "cloudrove-logs" -} diff --git a/examples/varible.tf b/examples/varible.tf deleted file mode 100755 index 8343f96..0000000 --- a/examples/varible.tf +++ /dev/null @@ -1,59 +0,0 @@ -variable "region" { - default = "us-east-1" -} -variable "name" { - type = "string" - description = "Name (e.g. `app` or `cluster`)" -} - -variable "application" { - type = "string" - description = "Application (e.g. `cd` or `clouddrove`)" -} - -variable "environment" { - type = "string" - description = "Environment (e.g. `prod`, `dev`, `staging`)" -} - -variable "vpc_id" { - type = "string" - description = "" -} - -variable "source_ip" { - type = "list" - description = "" -} - -variable "instance_type" { - type = "string" - description = "" - default = "t2.micro" -} - -variable "key_name" { - type = "string" - description = "" -} - -variable "ami_id" { - type = "string" - description = "" -} - -variable "subnet" { - type = "string" - description = "" -} - -variable "disk_size" { - description = "Size of the root volume in gigabytes" - default = "8" -} - -variable "user_data_base64" { - type = "string" - description = "The Base64-encoded user data to provide when launching the instances" - default = "" -} diff --git a/main.tf b/main.tf old mode 100755 new mode 100644 index 1e47897..4880732 --- a/main.tf +++ b/main.tf @@ -1,66 +1,104 @@ -module "label" { - source = "git::https://github.com/clouddrove/terraform-lables.git?ref=tags/0.11.0" - name = "${var.name}" - application = "${var.application}" - environment = "${var.environment}" +## Managed By : CloudDrove +## Description : This Script is used to create Aws Loadbalancer,Aws Loadbalancer Listeners. +## Copyright @ CloudDrove. All Right Reserved. + +#Module : label +#Description : This terraform module is designed to generate consistent label names and +# tags for resources. You can use terraform-labels to implement a strict +# naming convention. +module "labels" { + source = "git::https://github.com/clouddrove/terraform-labels.git" + + name = var.name + application = var.application + environment = var.environment + label_order = var.label_order } -## ALB + +# Module : APPLICATION LOAD BALANCER +# Description : This terraform module is used to create ALB on AWS. resource "aws_lb" "main" { - name = "${module.label.id}" - internal = "${var.internal}" - load_balancer_type = "${var.load_balancer_type}" - security_groups = ["${var.security_groups}"] - subnets = ["${var.subnets}"] - enable_deletion_protection = "${var.enable_deletion_protection}" - idle_timeout = "${var.idle_timeout}" - enable_cross_zone_load_balancing = "${var.enable_cross_zone_load_balancing}" - enable_http2 = "${var.enable_http2}" - ip_address_type = "${var.ip_address_type}" - tags = "${module.label.tags}" + name = module.labels.id + internal = var.internal + load_balancer_type = var.load_balancer_type + security_groups = var.security_groups + subnets = var.subnets + enable_deletion_protection = var.enable_deletion_protection + idle_timeout = var.idle_timeout + enable_cross_zone_load_balancing = var.enable_cross_zone_load_balancing + enable_http2 = var.enable_http2 + ip_address_type = var.ip_address_type + tags = module.labels.tags timeouts { - create = "${var.load_balancer_create_timeout}" - delete = "${var.load_balancer_delete_timeout}" - update = "${var.load_balancer_update_timeout}" + create = var.load_balancer_create_timeout + delete = var.load_balancer_delete_timeout + update = var.load_balancer_update_timeout } access_logs { - enabled = "${var.access_logs}" - bucket = "${var.log_bucket_name}" - prefix = "${module.label.id}" + enabled = var.access_logs + bucket = var.log_bucket_name + prefix = module.labels.id } } +# Module : LOAD BALANCER LISTENER HTTPS +# Description : Provides a Load Balancer Listener resource. +resource "aws_lb_listener" "https" { + count = var.https_enabled == true ? 1 : 0 -##### ALB LISTENER - -resource "aws_lb_listener" "front_end" { - load_balancer_arn = "${aws_lb.main.arn}" - port = "${var.listener_port}" - protocol = "${var.listener_protocol}" - ssl_policy = "${var.listener_ssl_policy}" - certificate_arn = "${var.listener_certificate_arn}" + load_balancer_arn = aws_lb.main.arn + port = var.https_port + protocol = var.listener_protocol + ssl_policy = var.listener_ssl_policy + certificate_arn = var.listener_certificate_arn default_action { - target_group_arn = "${aws_lb_target_group.main.arn}" - type = "forward" + target_group_arn = aws_lb_target_group.main.arn + type = var.listener_type } } -##### ALB TARGET GROUP +# Module : LOAD BALANCER LISTENER HTTP +# Description : Provides a Load Balancer Listener resource. +resource "aws_lb_listener" "http" { + count = var.http_enabled == true ? 1 : 0 -resource "aws_lb_target_group" "main" { - name = "${module.label.id}" - port = "${var.target_group_port}" - protocol = "${var.target_group_protocol}" - vpc_id = "${var.vpc_id}" + load_balancer_arn = aws_lb.main.arn + port = var.http_port + protocol = "HTTP" + default_action { + target_group_arn = aws_lb_target_group.main.arn + type = var.listener_type + } } +# Module : LOAD BALANCER TARGET GROUP +# Description : Provides a Target Group resource for use with Load Balancer resources. +resource "aws_lb_target_group" "main" { + name = module.labels.id + port = var.target_group_port + protocol = var.target_group_protocol + vpc_id = var.vpc_id + target_type = var.target_type + deregistration_delay = var.deregistration_delay + health_check { + path = var.health_check_path + timeout = var.health_check_timeout + healthy_threshold = var.health_check_healthy_threshold + unhealthy_threshold = var.health_check_unhealthy_threshold + interval = var.health_check_interval + matcher = var.health_check_matcher + } +} -##### ALB TARGET GROUP ATTACHMENT 1 +# Module : TARGET GROUP ATTACHMENT +# Description : Provides the ability to register instances and containers with an +# Application Load Balancer (ALB) or Network Load Balancer (NLB) target group. +resource "aws_lb_target_group_attachment" "attachment" { + count = var.instance_count -resource "aws_lb_target_group_attachment" "attachment1" { - count = "${var.instance_count}" - target_group_arn = "${aws_lb_target_group.main.arn}" - target_id = "${element(var.target_id, count.index)}" - port = "${var.target_group_attachment_port}" + target_group_arn = aws_lb_target_group.main.arn + target_id = element(var.target_id, count.index) + port = var.target_group_port } diff --git a/outputs.tf b/outputs.tf old mode 100755 new mode 100644 index e69de29..2310df9 --- a/outputs.tf +++ b/outputs.tf @@ -0,0 +1,51 @@ +#Module : ALB +#Description : This terraform module is used to create ALB on AWS. +output "name" { + value = aws_lb.main.name + description = "The ARN suffix of the ALB." +} + +output "arn" { + value = aws_lb.main.arn + description = "The ARN of the ALB." +} + +output "arn_suffix" { + value = aws_lb.main.arn_suffix + description = "The ARN suffix of the ALB." +} + +output "dns_name" { + value = aws_lb.main.dns_name + description = "DNS name of ALB." +} + +output "zone_id" { + value = aws_lb.main.zone_id + description = "The ID of the zone which ALB is provisioned." +} + +output "main_target_group_arn" { + value = aws_lb_target_group.main.arn + description = "The main target group ARN." +} + +output "http_listener_arn" { + value = join("", aws_lb_listener.http.*.arn) + description = "The ARN of the HTTP listener." +} + +output "https_listener_arn" { + value = join("", aws_lb_listener.https.*.arn) + description = "The ARN of the HTTPS listener." +} + +output "listener_arns" { + value = compact(concat(aws_lb_listener.http.*.arn, aws_lb_listener.https.*.arn)) + description = "A list of all the listener ARNs." +} + +output "tags" { + value = module.labels.tags + description = "A mapping of tags to assign to the resource." +} \ No newline at end of file diff --git a/variables.tf b/variables.tf old mode 100755 new mode 100644 index 0a3937e..73ebbe1 --- a/variables.tf +++ b/variables.tf @@ -1,177 +1,276 @@ -##### -##### ALB +#Module : LABEL +#Description : Terraform label module variables +variable "name" { + type = string + default = "" + description = "Name (e.g. `app` or `cluster`)." +} + variable "application" { - type = "string" - description = "Application (e.g. `cp` or `clouddrove`)" + type = string + default = "" + description = "Application (e.g. `cd` or `clouddrove`)." } + variable "environment" { - type = "string" - description = "Environment (e.g. `prod`, `dev`, `staging`)" + type = string + default = "" + description = "Environment (e.g. `prod`, `dev`, `staging`)." } -variable "name" { - description = "Name (e.g. `app` or `cluster`)" - type = "string" +variable "label_order" { + type = list + default = [] + description = "Label order, e.g. `name`,`application`." +} + +variable "attributes" { + type = list + default = [] + description = "Additional attributes (e.g. `1`)." } variable "delimiter" { - type = "string" + type = string default = "-" - description = "Delimiter to be used between `namespace`, `stage`, `name` and `attributes`" + description = "Delimiter to be used between `organization`, `environment`, `name` and `attributes`." } -variable "attributes" { - type = "list" - default = [] - description = "Additional attributes (e.g. `1`)" -} variable "tags" { - type = "map" + type = map default = {} - description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)" + description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)." } + +# Module : ALB +# Description : Terraform ALB module variables. variable "alb_name" { - description = "The name of the LB. This name must be unique within your AWS account, can have a maximum of 32 characters, must contain only alphanumeric characters or hyphens, and must not begin or end with a hyphen. If not specified, Terraform will autogenerate a name beginning with tf-lb." - default = "" + type = string + default = "" + description = "The name of the LB. This name must be unique within your AWS account, can have a maximum of 32 characters, must contain only alphanumeric characters or hyphens, and must not begin or end with a hyphen. If not specified, Terraform will autogenerate a name beginning with tf-lb." +} + +variable "instance_count" { + type = number + default = 0 + description = "The count of instances." } variable "internal" { - description = "(Optional) If true, the LB will be internal." - default = "" + type = string + default = "" + description = "If true, the LB will be internal." } variable "load_balancer_type" { - description = "(Optional) The type of load balancer to create. Possible values are application or network. The default value is application." - default = "" + type = string + default = "" + description = "The type of load balancer to create. Possible values are application or network. The default value is application." } variable "security_groups" { - - description = "(Optional) A list of security group IDs to assign to the LB. Only valid for Load Balancers of type application." - default = [] + type = list + default = [] + description = "A list of security group IDs to assign to the LB. Only valid for Load Balancers of type application." } variable "subnets" { - description = "(Optional) A list of subnet IDs to attach to the LB. Subnets cannot be updated for Load Balancers of type network. Changing this value will for load balancers of type network will force a recreation of the resource." - default = [] + type = list + default = [] + description = "A list of subnet IDs to attach to the LB. Subnets cannot be updated for Load Balancers of type network. Changing this value will for load balancers of type network will force a recreation of the resource." } variable "enable_deletion_protection" { - description = "(Optional) If true, deletion of the load balancer will be disabled via the AWS API. This will prevent Terraform from deleting the load balancer. Defaults to false." - default = "" + type = string + default = "" + description = "If true, deletion of the load balancer will be disabled via the AWS API. This will prevent Terraform from deleting the load balancer. Defaults to false." } +variable "subnet_id" { + type = string + default = "" + description = "The id of the subnet of which to attach to the load balancer. You can specify only one subnet per Availability Zone." +} +variable "allocation_id" { + type = string + default = "" + description = "The allocation ID of the Elastic IP address." +} variable "alb_environment" { - description = "(Optional) A mapping of tags to assign to the resource." - default = "" + type = string + default = "" + description = "A mapping of tags to assign to the resource." } -##### ALB LISTENER +variable "https_port" { + type = number + description = "The port on which the load balancer is listening. like 80 or 443." +} +variable "listener_protocol" { + type = string + default = "HTTPS" + description = "The protocol for connections from clients to the load balancer. Valid values are TCP, HTTP and HTTPS. Defaults to HTTP." +} -variable "listener_port" { - description = "The port on which the load balancer is listening. like 80 or 443" - default = "" +variable "http_port" { + type = number + default = 80 + description = "The port on which the load balancer is listening. like 80 or 443." } -variable "listener_protocol" { - description = "The protocol for connections from clients to the load balancer. Valid values are TCP, HTTP and HTTPS. Defaults to HTTP" - default = "" +variable "https_enabled" { + type = bool + default = true + description = "A boolean flag to enable/disable HTTPS listener." +} + +variable "http_enabled" { + type = bool + default = true + description = "A boolean flag to enable/disable HTTP listener." } +variable "listener_type" { + type = string + description = "The type of routing action. Valid values are forward, redirect, fixed-response, authenticate-cognito and authenticate-oidc." +} variable "listener_ssl_policy" { - description = "The security policy if using HTTPS externally on the load balancer. [See](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-security-policy-table.html)." + type = string default = "ELBSecurityPolicy-2016-08" + description = "The security policy if using HTTPS externally on the load balancer. [See](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-security-policy-table.html)." } variable "listener_certificate_arn" { - description = "The ARN of the SSL server certificate. Exactly one certificate is required if the protocol is HTTPS" - default = "" + type = string + default = "" + description = "The ARN of the SSL server certificate. Exactly one certificate is required if the protocol is HTTPS." } - -##### ALB TARGET GROUP - - - variable "target_group_port" { - description = "The port on which targets receive traffic, unless overridden when registering a specific target." - default = "" + type = string + default = "" + description = "The port on which targets receive traffic, unless overridden when registering a specific target." } - variable "target_group_protocol" { - description = "The protocol to use for routing traffic to the targets." - default = "" + type = string + default = "" + description = "The protocol to use for routing traffic to the targets." } variable "vpc_id" { - description = "The identifier of the VPC in which to create the target group." - default = "" + type = string + default = "" + description = "The identifier of the VPC in which to create the target group." } - variable "target_id" { - description = "The ID of the target. This is the Instance ID for an instance, or the container ID for an ECS container. If the target type is ip, specify an IP address." - default = [] -} - - -variable "target_group_attachment_port" { - description = "The port on which targets receive traffic." - default = "" + type = list + description = "The ID of the target. This is the Instance ID for an instance, or the container ID for an ECS container. If the target type is ip, specify an IP address." } variable "idle_timeout" { - description = "The time in seconds that the connection is allowed to be idle." + type = number default = 60 + description = "The time in seconds that the connection is allowed to be idle." } variable "enable_cross_zone_load_balancing" { - description = "Indicates whether cross zone load balancing should be enabled in application load balancers." + type = bool default = false + description = "Indicates whether cross zone load balancing should be enabled in application load balancers." } variable "enable_http2" { - description = "Indicates whether HTTP/2 is enabled in application load balancers." + type = bool default = true + description = "Indicates whether HTTP/2 is enabled in application load balancers." } variable "ip_address_type" { - description = "The type of IP addresses used by the subnets for your load balancer. The possible values are ipv4 and dualstack." + type = string default = "ipv4" -} -variable "log_bucket_name" { - description = "S3 bucket (externally created) for storing load balancer access logs. Required if logging_enabled is true." - default = "" + description = "The type of IP addresses used by the subnets for your load balancer. The possible values are ipv4 and dualstack." } -variable "log_location_prefix" { - description = "S3 prefix within the log_bucket_name under which logs are stored." +variable "log_bucket_name" { + type = string default = "" + description = "S3 bucket (externally created) for storing load balancer access logs. Required if logging_enabled is true." } variable "load_balancer_create_timeout" { + type = string + default = "10m" description = "Timeout value when creating the ALB." - default = "10m" } + variable "load_balancer_delete_timeout" { - description = "Timeout value when deleting the ALB." - default = "10m" - } -variable "load_balancer_update_timeout" { - description = "Timeout value when updating the ALB." + type = string default = "10m" + description = "Timeout value when deleting the ALB." } -variable "instance_count" { - description = "Push these instances to ALB" - default = "" +variable "load_balancer_update_timeout" { + type = string + default = "10m" + description = "Timeout value when updating the ALB." } + variable "access_logs" { - description = "Access logs Enable or Disable" - default = false + type = bool + default = false + description = "Access logs Enable or Disable." +} + +variable "target_type" { + type = string + default = "instance" + description = "The type of target that you must specify when registering targets with this target group. The possible values are instance (targets are specified by instance ID) or ip (targets are specified by IP address) or lambda (targets are specified by lambda arn). The default is instance." +} + +variable "deregistration_delay" { + type = number + default = 300 + description = "The amount time for Elastic Load Balancing to wait before changing the state of a deregistering target from draining to unused. The range is 0-3600 seconds. The default value is 300 seconds." } + +variable "health_check_path" { + type = string + default = "/" + description = "The destination for the health check request." +} + +variable "health_check_timeout" { + type = number + default = 10 + description = "The amount of time to wait in seconds before failing a health check request." +} + +variable "health_check_healthy_threshold" { + type = number + default = 2 + description = "The number of consecutive health checks successes required before considering an unhealthy target healthy." +} + +variable "health_check_unhealthy_threshold" { + type = number + default = 2 + description = "The number of consecutive health check failures required before considering the target unhealthy." +} + +variable "health_check_interval" { + type = number + default = 15 + description = "The duration in seconds in between health checks." +} + +variable "health_check_matcher" { + type = string + default = "200-399" + description = "The HTTP response codes to indicate a healthy check." +} \ No newline at end of file diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..5e90985 --- /dev/null +++ b/versions.tf @@ -0,0 +1,4 @@ +# Terraform version +terraform { + required_version = ">= 0.12" +}