Skip to content

Commit

Permalink
Add convenience flag on easy module to force distinct instance placement
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelaw320 committed Jan 12, 2024
1 parent a284600 commit 343f429
Show file tree
Hide file tree
Showing 27 changed files with 235 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ This module uses Semver.

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion examples/complete-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ $ terraform apply

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion examples/easy/ec2-alb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ To test that it's working:

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
6 changes: 4 additions & 2 deletions examples/easy/ec2-alb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module "vpc" {
enable_dhcp_options = true
dhcp_options_domain_name_servers = ["AmazonProvidedDNS"]

map_public_ip_on_launch = true

enable_ipv6 = var.enable_ipv6
public_subnet_assign_ipv6_address_on_creation = var.enable_ipv6
public_subnet_ipv6_prefixes = range(length(local.vpc_azs))
Expand Down Expand Up @@ -118,9 +120,9 @@ module "asg" {
max_size = 2
desired_capacity = 1
health_check_type = "EC2"
user_data = templatefile("../../templates/ec2_userdata.tpl", {
user_data = base64encode(templatefile("../../templates/ec2_userdata.tpl", {
ecs_cluster = module.ecs_cluster.name
})
}))
iam_instance_profile_arn = var.instance_profile_arn
}

Expand Down
72 changes: 72 additions & 0 deletions examples/easy/ec2-distinct/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# examples/easy/ec2-distinct

In this example we'll instantiate a simple nginx webserver running in **3 distinct instances**.

Task networking mode in this example is `host` so that the port specified in `templates/container_definitions.tpl` will be bind.
Means there cannot be more than 1 task in an instance. Do experiment with dynamic port mapping with `bridge` network mode.

This example will also create `vpc` and `security_group` and `autoscaling group`.

Note: Instance profile is required for EC2 to connect to ECS Cluster. See [`modules/iam/ecs-instance-profile`](https://github.com/HENNGE/terraform-aws-ecs/tree/main/modules/iam/ecs-instance-profile).

To test that it's working:
1. Go to EC2 console
1. Find the EC2 instance started by this example. (Search the name)
1. Go to the IP Address, you should see nginx hello world screen

## Usage

To run this example you need to execute:

```bash
$ terraform init
$ terraform plan
$ terraform apply
```


<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.74.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_asg"></a> [asg](#module\_asg) | terraform-aws-modules/autoscaling/aws | ~> 7.0 |
| <a name="module_easy_ec2_distinct_instance_mode"></a> [easy\_ec2\_distinct\_instance\_mode](#module\_easy\_ec2\_distinct\_instance\_mode) | ../../../modules/simple/ec2 | n/a |
| <a name="module_ec2_security_group"></a> [ec2\_security\_group](#module\_ec2\_security\_group) | terraform-aws-modules/security-group/aws | ~> 4.0 |
| <a name="module_ecs_cluster"></a> [ecs\_cluster](#module\_ecs\_cluster) | ../../.. | n/a |
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 5.0 |

## Resources

| Name | Type |
|------|------|
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
| [aws_ssm_parameter.ami_image](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_availability_zones"></a> [availability\_zones](#input\_availability\_zones) | Override automatic detection of availability zones | `list(string)` | `[]` | no |
| <a name="input_enable_ipv6"></a> [enable\_ipv6](#input\_enable\_ipv6) | Enable IPv6? | `bool` | `true` | no |
| <a name="input_instance_profile_arn"></a> [instance\_profile\_arn](#input\_instance\_profile\_arn) | Instance Profile to use for EC2 to join to ECS Cluster. See `modules/iam/ecs-instance-profile` | `string` | n/a | yes |

## Outputs

No outputs.
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->


103 changes: 103 additions & 0 deletions examples/easy/ec2-distinct/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Just Supporting Infrastructures

data "aws_availability_zones" "available" {}

data "aws_ssm_parameter" "ami_image" {
name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}

locals {
prefix = "easy-ec2"

vpc_cidr = "10.0.0.0/16"
discovered_azs = data.aws_availability_zones.available.names
vpc_azs = length(var.availability_zones) == 0 ? local.discovered_azs : var.availability_zones
}

module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"

name = "${local.prefix}-vpc"
cidr = local.vpc_cidr
azs = local.vpc_azs

public_subnets = [for i in range(length(local.vpc_azs)) : cidrsubnet(local.vpc_cidr, 8, i)]

enable_nat_gateway = false
enable_dhcp_options = true
dhcp_options_domain_name_servers = ["AmazonProvidedDNS"]

map_public_ip_on_launch = true

enable_ipv6 = var.enable_ipv6
public_subnet_assign_ipv6_address_on_creation = var.enable_ipv6
public_subnet_ipv6_prefixes = range(length(local.vpc_azs))
}

module "ec2_security_group" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 4.0"

name = "${local.prefix}-ec2-sg"
vpc_id = module.vpc.vpc_id


# Ingress for HTTP
ingress_cidr_blocks = ["0.0.0.0/0"]
ingress_ipv6_cidr_blocks = ["::/0"]
ingress_rules = ["http-80-tcp"]

# Allow all egress
egress_cidr_blocks = ["0.0.0.0/0"]
egress_ipv6_cidr_blocks = ["::/0"]
egress_rules = ["all-all"]
}

module "asg" {
source = "terraform-aws-modules/autoscaling/aws"
version = "~> 7.0"

name = "${local.prefix}-asg"

image_id = data.aws_ssm_parameter.ami_image.value
instance_type = "t2.micro"
security_groups = [module.ec2_security_group.security_group_id]
vpc_zone_identifier = module.vpc.public_subnets
min_size = 1
max_size = 5
desired_capacity = 3
health_check_type = "EC2"
user_data = base64encode(templatefile("../../templates/ec2_userdata.tpl", {
ecs_cluster = module.ecs_cluster.name
}))
iam_instance_profile_arn = var.instance_profile_arn
}

# This module usage starts here
module "ecs_cluster" {
source = "../../.."

name = "${local.prefix}-cluster"
}

module "easy_ec2_distinct_instance_mode" {
source = "../../../modules/simple/ec2"

name = "${local.prefix}-service"
cluster = module.ecs_cluster.name
cpu = 256
memory = 512
desired_count = 3
ignore_desired_count_changes = false

network_mode = "host"

distinct_instance = true

container_definitions = templatefile("../../templates/container_definitions.tpl", {
name = "${local.prefix}-cont"
cpu = 256
memory = 512
})
}
Empty file.
16 changes: 16 additions & 0 deletions examples/easy/ec2-distinct/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
variable "instance_profile_arn" {
description = "Instance Profile to use for EC2 to join to ECS Cluster. See `modules/iam/ecs-instance-profile`"
type = string
}

variable "availability_zones" {
description = "Override automatic detection of availability zones"
default = []
type = list(string)
}

variable "enable_ipv6" {
description = "Enable IPv6?"
default = true
type = bool
}
10 changes: 10 additions & 0 deletions examples/easy/ec2-distinct/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.74.0"
}
}
}
2 changes: 1 addition & 1 deletion examples/easy/ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ $ terraform apply

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
6 changes: 4 additions & 2 deletions examples/easy/ec2/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module "vpc" {
enable_dhcp_options = true
dhcp_options_domain_name_servers = ["AmazonProvidedDNS"]

map_public_ip_on_launch = true

enable_ipv6 = var.enable_ipv6
public_subnet_assign_ipv6_address_on_creation = var.enable_ipv6
public_subnet_ipv6_prefixes = range(length(local.vpc_azs))
Expand Down Expand Up @@ -66,9 +68,9 @@ module "asg" {
max_size = 2
desired_capacity = 1
health_check_type = "EC2"
user_data = templatefile("../../templates/ec2_userdata.tpl", {
user_data = base64encode(templatefile("../../templates/ec2_userdata.tpl", {
ecs_cluster = module.ecs_cluster.name
})
}))
iam_instance_profile_arn = var.instance_profile_arn
}

Expand Down
2 changes: 1 addition & 1 deletion examples/easy/fargate-alb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ $ terraform apply

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion examples/easy/fargate-spot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ $ terraform apply

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion examples/easy/fargate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $ terraform apply

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module "ecs_service_scaling" {

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module "ecs_service_scaling" {

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module "asg_scaling" {

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion modules/autoscaling/ecs-scheduled/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module "ecs_scaling_scheduled" {

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module "ecs_service_scaling" {

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion modules/core/ecs-autoscaling-target/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module "ecs_service_scaling_target" {

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
8 changes: 4 additions & 4 deletions modules/core/service/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ resource "aws_ecs_service" "main" {
dynamic "placement_constraints" {
for_each = var.service_placement_constraints
content {
type = lookup(service_placement_constraints.value, "type", null)
expression = lookup(service_placement_constraints.value, "expression", null)
type = lookup(placement_constraints.value, "type", null)
expression = lookup(placement_constraints.value, "expression", null)
}
}

Expand Down Expand Up @@ -173,8 +173,8 @@ resource "aws_ecs_service" "main_ignore_desired_count_changes" {
dynamic "placement_constraints" {
for_each = var.service_placement_constraints
content {
type = lookup(service_placement_constraints.value, "type", null)
expression = lookup(service_placement_constraints.value, "expression", null)
type = lookup(placement_constraints.value, "type", null)
expression = lookup(placement_constraints.value, "expression", null)
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/core/task/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Almost a 1-1 mapping to `resources`.

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.32.0 |

## Modules

Expand Down
Loading

0 comments on commit 343f429

Please sign in to comment.