From 5fc69f2371e0b140d73a01dbd0a5802d209a8f99 Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Fri, 17 Jun 2016 11:45:27 -0700 Subject: [PATCH 1/4] default AZ for the region --- main.tf | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/main.tf b/main.tf index 1f7ccfcb..8b2952a3 100644 --- a/main.tf +++ b/main.tf @@ -56,8 +56,25 @@ variable "external_subnets" { } variable "availability_zones" { - description = "a comma-separated list of availability zones" - default = "us-west-2a,us-west-2b,us-west-2c" + description = "a comma-separated list of availability zones, defaults to all AZ of the region" + default = "" +} + +variable "default_availability_zones" { + description = "a mapping of the default availability zones for each AWS region" + + default = { + us-east-1 = "us-east-1a,us-east-1b,us-east-1c,us-east-1e" + us-west-1 = "us-west-1a,us-west-1b" + us-west-2 = "us-west-2a,us-west-2b,us-west-2c" + eu-west-1 = "eu-west-1a,eu-west-1b,eu-west-1c" + eu-central-1 = "eu-central-1a,eu-central-1b" + 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" + sa-east-1 = "sa-east-1a,sa-east-1b,sa-east-1c" + } } variable "ecs_instance_type" { @@ -132,16 +149,13 @@ variable "default_ecs_ami" { } module "vpc" { - source = "./vpc" - name = "${var.name}" - - cidr = "${var.cidr}" - internal_subnets = "${var.internal_subnets}" - external_subnets = "${var.external_subnets}" - - availability_zones = "${var.availability_zones}" - - environment = "${var.environment}" + source = "./vpc" + name = "${var.name}" + cidr = "${var.cidr}" + internal_subnets = "${var.internal_subnets}" + external_subnets = "${var.external_subnets}" + availability_zones = "${coalesce(var.availability_zones, lookup(var.default_availability_zones, var.region))}" + environment = "${var.environment}" } module "security_groups" { From 743a74af47ee2b69e10d5c2eee8ec0c41a672ad8 Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Fri, 17 Jun 2016 17:16:30 -0700 Subject: [PATCH 2/4] add stack//defaults module --- defaults/main.tf | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ dhcp/main.tf | 2 +- main.tf | 58 ++++++++----------------------- s3-logs/main.tf | 22 ++---------- vpc/main.tf | 5 +++ 5 files changed, 112 insertions(+), 64 deletions(-) create mode 100644 defaults/main.tf diff --git a/defaults/main.tf b/defaults/main.tf new file mode 100644 index 00000000..5f976cf8 --- /dev/null +++ b/defaults/main.tf @@ -0,0 +1,89 @@ +/** + * This module is used to set configuration defaults for the AWS infrastructure. + * It doesn't provide much value when used on its own because terraform makes it + * hard to do dynamic generations of things like subnets, for now it's used as + * a helper module for the stack. + * + * Usage: + * + * module "defaults" { + * source = "github.com/segmentio/stack/defaults" + * region = "us-east-1" + * cidr = "10.0.0.0/16" + * } + * + */ + +variable "region" { + description = "The AWS region" +} + +variable "cidr" { + description = "The CIDR block to provision for the VPC" +} + +variable "default_availability_zones" { + description = "A mapping of the default availability zones for each AWS region" + + default = { + us-east-1 = "us-east-1a,us-east-1b,us-east-1c,us-east-1e" + us-west-1 = "us-west-1a,us-west-1b" + us-west-2 = "us-west-2a,us-west-2b,us-west-2c" + eu-west-1 = "eu-west-1a,eu-west-1b,eu-west-1c" + eu-central-1 = "eu-central-1a,eu-central-1b" + 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" + sa-east-1 = "sa-east-1a,sa-east-1b,sa-east-1c" + } +} + +variable "default_ecs_ami" { + default = { + us-east-1 = "ami-5f3ff932" + us-west-1 = "ami-31c08551" + us-west-2 = "ami-f3985d93" + eu-west-1 = "ami-ab4bd5d8" + eu-central-1 = "ami-6c58b103" + ap-northeast-1 = "ami-a69d68c7" + ap-northeast-2 = "ami-7b2de615" + ap-southeast-1 = "ami-550dde36" + ap-southeast-2 = "ami-c799b0a4" + sa-east-1 = "ami-0274fe6e" + } +} + +# http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/enable-access-logs.html#attach-bucket-policy +variable "default_log_account_ids" { + default = { + us-east-1 = "127311923021" + us-west-2 = "797873946194" + us-west-1 = "027434742980" + eu-west-1 = "156460612806" + eu-central-1 = "054676820928" + ap-southeast-1 = "114774131450" + ap-northeast-1 = "582318560864" + ap-southeast-2 = "783225319266" + ap-northeast-2 = "600734575887" + sa-east-1 = "507241528517" + us-gov-west-1 = "048591011584" + cn-north-1 = "638102146993" + } +} + +output "availability_zones" { + value = "${lookup(var.default_availability_zones, var.region)}" +} + +output "domain_name_servers" { + value = "${cidrhost(var.cidr, 2)}" +} + +output "ecs_ami" { + value = "${lookup(var.default_ecs_ami, var.region)}" +} + +output "s3_logs_account_id" { + value = "${lookup(var.default_log_account_ids, var.region)}" +} diff --git a/dhcp/main.tf b/dhcp/main.tf index 5a54ee68..79ac7d0d 100644 --- a/dhcp/main.tf +++ b/dhcp/main.tf @@ -7,7 +7,7 @@ variable "vpc_id" { } variable "servers" { - description = "A coma separated list of the IP addresses of internal DHCP servers" + description = "A comma separated list of the IP addresses of internal DHCP servers" } resource "aws_vpc_dhcp_options" "dns_resolver" { diff --git a/main.tf b/main.tf index 8b2952a3..6ca6a991 100644 --- a/main.tf +++ b/main.tf @@ -41,42 +41,25 @@ variable "region" { } variable "cidr" { - description = "the CIDR block to provision for the VPC" + description = "the CIDR block to provision for the VPC, if set to something other than the default, both internal_subnets and external_subnets have to be defined as well" default = "10.30.0.0/16" } variable "internal_subnets" { - description = "a comma-separated list of CIDRs for internal subnets in your VPC" + description = "a comma-separated list of CIDRs for internal subnets in your VPC, must be set if the cidr variable is defined, needs to have as many elements as there are availability zones" default = "10.30.0.0/19,10.30.64.0/19,10.30.128.0/19" } variable "external_subnets" { - description = "a comma-separated list of CIDRs for external subnets in your VPC" + description = "a comma-separated list of CIDRs for external subnets in your VPC, must be set if the cidr variable is defined, needs to have as many elements as there are availability zones" default = "10.30.32.0/20,10.30.96.0/20,10.30.160.0/20" } variable "availability_zones" { - description = "a comma-separated list of availability zones, defaults to all AZ of the region" + description = "a comma-separated list of availability zones, defaults to all AZ of the region, if set to something other than the defaults, both internal_subnets and external_subnets have to be defined as well" default = "" } -variable "default_availability_zones" { - description = "a mapping of the default availability zones for each AWS region" - - default = { - us-east-1 = "us-east-1a,us-east-1b,us-east-1c,us-east-1e" - us-west-1 = "us-west-1a,us-west-1b" - us-west-2 = "us-west-2a,us-west-2b,us-west-2c" - eu-west-1 = "eu-west-1a,eu-west-1b,eu-west-1c" - eu-central-1 = "eu-central-1a,eu-central-1b" - 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" - sa-east-1 = "sa-east-1a,sa-east-1b,sa-east-1c" - } -} - variable "ecs_instance_type" { description = "the instance type to use for your default ecs cluster" default = "m4.large" @@ -122,7 +105,7 @@ variable "ecs_docker_auth_data" { } variable "ecs_security_groups" { - description = "A coma separated list of security groups from which ingest traffic will be allowed on the ECS cluster, it defaults to allowing ingress traffic on port 22 and coming grom the ELBs" + description = "A comma separated list of security groups from which ingest traffic will be allowed on the ECS cluster, it defaults to allowing ingress traffic on port 22 and coming grom the ELBs" default = "" } @@ -131,21 +114,10 @@ variable "ecs_ami" { default = "" } -variable "default_ecs_ami" { - description = "A mapping of AWS regions to the default ECS AMIs" - - default = { - us-east-1 = "ami-5f3ff932" - us-west-1 = "ami-31c08551" - us-west-2 = "ami-f3985d93" - eu-west-1 = "ami-ab4bd5d8" - eu-central-1 = "ami-6c58b103" - ap-northeast-1 = "ami-a69d68c7" - ap-northeast-2 = "ami-7b2de615" - ap-southeast-1 = "ami-550dde36" - ap-southeast-2 = "ami-c799b0a4" - sa-east-1 = "ami-0274fe6e" - } +module "defaults" { + source = "./defaults" + region = "${var.region}" + cidr = "${var.cidr}" } module "vpc" { @@ -154,7 +126,7 @@ module "vpc" { cidr = "${var.cidr}" internal_subnets = "${var.internal_subnets}" external_subnets = "${var.external_subnets}" - availability_zones = "${coalesce(var.availability_zones, lookup(var.default_availability_zones, var.region))}" + availability_zones = "${coalesce(var.availability_zones, module.defaults.availability_zones)}" environment = "${var.environment}" } @@ -180,7 +152,7 @@ module "dhcp" { source = "./dhcp" name = "${module.dns.name}" vpc_id = "${module.vpc.id}" - servers = "${coalesce(var.domain_name_servers, cidrhost(var.cidr, 2))}" + servers = "${coalesce(var.domain_name_servers, module.defaults.domain_name_servers)}" } module "dns" { @@ -200,7 +172,7 @@ module "ecs_cluster" { name = "default" environment = "${var.environment}" vpc_id = "${module.vpc.id}" - image_id = "${coalesce(var.ecs_ami, lookup(var.default_ecs_ami, var.region))}" + image_id = "${coalesce(var.ecs_ami, module.defaults.ecs_ami)}" subnet_ids = "${module.vpc.internal_subnets}" key_name = "${var.key_name}" instance_type = "${var.ecs_instance_type}" @@ -209,7 +181,7 @@ module "ecs_cluster" { max_size = "${var.ecs_max_size}" desired_capacity = "${var.ecs_desired_capacity}" region = "${var.region}" - availability_zones = "${var.availability_zones}" + availability_zones = "${module.vpc.availability_zones}" root_volume_size = "${var.ecs_root_volume_size}" docker_volume_size = "${var.ecs_docker_volume_size}" docker_auth_type = "${var.ecs_docker_auth_type}" @@ -221,7 +193,7 @@ module "s3_logs" { source = "./s3-logs" name = "${var.name}" environment = "${var.environment}" - region = "${var.region}" + account_id = "${module.defaults.s3_logs_account_id}" } // The region in which the infra lives. @@ -286,7 +258,7 @@ output "cluster" { // The VPC availability zones. output "availability_zones" { - value = "${var.availability_zones}" + value = "${module.vpc.availability_zones}" } // The VPC security group ID. diff --git a/s3-logs/main.tf b/s3-logs/main.tf index d8086c4f..caf0da0b 100644 --- a/s3-logs/main.tf +++ b/s3-logs/main.tf @@ -4,25 +4,7 @@ variable "name" { variable "environment" { } -variable "region" { -} - -# http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/enable-access-logs.html#attach-bucket-policy -variable "log_account_ids" { - default = { - us-east-1 = "127311923021" - us-west-2 = "797873946194" - us-west-1 = "027434742980" - eu-west-1 = "156460612806" - eu-central-1 = "054676820928" - ap-southeast-1 = "114774131450" - ap-northeast-1 = "582318560864" - ap-southeast-2 = "783225319266" - ap-northeast-2 = "600734575887" - sa-east-1 = "507241528517" - us-gov-west-1 = "048591011584" - cn-north-1 = "638102146993" - } +variable "account_id" { } resource "template_file" "policy" { @@ -30,7 +12,7 @@ resource "template_file" "policy" { vars = { bucket = "${var.name}-${var.environment}-logs" - account_id = "${lookup(var.log_account_ids, var.region)}" + account_id = "${var.account_id}" } } diff --git a/vpc/main.tf b/vpc/main.tf index 62aa037f..af459e65 100644 --- a/vpc/main.tf +++ b/vpc/main.tf @@ -160,3 +160,8 @@ output "internal_subnets" { output "security_group" { value = "${aws_vpc.main.default_security_group_id}" } + +// The list of availability zones of the VPC. +output "availability_zones" { + value = "${join(",", aws_subnet.external.*.availability_zone)}" +} From bc928afa58b19bdaf02fb85749c71d82abaee1e6 Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Fri, 17 Jun 2016 17:25:48 -0700 Subject: [PATCH 3/4] give up on too much complexity, add better documentation --- defaults/main.tf | 21 --------------------- main.tf | 6 +++--- 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/defaults/main.tf b/defaults/main.tf index 5f976cf8..bd0d6892 100644 --- a/defaults/main.tf +++ b/defaults/main.tf @@ -22,23 +22,6 @@ variable "cidr" { description = "The CIDR block to provision for the VPC" } -variable "default_availability_zones" { - description = "A mapping of the default availability zones for each AWS region" - - default = { - us-east-1 = "us-east-1a,us-east-1b,us-east-1c,us-east-1e" - us-west-1 = "us-west-1a,us-west-1b" - us-west-2 = "us-west-2a,us-west-2b,us-west-2c" - eu-west-1 = "eu-west-1a,eu-west-1b,eu-west-1c" - eu-central-1 = "eu-central-1a,eu-central-1b" - 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" - sa-east-1 = "sa-east-1a,sa-east-1b,sa-east-1c" - } -} - variable "default_ecs_ami" { default = { us-east-1 = "ami-5f3ff932" @@ -72,10 +55,6 @@ variable "default_log_account_ids" { } } -output "availability_zones" { - value = "${lookup(var.default_availability_zones, var.region)}" -} - output "domain_name_servers" { value = "${cidrhost(var.cidr, 2)}" } diff --git a/main.tf b/main.tf index 6ca6a991..658ed29e 100644 --- a/main.tf +++ b/main.tf @@ -36,7 +36,7 @@ variable "domain_name_servers" { } variable "region" { - description = "the AWS region" + description = "the AWS region in which resources are created, you must set the availability_zones variable as well if you define this value to something other than the default" default = "us-west-2" } @@ -57,7 +57,7 @@ variable "external_subnets" { variable "availability_zones" { description = "a comma-separated list of availability zones, defaults to all AZ of the region, if set to something other than the defaults, both internal_subnets and external_subnets have to be defined as well" - default = "" + default = "us-west-2a,us-west-2b,us-west-2c" } variable "ecs_instance_type" { @@ -126,7 +126,7 @@ module "vpc" { cidr = "${var.cidr}" internal_subnets = "${var.internal_subnets}" external_subnets = "${var.external_subnets}" - availability_zones = "${coalesce(var.availability_zones, module.defaults.availability_zones)}" + availability_zones = "${var.availability_zones}" environment = "${var.environment}" } From 6066022bcd5b54f3f498bc37a7d4258da793d71d Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Fri, 17 Jun 2016 17:26:17 -0700 Subject: [PATCH 4/4] generate docs --- docs.md | 57 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/docs.md b/docs.md index 9dfd5798..35d58321 100644 --- a/docs.md +++ b/docs.md @@ -19,18 +19,16 @@ | Name | Description | Default | Required | |------|-------------|:-----:|:-----:| -| base_ami | | `"ami-729c5912"` | no | -| ecs_ami | | `"ami-f3985d93"` | no | | name | the name of your stack, e.g. "segment" | - | yes | | environment | the name of your environment, e.g. "prod-west" | - | yes | | key_name | the name of the ssh key to use, e.g. "internal-key" | - | yes | | domain_name | the internal DNS name to use with services | `"stack.local"` | no | | domain_name_servers | the internal DNS servers, defaults to the internal route53 server of the VPC | `""` | no | -| region | the AWS region | `"us-west-2"` | no | -| cidr | the CIDR block to provision for the VPC | `"10.30.0.0/16"` | no | -| internal_subnets | a comma-separated list of CIDRs for internal subnets in your VPC | `"10.30.0.0/19,10.30.64.0/19,10.30.128.0/19"` | no | -| external_subnets | a comma-separated list of CIDRs for external subnets in your VPC | `"10.30.32.0/20,10.30.96.0/20,10.30.160.0/20"` | no | -| availability_zones | a comma-separated list of availability zones | `"us-west-2a,us-west-2b,us-west-2c"` | no | +| region | the AWS region in which resources are created, you must set the availability_zones variable as well if you define this value to something other than the default | `"us-west-2"` | no | +| cidr | the CIDR block to provision for the VPC, if set to something other than the default, both internal_subnets and external_subnets have to be defined as well | `"10.30.0.0/16"` | no | +| internal_subnets | a comma-separated list of CIDRs for internal subnets in your VPC, must be set if the cidr variable is defined, needs to have as many elements as there are availability zones | `"10.30.0.0/19,10.30.64.0/19,10.30.128.0/19"` | no | +| external_subnets | a comma-separated list of CIDRs for external subnets in your VPC, must be set if the cidr variable is defined, needs to have as many elements as there are availability zones | `"10.30.32.0/20,10.30.96.0/20,10.30.160.0/20"` | no | +| availability_zones | a comma-separated list of availability zones, defaults to all AZ of the region, if set to something other than the defaults, both internal_subnets and external_subnets have to be defined as well | `"us-west-2a,us-west-2b,us-west-2c"` | no | | ecs_instance_type | the instance type to use for your default ecs cluster | `"m4.large"` | no | | ecs_min_size | the minimum number of instances to use in the default ecs cluster | `3` | no | | ecs_max_size | the maximum number of instances to use in the default ecs cluster | `100` | no | @@ -39,7 +37,8 @@ | ecs_docker_volume_size | the size of the ecs instance docker volume | `25` | no | | ecs_docker_auth_type | The docker auth type, see https://godoc.org/github.com/aws/amazon-ecs-agent/agent/engine/dockerauth for the possible values | `""` | no | | ecs_docker_auth_data | A JSON object providing the docker auth data, see https://godoc.org/github.com/aws/amazon-ecs-agent/agent/engine/dockerauth for the supported formats | `""` | no | -| ecs_security_groups | A coma separated list of security groups from which ingest traffic will be allowed on the ECS cluster, it defaults to allowing ingress traffic on port 22 and coming grom the ELBs | `""` | no | +| ecs_security_groups | A comma separated list of security groups from which ingest traffic will be allowed on the ECS cluster, it defaults to allowing ingress traffic on port 22 and coming grom the ELBs | `""` | no | +| ecs_ami | The AMI that will be used to launch EC2 instances in the ECS cluster | `""` | no | ## Outputs @@ -75,7 +74,7 @@ Usage: module "bastion" { - source = "github.com/segmentio/stack" + source = "github.com/segmentio/stack/bastion" region = "us-west-2" security_groups = "sg-1,sg-2" vpc_id = "vpc-12" @@ -104,6 +103,40 @@ |------|-------------| | external_ip | Bastion external IP address. | +# defaults + + This module is used to set configuration defaults for the AWS infrastructure. + It doesn't provide much value when used on its own because terraform makes it + hard to do dynamic generations of things like subnets, for now it's used as + a helper module for the stack. + + Usage: + + module "defaults" { + source = "github.com/segmentio/stack/defaults" + region = "us-east-1" + cidr = "10.0.0.0/16" + } + + + +## Inputs + +| Name | Description | Default | Required | +|------|-------------|:-----:|:-----:| +| region | The AWS region | - | yes | +| cidr | The CIDR block to provision for the VPC | - | yes | +| default_ecs_ami | | - | yes | +| default_log_account_ids | | - | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| domain_name_servers | | +| ecs_ami | | +| s3_logs_account_id | | + # dhcp @@ -113,7 +146,7 @@ |------|-------------|:-----:|:-----:| | name | The domain name to setup DHCP for | - | yes | | vpc_id | The ID of the VPC to setup DHCP for | - | yes | -| servers | A coma separated list of the IP addresses of internal DHCP servers | - | yes | +| servers | A comma separated list of the IP addresses of internal DHCP servers | - | yes | # dns @@ -304,8 +337,7 @@ |------|-------------|:-----:|:-----:| | name | | - | yes | | environment | | - | yes | -| region | | - | yes | -| log_account_ids | | - | yes | +| account_id | | - | yes | ## Outputs @@ -444,6 +476,7 @@ | external_subnets | A comma-separated list of subnet IDs. | | internal_subnets | A comma-separated list of subnet IDs. | | security_group | The default VPC security group ID. | +| availability_zones | The list of availability zones of the VPC. | # web-service