A terraform module for create ECS on Spot Fleet. This is a demo repository. The outline is as following:
- Bid on Spot Fleet and launch instances that spans two AZs.
- Started instances constitute an ECS cluster.
- Invoked containers support dynamic port mapping by ALB.
By using the bundled ruby script, you can try ECS on Spot Fleet fastest.
$ git clone https://github.com/wata727/tf_aws_ecs_on_spotfleet.git
$ cd tf_aws_ecs_on_spotfleet/cli
$ bundle install
$ ruby wizard.rb generate
create template.tf
$ terraform init
$ terraform apply
This script generates Terraform template. By default, it requests the cheapest spot price with the two subnets in default VPC on us-east-1
. Also, if you do not have a key pair in us-east-1, it will automatically generate demo-app.pem
. Since AWS credentials are required for this operation, please use environment variables or shared credentials.
If you want to delete this cluster, please run the following:
$ terraform destroy
Required
vpc
- VPC id for ECS clustersubnets
- List of subnet ids for ECS cluster, please choose 2 subnetskey_name
- Name of key pair for SSH login to ECS cluster instances
Optional
ami
- ECS cluster instance AMI id, default is Amazon ECS-optimized AMI inus-east-1
app_name
- Your application name, default isdemo-app
image
- Your docker image name, default it ECS PHP Simple Appcontainer_port
- Port number exposed by container, default is 80service_count
- Number of containers, default is 3cpu_unit
- Number of cpu_units for container, default is 128memory
- Number of memory for container, default is 128spot_prices
- Bid amount to spot fleet, please choose 2 prices, default is$0.03
strategy
- Instance placement strategy name, default isdiversified
instance_count
- Number of instances, default is 3instance_type
- Instance type launched by Spot Fleet. default ism3.medium
volume_size
- Root volume size, default is 16https
- Whether the load balancer should listen to https requests, default isfalse
app_certificate_arn
- The ARN of the ssl certificate, default is emptyapp_ssl_policy
- The ssl policy, default isELBSecurityPolicy-2015-05
valid_until
- limit of Spot Fleet request, default is2020-12-15T00:00:00Z
Like other modules, you can easily start ECS cluster by adding this module to your template with required parameters.
provider "aws" {
region = "us-east-1"
}
module "ecs_on_spotfleet" {
source = "github.com/wata727/tf_aws_ecs_on_spotfleet"
vpc = "vpc-12345"
subnets = ["subnet-12345", "subnet-abcde"]
spot_prices = ["0.03", "0.02"]
key_name = "demo-app"
}
output "endpoint" {
value = "${module.ecs_on_spotfleet.endpoint}"
}
This module is very simple, please remodel and create your own module.