/ modules / aws-eb-env / main.tf
main.tf
  1  locals {
  2    fqdn     = "${var.stage}.${var.dns_domain}"
  3    /* also used in deployment user policy */
  4    app_name = "${replace(var.dns_domain, ".", "-")}-app"
  5  }
  6  
  7  data "aws_availability_zones" "available" {
  8  }
  9  
 10  module "vpc" {
 11    source = "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=0.28.1"
 12  
 13    namespace  = ""
 14    stage      = var.stage
 15    name       = "${local.app_name}-vpc"
 16    cidr_block = "10.0.0.0/16"
 17  }
 18  
 19  module "subnets" {
 20    source = "git::https://github.com/cloudposse/terraform-aws-dynamic-subnets.git?ref=0.40.1"
 21  
 22    availability_zones      = slice(data.aws_availability_zones.available.names, 0, var.max_availability_zones)
 23    namespace               = ""
 24    stage                   = var.stage
 25    name                    = local.fqdn
 26    vpc_id                  = module.vpc.vpc_id
 27    igw_id                  = module.vpc.igw_id
 28    cidr_block              = module.vpc.vpc_cidr_block
 29    nat_gateway_enabled     = "false" # This costs a LOT
 30    map_public_ip_on_launch = "true"
 31  }
 32  
 33  module "eb_application" {
 34    source = "git::https://github.com/cloudposse/terraform-aws-elastic-beanstalk-application.git?ref=0.11.1"
 35  
 36    name        = local.app_name
 37    description = "${local.fqdn} application"
 38    stage       = var.stage
 39    namespace   = ""
 40  }
 41  
 42  module "eb_environment" {
 43    source = "git::https://github.com/cloudposse/terraform-aws-elastic-beanstalk-environment.git?ref=0.46.1"
 44  
 45    description         = "Dapp Discovery Store - ${local.fqdn}"
 46    name                = local.app_name
 47    stage               = var.stage
 48    region              = "us-east-1"
 49    solution_stack_name = var.stack_name
 50    keypair             = var.keypair_name
 51  
 52    loadbalancer_certificate_arn = var.cert_arn
 53  
 54    vpc_id               = module.vpc.vpc_id
 55    application_subnets  = module.subnets.public_subnet_ids
 56    loadbalancer_subnets = module.subnets.public_subnet_ids /* should be private */
 57    loadbalancer_security_groups       = [module.vpc.vpc_default_security_group_id]
 58    elastic_beanstalk_application_name = module.eb_application.elastic_beanstalk_application_name
 59  
 60    /* Access */
 61    ssh_listener_port           = "22"
 62    ssh_listener_enabled        = "true"
 63    ssh_source_restriction      = "0.0.0.0/0"
 64    associate_public_ip_address = "true"
 65  
 66    /* Application */
 67    application_port      = 8080
 68    http_listener_enabled = "true"
 69    healthcheck_url       = "/healthcheck"
 70  
 71    /* Environment */
 72    additional_settings = [
 73      for key, value in var.env_vars:
 74        {
 75          name      = key
 76          value     = value
 77          namespace = "aws:elasticbeanstalk:application:environment"
 78        }
 79    ]
 80  
 81    /* Deployment */
 82    updating_min_in_service = 1 /* min number of hosts up during updates */
 83    updating_max_batch      = 1 /* max number of hosts to deploy at once */
 84    rolling_update_type     = "Health" /* "Immutable" replaces instances */
 85  
 86    /* Scaling */
 87    instance_type          = var.instance_type
 88    autoscale_min          = var.autoscale_min /* min instances */
 89    autoscale_max          = var.autoscale_max /* max instances */
 90    autoscale_measure_name = "CPUUtilization"
 91    autoscale_statistic    = "Average"
 92    autoscale_unit         = "Percent"
 93    autoscale_lower_bound  = 20 /* min cpu usage to remove instance */
 94    autoscale_upper_bound  = 80 /* max cpu usage to add an instance */
 95  }
 96  
 97  /* DNS ------------------------------------------*/
 98  
 99  /* need to get the full DNS entries for the ELBs */
100  data "aws_elb" "main" {
101    name  = module.eb_environment.load_balancers[count.index]
102    count = 1
103  }