-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for spot instances (#55)
* Add support for spot instances * Add test for spot instances
- Loading branch information
1 parent
eb59c40
commit 931e19f
Showing
13 changed files
with
327 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
data "cloudinit_config" "webserver_init" { | ||
gzip = false | ||
base64_encode = true | ||
|
||
part { | ||
content_type = "text/cloud-config" | ||
content = join( | ||
"\n", | ||
[ | ||
"#cloud-config", | ||
yamlencode( | ||
{ | ||
"package_update" : true, | ||
packages : [ | ||
"xinetd", | ||
"net-tools" | ||
] | ||
write_files : [ | ||
{ | ||
path : "/etc/xinetd.d/http" | ||
permissions : "0600" | ||
content : file("${path.module}/xinetd.d.http") | ||
}, | ||
{ | ||
path : "/usr/local/bin/httpd" | ||
permissions : "0755" | ||
content : file("${path.module}/httpd.sh") | ||
} | ||
] | ||
runcmd : [ | ||
"systemctl start xinetd" | ||
] | ||
} | ||
) | ||
] | ||
) | ||
} | ||
} | ||
|
||
data "aws_ami" "ubuntu" { | ||
most_recent = true | ||
|
||
filter { | ||
name = "name" | ||
values = ["ubuntu/images/hvm-ssd/ubuntu-${var.ubuntu_codename}-*"] | ||
} | ||
|
||
filter { | ||
name = "architecture" | ||
values = ["x86_64"] | ||
} | ||
|
||
filter { | ||
name = "virtualization-type" | ||
values = ["hvm"] | ||
} | ||
|
||
filter { | ||
name = "state" | ||
values = [ | ||
"available" | ||
] | ||
} | ||
|
||
owners = ["099720109477"] # Canonical | ||
} | ||
|
||
data "aws_iam_policy_document" "webserver_permissions" { | ||
statement { | ||
actions = ["ec2:Describe*"] | ||
resources = ["*"] | ||
} | ||
} | ||
|
||
data "aws_route53_zone" "website" { | ||
name = var.dns_zone | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env bash | ||
|
||
http_response () { | ||
HTTP_CODE=$1 | ||
MESSAGE=${2:-Message Undefined} | ||
length=$((${#MESSAGE} + 2)) | ||
if [[ "$HTTP_CODE" -eq 503 ]]; then | ||
echo -en "HTTP/1.1 503 Service Unavailable\r\n" | ||
elif [[ "$HTTP_CODE" -eq 200 ]]; then | ||
echo -en "HTTP/1.1 200 OK\r\n" | ||
else | ||
echo -en "HTTP/1.1 ${HTTP_CODE} UNKNOWN\r\n" | ||
fi | ||
echo -en "Content-Type: text/plain\r\n" | ||
echo -en "Connection: close\r\n" | ||
echo -en "Content-Length: ${length}\r\n" | ||
echo -en "\r\n" | ||
echo -en "$MESSAGE" | ||
echo -en "\r\n" | ||
sleep 0.1 | ||
exit 0 | ||
} | ||
|
||
http_response 200 "Success Message" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
resource "aws_key_pair" "test" { | ||
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDpgAP1z1Lxg9Uv4tam6WdJBcAftZR4ik7RsSr6aNXqfnTj4civrhd/q8qMqF6wL//3OujVDZfhJcffTzPS2XYhUxh/rRVOB3xcqwETppdykD0XZpkHkc8XtmHpiqk6E9iBI4mDwYcDqEg3/vrDAGYYsnFwWmdDinxzMH1Gei+NPTmTqU+wJ1JZvkw3WBEMZKlUVJC/+nuv+jbMmCtm7sIM4rlp2wyzLWYoidRNMK97sG8+v+mDQol/qXK3Fuetj+1f+vSx2obSzpTxL4RYg1kS6W1fBlSvstDV5bQG4HvywzN5Y8eCpwzHLZ1tYtTycZEApFdy+MSfws5vPOpggQlWfZ4vA8ujfWAF75J+WABV4DlSJ3Ng6rLMW78hVatANUnb9s4clOS8H6yAjv+bU3OElKBkQ10wNneoFIMOA3grjPvPp5r8dI0WDXPIznJThDJO5yMCy3OfCXlu38VDQa1sjVj1zAPG+Vn2DsdVrl50hWSYSB17Zww0MYEr8N5rfFE= aleks@MediaPC" | ||
} | ||
|
||
module "lb" { | ||
source = "../../" | ||
providers = { | ||
aws = aws | ||
aws.dns = aws | ||
} | ||
service_name = "website" | ||
subnets = var.lb_subnet_ids | ||
ami = data.aws_ami.ubuntu.id | ||
backend_subnets = var.backend_subnet_ids | ||
asg_name = var.asg_name | ||
asg_min_size = 2 | ||
on_demand_base_capacity = 1 | ||
internet_gateway_id = var.internet_gateway_id | ||
zone_id = data.aws_route53_zone.website.zone_id | ||
dns_a_records = var.dns_a_records | ||
key_pair_name = aws_key_pair.test.key_name | ||
userdata = data.cloudinit_config.webserver_init.rendered | ||
health_check_type = "ELB" | ||
instance_profile_permissions = data.aws_iam_policy_document.webserver_permissions.json | ||
instance_role_name = var.instance_role_name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
output "network_subnet_public_ids" { | ||
value = var.lb_subnet_ids | ||
} | ||
|
||
output "network_subnet_private_ids" { | ||
value = var.backend_subnet_ids | ||
} | ||
|
||
output "network_subnet_all_ids" { | ||
value = concat(var.backend_subnet_ids, var.lb_subnet_ids) | ||
} | ||
|
||
output "asg_name" { | ||
value = module.lb.asg_name | ||
} | ||
|
||
output "instance_profile_name" { | ||
value = module.lb.instance_profile_name | ||
} | ||
|
||
output "load_balancer_dns_name" { | ||
value = module.lb.load_balancer_dns_name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
provider "aws" { | ||
assume_role { | ||
role_arn = var.role_arn | ||
} | ||
region = var.region | ||
default_tags { | ||
tags = { | ||
"created_by" : "infrahouse/terraform-aws-website-pod" # GitHub repository that created a resource | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 5.11" | ||
} | ||
cloudinit = { | ||
source = "hashicorp/cloudinit" | ||
version = "~> 2.3" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
variable "region" {} | ||
variable "role_arn" {} | ||
variable "dns_a_records" { | ||
default = ["", "www", "bogus-test-stuff"] | ||
} | ||
variable "dns_zone" {} | ||
variable "ubuntu_codename" {} | ||
variable "asg_name" { default = null } | ||
|
||
variable "backend_subnet_ids" {} | ||
variable "lb_subnet_ids" {} | ||
variable "internet_gateway_id" {} | ||
variable "instance_role_name" { default = null } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# default: on | ||
# description: xinetdhttpservice | ||
service xinetdhttpservice | ||
{ | ||
# Inspired by: https://github.com/rglaue/xinetd_bash_http_service/blob/master/xinetdhttpservice_config | ||
disable = no | ||
flags = REUSE | ||
socket_type = stream | ||
type = UNLISTED | ||
port = 80 | ||
wait = no | ||
user = nobody | ||
server = /usr/local/bin/httpd | ||
log_on_failure += USERID | ||
only_from = 0.0.0.0/0 | ||
per_source = UNLIMITED | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import json | ||
from os import path as osp | ||
from pprint import pformat | ||
from textwrap import dedent | ||
|
||
import pytest | ||
from infrahouse_toolkit.terraform import terraform_apply | ||
|
||
from tests.conftest import ( | ||
TEST_ZONE, | ||
REGION, | ||
UBUNTU_CODENAME, | ||
TRACE_TERRAFORM, | ||
TEST_ROLE_ARN, | ||
TEST_TIMEOUT, | ||
wait_for_instance_refresh, | ||
LOG, | ||
) | ||
|
||
|
||
@pytest.mark.timeout(TEST_TIMEOUT) | ||
def test_lb( | ||
service_network, | ||
ec2_client, | ||
route53_client, | ||
elbv2_client, | ||
autoscaling_client, | ||
keep_after, | ||
): | ||
subnet_public_ids = service_network["subnet_public_ids"]["value"] | ||
subnet_private_ids = service_network["subnet_private_ids"]["value"] | ||
internet_gateway_id = service_network["internet_gateway_id"]["value"] | ||
|
||
terraform_dir = "test_data/test_spot" | ||
|
||
with open(osp.join(terraform_dir, "terraform.tfvars"), "w") as fp: | ||
fp.write( | ||
dedent( | ||
f""" | ||
region = "{REGION}" | ||
role_arn = "{TEST_ROLE_ARN}" | ||
dns_zone = "{TEST_ZONE}" | ||
ubuntu_codename = "{UBUNTU_CODENAME}" | ||
lb_subnet_ids = {json.dumps(subnet_public_ids)} | ||
backend_subnet_ids = {json.dumps(subnet_private_ids)} | ||
internet_gateway_id = "{internet_gateway_id}" | ||
""" | ||
) | ||
) | ||
|
||
with terraform_apply( | ||
terraform_dir, | ||
destroy_after=not keep_after, | ||
json_output=True, | ||
enable_trace=TRACE_TERRAFORM, | ||
) as tf_output: | ||
asg_name = tf_output["asg_name"]["value"] | ||
wait_for_instance_refresh(asg_name, autoscaling_client) | ||
response = autoscaling_client.describe_auto_scaling_groups( | ||
AutoScalingGroupNames=[ | ||
asg_name, | ||
], | ||
) | ||
LOG.debug( | ||
"describe_auto_scaling_groups(%s): %s", | ||
asg_name, | ||
pformat(response, indent=4), | ||
) | ||
|
||
healthy_instance = None | ||
for instance in response["AutoScalingGroups"][0]["Instances"]: | ||
LOG.debug("Evaluating instance %s", pformat(instance, indent=4)) | ||
if instance["LifecycleState"] == "InService": | ||
healthy_instance = instance | ||
break | ||
assert healthy_instance, f"Could not find a healthy instance in ASG {asg_name}" | ||
healthy_instance_count = len( | ||
[ | ||
i | ||
for i in response["AutoScalingGroups"][0]["Instances"] | ||
if i["LifecycleState"] == "InService" | ||
] | ||
) | ||
assert healthy_instance_count == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters