Skip to content

Commit dd34295

Browse files
authored
Merge pull request #23 from wyTrivail/terraform
support windows
2 parents 7f405d6 + a971d3d commit dd34295

File tree

3 files changed

+115
-48
lines changed

3 files changed

+115
-48
lines changed

terraform/ec2/amis.tf

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,74 @@
1-
variable "amis" {
1+
variable "ami_family" {
22
default = {
33
ubuntu = {
4-
ami_search_pattern = "ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"
54
login_user = "ubuntu"
6-
install_package = "rpm"
5+
install_package = "aws-observability-collector.deb"
6+
instance_type = "t2.micro"
7+
otconfig_destination = "/tmp/ot-default.yml"
8+
download_command_pattern = "wget %s"
9+
install_command = "sudo dpkg -i aws-observability-collector.deb"
10+
start_command = "sudo /opt/aws/aws-observability-collector/bin/aws-observability-collector-ctl -c /tmp/ot-default.yml -a start"
11+
connection_type = "ssh"
12+
user_data = ""
13+
},
14+
amazon_linux = {
15+
login_user = "ec2-user"
16+
install_package = "aws-observability-collector.rpm"
17+
instance_type = "t2.micro"
18+
otconfig_destination = "/tmp/ot-default.yml"
19+
download_command_pattern = "wget %s"
20+
install_command = "sudo rpm -Uvh aws-observability-collector.rpm"
21+
start_command = "sudo /opt/aws/aws-observability-collector/bin/aws-observability-collector-ctl -c /tmp/ot-default.yml -a start"
22+
connection_type = "ssh"
23+
user_data = ""
24+
}
25+
windows = {
26+
login_user = "Administrator"
27+
install_package = "aws-observability-collector.msi"
28+
instance_type = "t2.micro"
29+
otconfig_destination = "C:\\ot-default.yml"
30+
download_command_pattern = "powershell -command \"Invoke-WebRequest -Uri %s -OutFile C:\\aws-observability-collector.msi\""
31+
install_command = "msiexec /i C:\\aws-observability-collector.msi"
32+
start_command = "powershell \"& 'C:\\Program Files\\Amazon\\AwsObservabilityCollector\\aws-observability-collector-ctl.ps1' -ConfigLocation C:\\ot-default.yml -Action start\""
33+
connection_type = "winrm"
34+
user_data = <<EOF
35+
<powershell>
36+
winrm quickconfig -q
37+
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="300"}'
38+
winrm set winrm/config '@{MaxTimeoutms="1800000"}'
39+
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
40+
winrm set winrm/config/service/auth '@{Basic="true"}'
41+
netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow
42+
netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow
43+
net stop winrm
44+
sc.exe config winrm start=auto
45+
net start winrm
46+
Set-NetFirewallProfile -Profile Public -Enabled False
47+
</powershell>
48+
EOF
49+
}
50+
}
51+
}
52+
53+
variable "amis" {
54+
default = {
55+
ubuntu16 = {
56+
ami_search_pattern = "ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"
57+
ami_owner = "099720109477"
58+
family = "ubuntu"
59+
arch = "amd64"
760
}
861
amazonlinux2 = {
962
ami_search_pattern = "amzn2-ami-hvm*"
10-
login_user = "ec2-user"
11-
install_package = "rpm"
12-
}
13-
suse = {
14-
ami_search_pattern = "suse-sles-15*"
15-
login_user = "ec2-user"
16-
install_package = "rpm"
63+
ami_owner = "amazon"
64+
family = "amazon_linux"
65+
arch = "amd64"
1766
}
1867
windows2019 = {
19-
ami_search_pattern = "Windows_Server-2019-English-Full-Base*"
20-
login_user = "Administrator"
21-
install_package = "msi"
68+
ami_search_pattern = "Windows_Server-2019-English-Full-Base-*"
69+
ami_owner = "amazon"
70+
family = "windows"
71+
arch = "amd64"
2272
}
2373
}
2474
}
@@ -31,14 +81,11 @@ data "aws_ami" "selected" {
3181
values = [var.amis[var.testing_ami]["ami_search_pattern"]]
3282
}
3383

