diff --git a/README.md b/README.md index da54038..b569a85 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Check valid versions on: source = "cn-terraform/nomad/aws" version = "2.0.2" - names_prefix = ${terraform.workspace} + name_prefix = ${terraform.workspace} profile = var.profile region = var.region @@ -55,7 +55,7 @@ Pleas run this command right after cloning the repository. pre-commit install For that you may need to install the folowwing tools: -* [Pre-commit](https://pre-commit.com/) +* [Pre-commit](https://pre-commit.com/) * [Terraform Docs](https://terraform-docs.io/) In order to run all checks at any point run the following command: @@ -131,11 +131,9 @@ No modules. | [consul\_address](#input\_consul\_address) | Consul Address | `any` | n/a | yes | | [consul\_version](#input\_consul\_version) | Consul Version | `string` | `"0.9.2"` | no | | [domain\_name](#input\_domain\_name) | Domain Name | `any` | n/a | yes | -| [names\_prefix](#input\_names\_prefix) | prefix for Resources Names | `any` | n/a | yes | +| [name\_prefix](#input\_name\_prefix) | prefix for Resources Names | `any` | n/a | yes | | [nomad\_version](#input\_nomad\_version) | Nomad Version | `string` | `"0.6.0"` | no | | [ports\_to\_open\_on\_elb\_security\_group](#input\_ports\_to\_open\_on\_elb\_security\_group) | Ports to Open on ELB Security Group | `list(any)` |
[
"22",
"80",
"443"
]
| no | -| [profile](#input\_profile) | AWS API key credentials to use | `any` | n/a | yes | -| [region](#input\_region) | AWS Region the infrastructure is hosted in | `any` | n/a | yes | | [route53\_zone\_id](#input\_route53\_zone\_id) | Route53 Zone ID to fetch | `any` | n/a | yes | | [server\_ami\_id](#input\_server\_ami\_id) | AMI ID to use on servers | `string` | n/a | yes | | [server\_asg\_desired\_capacity](#input\_server\_asg\_desired\_capacity) | Desired Number of Instances of PAAS Server to Create | `any` | n/a | yes | diff --git a/examples/test/.terraform.lock.hcl b/examples/test/.terraform.lock.hcl new file mode 100644 index 0000000..9f60982 --- /dev/null +++ b/examples/test/.terraform.lock.hcl @@ -0,0 +1,38 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.3.0" + constraints = ">= 4.0.0" + hashes = [ + "h1:+z4+nEWPJ7NGu4Zo2U/3ghgN3+M3tckgUiTZI0c8k9I=", + "zh:087c67e5429f343a164221c05a83f152322f411e7394f8a39ed81a75982af1f2", + "zh:2e852a1b107e5324524874e1cd98bcf3a69284b4fe04750aa373054177c54214", + "zh:4b9a54b5895f945827832e6ddd16ff107301fedf47acbd83d17d4e18bbf10bb1", + "zh:64dfc02bc85f5df2f51ff942fc78d72fcd0db17b0f53e1fae380e58adbd239b3", + "zh:766f9aef619cfd23e924aee523791acccd30b6d8f1cc0ed1a7b5c953bf8c5392", + "zh:90048d87ff3071a4356cf91916b46a7ec69ba55bcba5765b598d3fe545d4c6ca", + "zh:c51f5b238af37c63e9033a12fd7fedc87c03eb966f5f5c7786eb6246e8bf3071", + "zh:d0df94d3112a25de609dfb55c5e3b0d119dea519a2bdd8099e64a8d63f22b683", + "zh:de166ecfeed70f570cea72ec094f00c2f997496b3226fa08518e7cd4a73884e1", + "zh:e31c31d00f42ea2dbaab1ad4c245da5cfff63e28399b5a5795b5e6a826c6c8af", + "zh:f93725afd8410194ede51d83505327aa1ae6a9b4280cf31db649c62c7dc203ae", + ] +} + +provider "registry.terraform.io/hashicorp/template" { + version = "2.2.0" + hashes = [ + "h1:LN84cu+BZpVRvYlCzrbPfCRDaIelSyEx/W9Iwwgbnn4=", + "zh:01702196f0a0492ec07917db7aaa595843d8f171dc195f4c988d2ffca2a06386", + "zh:09aae3da826ba3d7df69efeb25d146a1de0d03e951d35019a0f80e4f58c89b53", + "zh:09ba83c0625b6fe0a954da6fbd0c355ac0b7f07f86c91a2a97849140fea49603", + "zh:0e3a6c8e16f17f19010accd0844187d524580d9fdb0731f675ffcf4afba03d16", + "zh:45f2c594b6f2f34ea663704cc72048b212fe7d16fb4cfd959365fa997228a776", + "zh:77ea3e5a0446784d77114b5e851c970a3dde1e08fa6de38210b8385d7605d451", + "zh:8a154388f3708e3df5a69122a23bdfaf760a523788a5081976b3d5616f7d30ae", + "zh:992843002f2db5a11e626b3fc23dc0c87ad3729b3b3cff08e32ffb3df97edbde", + "zh:ad906f4cebd3ec5e43d5cd6dc8f4c5c9cc3b33d2243c89c5fc18f97f7277b51d", + "zh:c979425ddb256511137ecd093e23283234da0154b7fa8b21c2687182d9aea8b2", + ] +} diff --git a/examples/test/main.tf b/examples/test/main.tf new file mode 100644 index 0000000..43649e1 --- /dev/null +++ b/examples/test/main.tf @@ -0,0 +1,121 @@ +module "base-network" { + source = "cn-terraform/networking/aws" + name_prefix = "test-networking" + vpc_cidr_block = "192.168.0.0/16" + availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d"] + public_subnets_cidrs_per_availability_zone = ["192.168.0.0/19", "192.168.32.0/19", "192.168.64.0/19", "192.168.96.0/19"] + private_subnets_cidrs_per_availability_zone = ["192.168.128.0/19", "192.168.160.0/19", "192.168.192.0/19", "192.168.224.0/19"] +} + +module "nomad" { + source = "../../" + name_prefix = "nomad" + region = "us-east-1" + vpc_id = module.base-network.vpc_id + subnets_ids = module.base-network.private_subnets_ids +} + +variable "route53_zone_id" { + description = "Route53 Zone ID to fetch" +} + +variable "domain_name" { + description = "Domain Name" +} + +variable "ssh_key_name" { + description = "SSH Key Name" +} + +#------------------------------------------------------------------------------ +# SECURITY GROUP +#------------------------------------------------------------------------------ +variable "cidrs_to_open_ports_on_security_groups" { + description = "List of CIDRs to open ports on instances security group" + type = list(any) +} + +variable "ports_to_open_on_elb_security_group" { + description = "Ports to Open on ELB Security Group" + type = list(any) + default = ["22", "80", "443"] +} + +variable "tcp_ports_to_open_on_instances_security_group" { + description = "TCP Ports to Open on Instances Security Group" + type = list(any) + default = ["22", "4646", "4647", "4648", "8300", "8301", "8302", "8500", "8600"] +} + +variable "udp_ports_to_open_on_instances_security_group" { + description = "UDP Ports to Open on Instances Security Group" + type = list(any) + default = ["4648", "8301", "8302", "8600"] +} + +#------------------------------------------------------------------------------ +# VERSIONS +#------------------------------------------------------------------------------ +variable "consul_version" { + description = "Consul Version" + default = "v1.11.4" +} + +variable "nomad_version" { + description = "Nomad Version" + default = "v1.2.6" +} + +variable "consul_address" { + description = "Consul Address" +} + +#------------------------------------------------------------------------------ +# SERVER SETTINGS +#------------------------------------------------------------------------------ +variable "server_ami_id" { + description = "AMI ID to use on servers" + type = string +} + +variable "server_instance_type" { + description = "AWS Instance type to use on servers" + type = string +} + +variable "server_asg_min_size" { + description = "Min Number of Instances of PAAS Server to Create" +} + +variable "server_asg_desired_capacity" { + description = "Desired Number of Instances of PAAS Server to Create" +} + +variable "server_asg_max_size" { + description = "Max Number of Instances of PAAS Server to Create" +} + +#------------------------------------------------------------------------------ +# NOMAD CLIENT SETTINGS +#------------------------------------------------------------------------------ +variable "client_ami_id" { + description = "AMI ID to use on Clients" + type = string +} + +variable "client_instance_type" { + description = "AWS Instance type to use on clients" + type = string +} + +variable "client_asg_min_size" { + description = "Min Number of Instances of Nomad Client to Create" +} + +variable "client_asg_desired_capacity" { + description = "Desired Number of Instances of Nomad Client to Create" +} + +variable "client_asg_max_size" { + description = "Max Number of Instances of Nomad Client to Create" +} diff --git a/examples/test/mock_provider.tf b/examples/test/mock_provider.tf new file mode 100644 index 0000000..934ad5a --- /dev/null +++ b/examples/test/mock_provider.tf @@ -0,0 +1,19 @@ +terraform { + required_version = ">= 0.13" + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 4" + } + } +} + +provider "aws" { + region = "us-east-1" + skip_credentials_validation = true + skip_requesting_account_id = true + skip_metadata_api_check = true + s3_use_path_style = true + access_key = "mock_access_key" + secret_key = "mock_secret_key" +} diff --git a/iam.tf b/iam.tf index d180d6c..6906b66 100644 --- a/iam.tf +++ b/iam.tf @@ -8,27 +8,27 @@ ## Create IAM role resource "aws_iam_role" "ec2_describe_role" { - name = "${var.names_prefix}_ec2_describe_role" + name = "${var.name_prefix}_ec2_describe_role" assume_role_policy = file("${path.module}/iam_files/iam_role_ec2_describe.json") } ## Create IAM policy resource "aws_iam_policy" "ec2_describe_policy" { - name = "${var.names_prefix}_ec2_describe_policy" + name = "${var.name_prefix}_ec2_describe_policy" description = "Allows EC2 Instances to Describe Other EC2 Instances" policy = file("${path.module}/iam_files/iam_policy_ec2_describe.json") } ## Attach the policy to the role resource "aws_iam_policy_attachment" "ec2_describe_attach_policy" { - name = "${var.names_prefix}_ec2_describe_attach" + name = "${var.name_prefix}_ec2_describe_attach" roles = [aws_iam_role.ec2_describe_role.name] policy_arn = aws_iam_policy.ec2_describe_policy.arn } ## Create the instance profile resource "aws_iam_instance_profile" "ec2_describe_instance_profile" { - name = "${var.names_prefix}_ec2_describe_instance_profile" + name = "${var.name_prefix}_ec2_describe_instance_profile" role = aws_iam_role.ec2_describe_role.name } @@ -38,27 +38,26 @@ resource "aws_iam_instance_profile" "ec2_describe_instance_profile" { ## Create IAM role resource "aws_iam_role" "push_to_ecr_role" { - name = "${var.names_prefix}_push_to_ecr_role" + name = "${var.name_prefix}_push_to_ecr_role" assume_role_policy = file("${path.module}/iam_files/iam_role_ec2_describe.json") } ## Create IAM policy to allow the push of docker images to ECR resource "aws_iam_policy" "push_to_ecr_policy" { - name = "${var.names_prefix}_push_to_ecr_policy" + name = "${var.name_prefix}_push_to_ecr_policy" description = "Allow EC2 instances to push docker images to ECR registry" policy = file("${path.module}/iam_files/iam_policy_push_to_ecr.json") } ## Attach the policy to the role resource "aws_iam_policy_attachment" "push_to_ecr_attach_policy" { - name = "${var.names_prefix}_push_to_ecr_attach" + name = "${var.name_prefix}_push_to_ecr_attach" roles = [aws_iam_role.push_to_ecr_role.name] policy_arn = aws_iam_policy.push_to_ecr_policy.arn } ## Create an instance profile resource "aws_iam_instance_profile" "ecr_role_instance_profile" { - name = "${var.names_prefix}_push_to_ecr_instance_profile" + name = "${var.name_prefix}_push_to_ecr_instance_profile" role = aws_iam_role.push_to_ecr_role.name } - diff --git a/load_balancer.tf b/load_balancer.tf index 7e98c4e..22b57b6 100644 --- a/load_balancer.tf +++ b/load_balancer.tf @@ -3,13 +3,13 @@ #------------------------------------------------------------------------------ ## ELB resource "aws_elb" "consul_elb" { - name = "${var.names_prefix}-consul-elb" + name = "${var.name_prefix}-consul-elb" security_groups = [aws_security_group.elb_security_group.id] subnets = data.aws_subnet.subnets.*.id internal = true cross_zone_load_balancing = true tags = { - Name = "${var.names_prefix}_consul_elb" + Name = "${var.name_prefix}_consul_elb" } listener { instance_port = "8500" @@ -37,13 +37,13 @@ resource "aws_route53_record" "consul_elb_dns" { #------------------------------------------------------------------------------ ## ELB resource "aws_elb" "nomad_elb" { - name = "${var.names_prefix}-nomad-elb" + name = "${var.name_prefix}-nomad-elb" security_groups = [aws_security_group.elb_security_group.id] subnets = data.aws_subnet.subnets.*.id internal = true cross_zone_load_balancing = true tags = { - Name = "${var.names_prefix}_nomad_elb" + Name = "${var.name_prefix}_nomad_elb" } listener { instance_port = "4646" @@ -65,4 +65,3 @@ resource "aws_route53_record" "nomad_elb_dns" { evaluate_target_health = true } } - diff --git a/main.tf b/main.tf index 38233b0..4ddf6e3 100644 --- a/main.tf +++ b/main.tf @@ -1,11 +1,3 @@ -#------------------------------------------------------------------------------ -# PROVIDER -#------------------------------------------------------------------------------ -provider "aws" { - profile = var.profile - region = var.region -} - #------------------------------------------------------------------------------ # Data #------------------------------------------------------------------------------ @@ -18,4 +10,3 @@ data "aws_route53_zone" "hosted_zone" { zone_id = var.route53_zone_id vpc_id = var.vpc_id } - diff --git a/nomad-client.tf b/nomad-client.tf index 628589a..02ed4b1 100644 --- a/nomad-client.tf +++ b/nomad-client.tf @@ -18,7 +18,7 @@ resource "aws_launch_configuration" "nomad_client_lc" { aws_autoscaling_attachment.nomad_asg_attachment, aws_security_group.instances_security_group, ] - name = "${var.names_prefix}_nomad_client_lc" + name = "${var.name_prefix}_nomad_client_lc" image_id = var.client_ami_id instance_type = var.client_instance_type iam_instance_profile = aws_iam_instance_profile.ecr_role_instance_profile.name @@ -34,7 +34,7 @@ resource "aws_launch_configuration" "nomad_client_lc" { #------------------------------------------------------------------------------ resource "aws_autoscaling_group" "nomad_client_asg" { depends_on = [aws_launch_configuration.nomad_client_lc] - name = "${var.names_prefix}_nomad_client_asg" + name = "${var.name_prefix}_nomad_client_asg" launch_configuration = aws_launch_configuration.nomad_client_lc.name vpc_zone_identifier = var.subnets_ids min_size = var.client_asg_min_size @@ -43,9 +43,8 @@ resource "aws_autoscaling_group" "nomad_client_asg" { tags = [ { key = "Name" - value = "${var.names_prefix}_nomad_client_asg" + value = "${var.name_prefix}_nomad_client_asg" propagate_at_launch = true }, ] } - diff --git a/paas-server.tf b/paas-server.tf index f0e5f31..fe8cb75 100644 --- a/paas-server.tf +++ b/paas-server.tf @@ -8,7 +8,7 @@ data "template_file" "server_user_data" { region = var.region desired_servers = var.server_asg_desired_capacity domain = var.domain_name - asg_name = "${var.names_prefix}_server_asg" + asg_name = "${var.name_prefix}_server_asg" nomad_version = var.nomad_version } } @@ -21,7 +21,7 @@ resource "aws_launch_configuration" "server_lc" { aws_security_group.instances_security_group, aws_iam_instance_profile.ec2_describe_instance_profile, ] - name = "${var.names_prefix}_server_lc" + name = "${var.name_prefix}_server_lc" image_id = var.server_ami_id instance_type = var.server_instance_type iam_instance_profile = aws_iam_instance_profile.ec2_describe_instance_profile.name @@ -37,7 +37,7 @@ resource "aws_launch_configuration" "server_lc" { #------------------------------------------------------------------------------ resource "aws_autoscaling_group" "server_asg" { depends_on = [aws_launch_configuration.server_lc] - name = "${var.names_prefix}_server_asg" + name = "${var.name_prefix}_server_asg" launch_configuration = aws_launch_configuration.server_lc.name vpc_zone_identifier = var.subnets_ids min_size = var.server_asg_min_size @@ -46,7 +46,7 @@ resource "aws_autoscaling_group" "server_asg" { tags = [ { key = "Name" - value = "${var.names_prefix}_server_asg" + value = "${var.name_prefix}_server_asg" propagate_at_launch = true }, ] @@ -66,4 +66,3 @@ resource "aws_autoscaling_attachment" "nomad_asg_attachment" { autoscaling_group_name = aws_autoscaling_group.server_asg.id elb = aws_elb.nomad_elb.id } - diff --git a/security_groups.tf b/security_groups.tf index 8640b7a..a8814bd 100644 --- a/security_groups.tf +++ b/security_groups.tf @@ -8,7 +8,7 @@ resource "aws_security_group" "instances_security_group" { description = "Open ports on instances" vpc_id = var.vpc_id tags = { - Name = "${var.names_prefix}_instances_security_group" + Name = "${var.name_prefix}_instances_security_group" } } @@ -91,7 +91,7 @@ resource "aws_security_group" "elb_security_group" { description = "Open ports on Load Balanacers" vpc_id = var.vpc_id tags = { - Name = "${var.names_prefix}_elb_security_group" + Name = "${var.name_prefix}_elb_security_group" } } @@ -129,4 +129,3 @@ resource "aws_security_group_rule" "elb_security_group_allow_egress_traffic" { cidr_blocks = ["0.0.0.0/0"] security_group_id = aws_security_group.elb_security_group.id } - diff --git a/variables.tf b/variables.tf index e3107b0..295d3a0 100644 --- a/variables.tf +++ b/variables.tf @@ -1,18 +1,10 @@ -variable "names_prefix" { +variable "name_prefix" { description = "prefix for Resources Names" } #------------------------------------------------------------------------------ -# AWS CREDENTIALS AND REGION - NETWORK - SETTINGS +# NETWORK - SETTINGS #------------------------------------------------------------------------------ -variable "profile" { - description = "AWS API key credentials to use" -} - -variable "region" { - description = "AWS Region the infrastructure is hosted in" -} - variable "vpc_id" { description = "VPC ID" } @@ -126,4 +118,3 @@ variable "client_asg_desired_capacity" { variable "client_asg_max_size" { description = "Max Number of Instances of Nomad Client to Create" } -