diff --git a/defaults/main.tf b/defaults/main.tf new file mode 100644 index 00000000..bd0d6892 --- /dev/null +++ b/defaults/main.tf @@ -0,0 +1,68 @@ +/** + * 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_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 "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/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 diff --git a/main.tf b/main.tf index 1f7ccfcb..658ed29e 100644 --- a/main.tf +++ b/main.tf @@ -36,27 +36,27 @@ 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" } 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" + 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 = "us-west-2a,us-west-2b,us-west-2c" } @@ -105,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 = "" } @@ -114,34 +114,20 @@ 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" { - source = "./vpc" - name = "${var.name}" - - cidr = "${var.cidr}" - internal_subnets = "${var.internal_subnets}" - external_subnets = "${var.external_subnets}" - + 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}" + environment = "${var.environment}" } module "security_groups" { @@ -166,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" { @@ -186,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}" @@ -195,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}" @@ -207,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. @@ -272,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)}" +}