34-
filter {
35-
name = "owner-alias"
36-
values = ["amazon"]
37-
}
38-
39-
owners = ["amazon"] # Canonical
84+
owners = [var.amis[var.testing_ami]["ami_owner"]] # Canonical
4085
}
4186

87+
88+
# this ami is used to launch the emitter instance
4289
data "aws_ami" "suse" {
4390
most_recent = true
4491

terraform/ec2/main.tf

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ provider "aws" {
1515
region = var.region
1616
}
1717

18+
# get ami object
19+
locals {
20+
selected_ami = var.amis[var.testing_ami]
21+
ami_family = var.ami_family[local.selected_ami["family"]]
22+
ami_id = data.aws_ami.selected.id
23+
instance_type = local.ami_family["instance_type"]
24+
otconfig_destination = local.ami_family["otconfig_destination"]
25+
login_user = local.ami_family["login_user"]
26+
connection_type = local.ami_family["connection_type"]
27+
user_data = local.ami_family["user_data"]
28+
download_command = format(local.ami_family["download_command_pattern"], "https://${var.package_s3_bucket}.s3.amazonaws.com/${local.selected_ami["family"]}/${local.selected_ami["arch"]}/${var.aoc_version}/${local.ami_family["install_package"]}")
29+
}
30+
1831
## get the ssh private key
1932
data "aws_s3_bucket_object" "ssh_private_key" {
2033
bucket = var.sshkey_s3_bucket
@@ -33,39 +46,43 @@ data "template_file" "otconfig" {
3346
}
3447
}
3548

36-
# launch ec2 instance to install aoc [todo, support more amis, only amazonlinux2 is supported now]
49+
# launch ec2 instance to install aoc [todo, support more amis, only amazonlinux2 ubuntu, windows2019 is supported now]
3750
resource "aws_instance" "aoc" {
38-
ami = data.aws_ami.selected.id
39-
instance_type = "t2.micro"
51+
ami = local.ami_id
52+
instance_type = local.instance_type
4053
subnet_id = tolist(module.basic_components.aoc_public_subnet_ids)[0]
4154
vpc_security_group_ids = [module.basic_components.aoc_security_group_id]
4255
associate_public_ip_address = true
4356
iam_instance_profile = module.common.aoc_iam_role_name
4457
key_name = module.common.ssh_key_name
58+
get_password_data = local.connection_type == "winrm" ? true : null
59+
user_data = local.user_data
4560

4661
provisioner "file" {
4762
content = data.template_file.otconfig.rendered
48-
destination = "/tmp/ot-default.yml"
63+
destination = local.otconfig_destination
4964

5065
connection {
51-
type = "ssh"
52-
user = "ec2-user"
53-
private_key = data.aws_s3_bucket_object.ssh_private_key.body
66+
type = local.connection_type
67+
user = local.login_user
68+
private_key = local.connection_type == "ssh" ? data.aws_s3_bucket_object.ssh_private_key.body : null
69+
password = local.connection_type == "winrm" ? rsadecrypt(aws_instance.aoc.password_data, data.aws_s3_bucket_object.ssh_private_key.body) : null
5470
host = aws_instance.aoc.public_ip
5571
}
5672
}
5773

5874
provisioner "remote-exec" {
5975
inline = [
60-
"wget https://${var.package_s3_bucket}.s3.amazonaws.com/amazon_linux/amd64/${var.aoc_version}/aws-observability-collector.rpm",
61-
"sudo rpm -Uvh aws-observability-collector.rpm",
62-
"sudo /opt/aws/aws-observability-collector/bin/aws-observability-collector-ctl -c /tmp/ot-default.yml -a start"
76+
local.download_command,
77+
local.ami_family["install_command"],
78+
local.ami_family["start_command"]
6379
]
6480

6581
connection {
66-
type = "ssh"
67-
user = "ec2-user"
68-
private_key = data.aws_s3_bucket_object.ssh_private_key.body
82+
type = local.connection_type
83+
user = local.login_user
84+
private_key = local.connection_type == "ssh" ? data.aws_s3_bucket_object.ssh_private_key.body : null
85+
password = local.connection_type == "winrm" ? rsadecrypt(aws_instance.aoc.password_data, data.aws_s3_bucket_object.ssh_private_key.body) : null
6986
host = aws_instance.aoc.public_ip
7087
}
7188
}
@@ -74,8 +91,6 @@ resource "aws_instance" "aoc" {
7491

7592
## launch a ec2 instance to install data emitter
7693
resource "aws_instance" "emitter" {
77-
# don't do emitter instance if the sample app is not callable
78-
count = var.sample_app_callable ? 1 : 0
7994
ami = data.aws_ami.suse.id
8095
instance_type = "t2.micro"
8196
subnet_id = tolist(module.basic_components.aoc_public_subnet_ids)[0]
@@ -98,16 +113,14 @@ data "template_file" "docker_compose" {
98113
}
99114
}
100115
resource "null_resource" "sample-app-validator" {
101-
count = var.sample_app_callable ? 1 : 0
102-
103116
provisioner "file" {
104117
content = data.template_file.docker_compose.rendered
105118
destination = "/tmp/docker-compose.yml"
106119
connection {
107120
type = "ssh"
108121
user = "ec2-user"
109122
private_key = data.aws_s3_bucket_object.ssh_private_key.body
110-
host = aws_instance.emitter[0].public_ip
123+
host = aws_instance.emitter.public_ip
111124
}
112125
}
113126
provisioner "remote-exec" {
@@ -122,21 +135,12 @@ resource "null_resource" "sample-app-validator" {
122135
type = "ssh"
123136
user = "ec2-user"
124137
private_key = data.aws_s3_bucket_object.ssh_private_key.body
125-
host = aws_instance.emitter[0].public_ip
138+
host = aws_instance.emitter.public_ip
126139
}
127140
}
128141

129142
provisioner "local-exec" {
130-
command = "${module.common.validator_path} --args='-c ${var.validation_config} -t ${module.common.testing_id} --region ${var.region} --metric-namespace ${module.common.otel_service_namespace}/${module.common.otel_service_name} --endpoint http://${aws_instance.emitter[0].public_ip}'"
131-
working_dir = "../../"
132-
}
133-
}
134-
135-
# only run it when aoc collects metrics without any sample apps
136-
resource "null_resource" "validator" {
137-
count = !var.sample_app_callable ? 1 : 0
138-
provisioner "local-exec" {
139-
command = "${module.common.validator_path} --args='-c ${var.validation_config} -t ${module.common.testing_id} --region ${var.region} --metric-namespace ${module.common.otel_service_namespace}/${module.common.otel_service_name}'"
143+
command = "${module.common.validator_path} --args='-c ${var.validation_config} -t ${module.common.testing_id} --region ${var.region} --metric-namespace ${module.common.otel_service_namespace}/${module.common.otel_service_name} --endpoint http://${aws_instance.emitter.public_ip}'"
140144
working_dir = "../../"
141145
}
142146
}

terraform/setup/setup.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,27 @@ resource "aws_security_group" "aoc_sg" {
132132
cidr_blocks = ["0.0.0.0/0"]
133133
}
134134

135+
ingress {
136+
from_port = 5985
137+
to_port = 5985
138+
protocol = "tcp"
139+
cidr_blocks = ["0.0.0.0/0"]
140+
}
141+
142+
ingress {
143+
from_port = 3389
144+
to_port = 3389
145+
protocol = "tcp"
146+
cidr_blocks = ["0.0.0.0/0"]
147+
}
148+
135149
egress {
136150
from_port = 0
137151
to_port = 0
138152
protocol = "-1"
139153
cidr_blocks = ["0.0.0.0/0"]
140154
}
155+
156+
141157
}
142158

0 commit comments

Comments
 (0)