Create a Windows EC2 VM on AWS With Terraform

In this Terraform blog post, we will learn how to create a Windows Server 2019 Datacenter edition EC2 virtual machine on AWS using a Terraform configuration code.

I have to say that creating resources on AWS using Terraform makes deploying and cleaning up resources easy.

In the following configuration, I will create a Windows Server 2019 instance. an important part of setting up a Windows machine is that we need to include a public key that we will use to decrypt the administrator user name after setting up the machine.


Below is the Terraform configuration file. For the instance name, you will need to use a unique key. Paste the public key of your SSH key in the public_key value.

Note Make sure you have access to the private SSH key before running the code.

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "3.47.0"

provider "aws" {
  region = "us-west-2"

variable "instance_names" {
    default = "ENTER_AN_INSTANCE_NAE"

resource "aws_instance" "vm" {
  ami           = "ami-0eadec45c219e2657"
  instance_type = "t2.medium"
  key_name =

  tags = {
    Name = var.instance_names



resource "aws_key_pair" "key" {
  key_name   = "myec2key"
  public_key = "YOUR_PUBLIC_KEY"