Automating AWS Deployments with PowerShell and cfn-init

Introduction

Automation is a key part of modern DevOps, and Windows PowerShell is a powerful tool for managing AWS resources, deploying applications, and configuring servers. By integrating PowerShell with AWS CloudFormation and cfn-init, we can automate infrastructure setup and software installations efficiently.

Why Use PowerShell for AWS Automation?

PowerShell is useful for AWS DevOps because it:

  • Automates infrastructure provisioning using CloudFormation
  • Deploys vendor applications on EC2 instances
  • Handles software installation and server configuration with cfn-init
  • Integrates easily with AWS services like S3, IAM, and Lambda

Automating Software Installation with cfn-init

When launching an EC2 instance, manually installing software can be time-consuming. Instead, we can automate it using CloudFormation and cfn-init. Here’s an example of how I install MongoDB on a Windows EC2 instance automatically.

CloudFormation Template to Install MongoDB

Resources:
MyInstance:
  Type: AWS::EC2::Instance
  Properties:
    ImageId: ami-0abcdef1234567890
    InstanceType: t2.micro
    UserData:
      Fn::Base64: |
        <script>
        cfn-init.exe -v --stack MyStack --resource MyInstance --region us-east-1
        </script>
    Metadata:
      AWS::CloudFormation::Init:
        config:
            files:
                "C:\install_mongo.ps1":
                    content: |
                        Write-Output "Downloading MongoDB..."
                        Invoke-WebRequest -Uri "https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-6.0.2-signed.msi" -OutFile "C:mongodb.msi"
                        Write-Output "Installing MongoDB..."
                        Start-Process msiexec.exe -ArgumentList '/i C:mongodb.msi /quiet /norestart' -Wait
                        Write-Output "MongoDB Installation Complete!"
                    mode: "000644"
            commands:
                01_install_mongo:
                    command: "powershell.exe -ExecutionPolicy Unrestricted -File C:\install_mongo.ps1"

Deploying CloudFormation Stacks with PowerShell

Once the CloudFormation template is ready, I use PowerShell to deploy it:

$stackName = "MyStack"
$templateFile = "C:\path\to\template.yaml"

New-CFNStack -StackName $stackName -TemplateBody (Get-Content $templateFile -Raw)
Write-Output "CloudFormation stack $stackName is being created."

This eliminates manual setup and ensures consistent deployments across environments.

Key Benefits of This Approach

  • Saves time by automating infrastructure and software installations
  • Ensures consistency across deployments
  • Scales easily, making it useful for large environments
  • Integrates seamlessly with AWS and DevOps pipelines

Final Thoughts

Using PowerShell with AWS CloudFormation and cfn-init is a game-changer for automating deployments. Whether setting up servers, installing software, or managing cloud resources, PowerShell helps make DevOps processes faster and more reliable.