Published on March 1, 2025
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.
PowerShell is useful for AWS DevOps because it:
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.
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"
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.
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.