Title: Deploying a Sample Web Application on Windows Server (IIS) using AWS Terraform

Solai Rajan
5 min readJun 16, 2023

Introduction:

In this blog post, we will guide you through the process of deploying a sample HTML, CSS, and JavaScript sample web application on a Windows Server running on AWS using Terraform. We will start by creating an IAM user with appropriate permissions, obtaining the access key and secret access key, and configuring the AWS CLI on your local PC. Then, we will create a new directory for our project and set up Terraform files, including the EC2 module. Please note that in this example, we are using the default VPC and subnets. In the next blog post, we will cover how to create a separate VPC. Finally, we will use user data in the EC2 instance to install the IIS server, download and install Git, clone the sample application, and copy it to the Windows Server’s root directory.

Prerequisites:

1. An AWS account

2. AWS CLI installed on your local PC

3. Terraform installed on your local PC

4. VS Code installed on your local PC

Step 1: Creating IAM User and Access Key

  1. Log in to the AWS Management Console.
AWS Console

2. Navigate to the IAM service.

Create Iam user in aws

3. Create a new IAM user with programmatic access.

4. Attach the necessary IAM policies, such as AmazonEC2FullAccess and AmazonVPCFullAccess, to the user.

5. After the user is created, Create the Access Key ID and Secret Access Key.

Create Access key and secret access for Iam user in aws

Step 2: Configuring AWS CLI

1. Open a terminal or command prompt on your local PC.

2. Run the command `aws configure` and provide the Access Key ID, Secret Access Key, default region, and output format.

3. Verify the configuration by running a simple AWS CLI command, such as `aws ec2 describe-instances`.

Step 3: Setting up Terraform Files

1. Create a new directory for your project.

2. Inside the project directory, create the following Terraform files:

  • `main.tf`: Main configuration file for Terraform.
module "ec2" {
source = "./ec2"

}
  • `provider.tf`: Provider configuration file for AWS.
provider "aws" {
region = var.region
}
  • `variables.tf`: Variable declarations file.
variable "Name" {
default = "windows-instance"
}
variable "region" {
default = "us-east-1"
}
  • `output.tf`: Output declarations file.
output "instancedetails_output"{
value =module.ec2.instancedetails
}

Step 4: Creating EC2 Module

1. Within your project directory, create an `ec2` directory.

2. Inside the `ec2` directory, create the following Terraform files:

  • `main.tf`: Define the EC2 instance and related resources.
# EC2 Instance
resource "aws_instance" "example" {
ami = "ami-0bde1eb2c18cb2abe" # Replace with your desired AMI ID
instance_type = "t2.micro"
key_name = "windows" # Replace with your key pair name
user_data = <<-EOF
<powershell>
Start-Sleep -Seconds 120

#Download Git installer
Set-ExecutionPolicy RemoteSigned -Force
$installerPath = "C:\Git-2.32.0-64-bit.exe"
$installerArgs = "/SILENT"

Invoke-WebRequest -Uri "https://github.com/git-for-windows/git/releases/download/v2.32.0.windows.1/Git-2.32.0-64-bit.exe" -OutFile $installerPath
Start-Process -Wait -FilePath $installerPath -ArgumentList $installerArgs -Verb RunAs


# Add Git to PATH environment variable
$GitPath = "C:\Program Files\Git\bin"
$EnvPath = [Environment]::GetEnvironmentVariable("PATH", "Machine")
[Environment]::SetEnvironmentVariable("PATH", "$EnvPath;$GitPath", "Machine")
# Install IIS
Install-WindowsFeature -Name Web-Server -IncludeManagementTools
# Create a new website
$docroot = "C:\\inetpub\\wwwroot"
New-Item -ItemType Directory -Force -Path $docroot
Set-WebConfigurationProperty -Filter /system.webServer/directoryBrowse -Name enabled -Value True -PSPath IIS:\ -Verbose
New-Website -Name "My Website" -PhysicalPath $docroot -Port 80 -Force -Verbose
# Set Git configuration
git config --global user.name "Your Name"
git config --global user.email "Your Email"
Start-Sleep -Seconds 120
# Clone the GitHub repository
$repositoryUrl = "your github repo url"
$clonePath = "C:\github"
& "C:\Program Files\Git\bin\git.exe" clone $repositoryUrl $clonePath
# Copy website files to web root directory
Copy-Item -Path C:\\github\\* -Destination $docroot -Recurse -Force -Verbose
</powershell>
EOF


tags = {
Name = var.Name
}
}
  • `variables.tf`: Declare variables specific to the EC2 module.
variable "Name" {
default = "windows-instance"
}
  • `output.tf`: Declare outputs specific to the EC2 module.
output "instancedetails"{
value = {
instance_state=aws_instance.example.instance_state

instance_id = aws_instance.example.id
public_ip= aws_instance.example.public_ip
}
}

Step 5: Utilizing User Data in EC2 Instance

1. Open the `ec2/main.tf` file.

2. Define the EC2 instance resource and include the `user_data` attribute.

3. In the `user_data` attribute, include a PowerShell script to install the IIS server and Git, clone the sample application repository, and copy it to the Windows Server’s root directory.

Step 6: Deploying the Web Application

  1. Run `terraform init` to initialize the Terraform project.

2. Run `terraform plan` to review the execution plan.

3. If the plan looks correct, run `terraform apply` to deploy the infrastructure.

4. Terraform will provision the EC2 instance, execute the user data script, and set up the IIS server and web application.

5. Once the deployment is complete, Terraform will provide you with the public IP.

6. Access your web application by pasting the public IP address in your browser’s address bar. Note that it may take time to propagate, so please be patient.

Conclusion:

In this blog post, we covered the step-by-step process of deploying a sample web application on a Windows Server running on AWS using Terraform. We started by creating an IAM user, configuring the AWS CLI, and setting up the Terraform project. With the help of user data in the EC2 instance, we installed the IIS server, installed Git, cloned the application repository, and copied it to the server’s root directory. By following these steps, you can easily deploy your own web applications on Windows Server instances in AWS using Terraform. Happy coding and deploying!

Powered by solai.website

Follow me on

linkedin | linktr.ee

--

--

Solai Rajan

Cloud Engineer with a strong focus on Amazon Web Services (AWS), proficient in designing, implementing, and managing cloud solutions on the AWS platform.