Skip to content

Instantly share code, notes, and snippets.

@mtcoffee
Created December 10, 2023 17:08
Show Gist options
  • Save mtcoffee/325ba4fd29b4528e9e15ab61293d1118 to your computer and use it in GitHub Desktop.
Save mtcoffee/325ba4fd29b4528e9e15ab61293d1118 to your computer and use it in GitHub Desktop.
terraform_aws_windows_ec2_t2micro
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "us-west-2"
}
resource "aws_instance" "ec2_instance" {
ami = "${var.ami_id}"
instance_type = "${var.instance_type}"
key_name = "${var.ami_key_pair_name}"
tags = {
Name = "Sample Terraform Build"
}
get_password_data = true
}
# create security group for our ec2 instance
resource "aws_security_group" "allow_all_3389" {
name = "allow_all_3389"
description = "Allow RDP inbound traffic"
ingress {
from_port = 3389
to_port = 3389
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Allow RDP from any IP address
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] # Allow all outbound traffic
}
}
# Attach security group to our ec2 instance
resource "aws_network_interface_sg_attachment" "sg_attachment" {
security_group_id = "${aws_security_group.allow_all_3389.id}"
network_interface_id = "${aws_instance.ec2_instance.primary_network_interface_id}"
}
#get randomly generated Administrator password for Windows using our private key file
resource "null_resource" "ec2_instance" {
count = 1
triggers = {
password = "${rsadecrypt(aws_instance.ec2_instance.*.password_data[count.index], file("${var.pem_file}"))}"
}
}
output "public_ip" {
value = "${aws_instance.ec2_instance.*.public_ip}"
}
output "Administrator_Password" {
value = "${null_resource.ec2_instance.*.triggers.password}"
}
output "Summary" {
value = "This VM should now be ready. You can check its status in the EC2 Console"
description = "A summary to display after the apply"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment