Skip to content

Instantly share code, notes, and snippets.

@zytek
Last active February 7, 2019 21:22
Show Gist options
  • Save zytek/d1e49e97c0ff1dcb965ab712351b035c to your computer and use it in GitHub Desktop.
Save zytek/d1e49e97c0ff1dcb965ab712351b035c to your computer and use it in GitHub Desktop.
####
####
# Terraform manifest that creates spot fleet by copying attributed from currently running instance.
# Is has a lot of copy-pasing due to two terraform 0.11 bugs
# - tags with dots in key names forced to copy-paste them in this manifest
# - instance data source does not provide user data script so it had to be manually copied to a file along with this manifest
# both bugs are being worker on in terraform 0.12
data "aws_caller_identity" "current" {}
# these two data sources point to a running kops provisioned instance
# this way we can get some info programatically and don't need to put it in this TF file
data "aws_instance" "donor" {
instance_id = "i-0288afcbc8699e3eb"
}
data "aws_iam_instance_profile" "iam" {
name = "${data.aws_instance.donor.iam_instance_profile}"
}
resource "aws_spot_fleet_request" "sandbox-fleet1" {
target_capacity = 12
iam_fleet_role = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/aws-ec2-spot-fleet-tagging-role"
terminate_instances_with_expiration = false
replace_unhealthy_instances = true
valid_until = "2022-12-31T00:00:00Z"
instance_pools_to_use_count = 2
"launch_specification" {
instance_type = "r3.xlarge"
weighted_capacity = "4"
ami = "${data.aws_instance.donor.ami}"
availability_zone = "${data.aws_instance.donor.availability_zone}"
subnet_id = "${data.aws_instance.donor.subnet_id}"
key_name = "${data.aws_instance.donor.key_name}"
associate_public_ip_address = true
iam_instance_profile_arn = "${data.aws_iam_instance_profile.iam.arn}"
vpc_security_group_ids = [
"${data.aws_instance.donor.vpc_security_group_ids}",
]
user_data = "${file("ud-dev.sh")}"
root_block_device {
volume_size = "80"
volume_type = "gp2"
}
tags = {
Name = "${format("%s.%s", "nodes-fleet", lookup(data.aws_instance.donor.tags, "KubernetesCluster"))}"
"k8s.io/cluster-autoscaler/node-template/label/kops.k8s.io/instancegroup" = "nodes-spot-r4"
"k8s.io/role/node" = "1"
env = "${lookup(data.aws_instance.donor.tags, "env")}"
KubernetesCluster = "${lookup(data.aws_instance.donor.tags, "KubernetesCluster")}"
}
}
"launch_specification" {
instance_type = "r4.xlarge"
weighted_capacity = "4"
ami = "${data.aws_instance.donor.ami}"
availability_zone = "${data.aws_instance.donor.availability_zone}"
subnet_id = "${data.aws_instance.donor.subnet_id}"
key_name = "${data.aws_instance.donor.key_name}"
associate_public_ip_address = true
iam_instance_profile_arn = "${data.aws_iam_instance_profile.iam.arn}"
vpc_security_group_ids = [
"${data.aws_instance.donor.vpc_security_group_ids}",
]
user_data = "${file("ud-dev.sh")}"
root_block_device {
volume_size = "80"
volume_type = "gp2"
}
tags = {
Name = "${format("%s.%s", "nodes-fleet", lookup(data.aws_instance.donor.tags, "KubernetesCluster"))}"
"k8s.io/cluster-autoscaler/node-template/label/kops.k8s.io/instancegroup" = "nodes-spot-r4"
"k8s.io/role/node" = "1"
env = "${lookup(data.aws_instance.donor.tags, "env")}"
KubernetesCluster = "${lookup(data.aws_instance.donor.tags, "KubernetesCluster")}"
}
}
"launch_specification" {
instance_type = "r5.xlarge"
weighted_capacity = "4"
ami = "${data.aws_instance.donor.ami}"
availability_zone = "${data.aws_instance.donor.availability_zone}"
subnet_id = "${data.aws_instance.donor.subnet_id}"
key_name = "${data.aws_instance.donor.key_name}"
associate_public_ip_address = true
iam_instance_profile_arn = "${data.aws_iam_instance_profile.iam.arn}"
vpc_security_group_ids = [
"${data.aws_instance.donor.vpc_security_group_ids}",
]
# this file containst copy-pasted user-data script from another kops-provisioned instance
user_data = "${file("ud-dev.sh")}"
root_block_device {
volume_size = "80"
volume_type = "gp2"
}
tags = {
Name = "${format("%s.%s", "nodes-fleet", lookup(data.aws_instance.donor.tags, "KubernetesCluster"))}"
"k8s.io/cluster-autoscaler/node-template/label/kops.k8s.io/instancegroup" = "nodes-spot-r4"
"k8s.io/role/node" = "1"
env = "${lookup(data.aws_instance.donor.tags, "env")}"
KubernetesCluster = "${lookup(data.aws_instance.donor.tags, "KubernetesCluster")}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment