🌐 AWS and Terraform in Action: Build Your First IIS Web Server on EC2

WHAT TO KNOW - Sep 7 - - Dev Community

<!DOCTYPE html>





AWS and Terraform in Action: Build Your First IIS Web Server on EC2

<br> body {<br> font-family: Arial, sans-serif;<br> margin: 0;<br> padding: 20px;<br> }</p> <div class="highlight"><pre class="highlight plaintext"><code> h1, h2, h3 { color: #333; } code { font-family: monospace; background-color: #eee; padding: 5px; } img { max-width: 100%; height: auto; } .code-block { background-color: #eee; padding: 10px; border-radius: 5px; margin-bottom: 20px; } .code-block pre { margin: 0; } </code></pre></div> <p>



AWS and Terraform in Action: Build Your First IIS Web Server on EC2



In the ever-evolving landscape of cloud computing, managing infrastructure can be a complex task. Amazon Web Services (AWS) provides a robust platform for hosting applications, while Terraform, an Infrastructure as Code (IaC) tool, empowers developers to automate infrastructure provisioning and management. This article will guide you through the process of building your first IIS web server on EC2 using Terraform and AWS.



Introduction



This tutorial will walk you through the following steps:


  1. Setting up Terraform and AWS credentials
  2. Creating an EC2 instance with Windows Server
  3. Installing and configuring IIS
  4. Deploying a simple website
  5. Securing the server with security groups
  6. Cleaning up resources


By the end of this tutorial, you will have a working IIS web server on EC2, managed entirely through Terraform. This provides several advantages:



  • Automation
    : Automate infrastructure creation, ensuring consistency and reducing manual errors.

  • Reproducibility
    : Terraform allows you to easily recreate your infrastructure, making it ideal for development and testing environments.

  • Version Control
    : Store your infrastructure configuration in version control systems for easy tracking and collaboration.

  • Scalability
    : Easily scale your infrastructure up or down as needed with minimal effort.


Setting up Terraform and AWS Credentials



Prerequisites




Configuring AWS Credentials


  1. Create an IAM user with appropriate permissions for EC2 and S3.
  2. Download the IAM user access keys and secret keys.
  3. Configure Terraform to use your AWS credentials. You can do this by creating a ~/.aws/credentials file with the following structure:
    [default]
    aws_access_key_id = YOUR_ACCESS_KEY_ID
    aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
    
  4. Verify your credentials by running the following command:
    terraform init
    

    Creating an EC2 Instance with Windows Server

    Create a main.tf file in your project directory to define your Terraform resources.

    # Configure AWS Provider
    provider "aws" {
    region = "us-east-1"  # Choose your preferred AWS region
    }
    
    

    Create EC2 Instance

    resource "aws_instance" "webserver" {
    ami = "ami-076316a3387a89265" # Replace with a suitable Windows Server AMI
    instance_type = "t2.micro" # Choose an instance type
    key_name = "your-key-pair-name" # Replace with your existing key pair name
    vpc_security_group_ids = [aws_security_group.webserver_sg.id]

    # Set up user data to install IIS
    user_data = <


    Explanation:


    • provider "aws"
      : Configures the AWS provider, specifying the region.

    • resource "aws_instance" "webserver"
      : Creates an EC2 instance with a specific AMI (Windows Server), instance type, and key pair. The user_data section executes a PowerShell script to install IIS on the instance after launch.

    • resource "aws_security_group" "webserver_sg"
      : Creates a security group that allows inbound traffic on ports 80 (HTTP) and 22 (SSH) from any IP address. This allows you to access the web server and manage it via SSH.


    To create the infrastructure, run the following command:


    terraform apply



    Terraform will prompt you to confirm the changes before applying them. After successful application, you will see the public IP address of your EC2 instance.




    Installing and Configuring IIS



    Once the instance is up and running, you can connect to it using SSH with the key pair you specified. You can then proceed with installing and configuring IIS. Since we have used user data to install IIS during instance creation, it should already be installed.



    To verify, open the Server Manager and check for the Web Server (IIS) role. If it's not installed, you can install it manually.




    Deploying a Simple Website



    Let's deploy a simple "Hello World" website to your IIS server.



    1. Create a simple HTML file

      named index.html with the following content:


    2. <!DOCTYPE html>
          <html>
          <head>
            <title>Hello World</title>
          </head>
          <body>
            <h1>Hello World!</h1>
          </body>
          </html>
          </pre>
      

    3. Use an FTP client or SCP
      to transfer the index.html file to the default website directory on your IIS server. Typically, this is located at C:\inetpub\wwwroot.

    4. Open the IIS Manager
      on your server and navigate to the Default Web Site. Right-click and select Browse. This should open your "Hello World" page in your web browser.


    You have now successfully deployed a website on your IIS web server.

    Web Server with IIS


    Securing the Server with Security Groups


    Security groups are essential for controlling inbound and outbound network traffic to your EC2 instance. We have already configured a basic security group to allow HTTP and SSH traffic. You can customize this further based on your application's security requirements.


    For instance, you might want to restrict access to your web server from specific IP addresses or block certain ports. You can achieve this by modifying the ingress and egress rules in the aws_security_group resource block within your Terraform code.


    Cleaning Up Resources


    When you are finished with your web server, you can clean up the resources you created by running the following command:


    terraform destroy



    This will delete the EC2 instance, security group, and other associated resources. Be sure to confirm the action before proceeding.




    Conclusion



    This article has demonstrated how to build your first IIS web server on EC2 using Terraform. By leveraging Infrastructure as Code, you can automate infrastructure provisioning, ensuring consistency and reducing manual errors. The use of security groups helps you control network access and enhance security. Remember to clean up your resources when you are done to avoid unnecessary costs.



    With this hands-on guide, you have gained the foundational knowledge to build and manage IIS web servers on AWS using Terraform. Explore further customization options, including installing additional software packages, configuring SSL certificates, and integrating with other AWS services to enhance your web server deployment.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player