DevOps Automation: Create Windows amazon EC2 Instance & Login to VMs Using WinRM Port
By: Tabexp Apps
Tags: Aws Ec2, Windows, AWS, Virtual Machine, Remote Machine

Developers, Sysadmins, and IT administrators have been carrying out system administration tasks or DevOps tasks ‘manually’ from time immemorial. Be it desktop machines configuration, server configuration, operating system installation, software installations, or setting up VMs; everything was manual, time-consuming, and error-prone until DevOps conquered the software development and delivery space.

With profound knowledge and deep expertise in Azure DevOps automation services, our automation experts share scripts to help you automate creation of Windows Amazon EC2 instances using DevOps and automate logins to VMs using WinRM Port.

Other similar post: How to Automate Windows remote machines logins using DevOps

When you sign up with AWS (Amazon Web Services) you sign up for an entire universe of cloud computing services. One of such services is Amazon EC2 instance — a virtual server where subscribers provision compute servers in the AWS cloud. We understand creating Windows Amazon EC2 instances is a time and cost consuming process, especially in a 3-tier architecture where software developers require multiple VMs and several thousand dollar budget. The below automation for creating EC2 instances can save your business a fortune.

Let’s begin:

Create AWS EC2 instance

Install-Module -Name AWS.Tools.Installer -Force

Install-AWSToolsModule AWS.Tools.EC2,AWS.Tools.S3 -CleanUp -Force

Install-Module -Name AWSPowerShell -Force

Install-Module -name AWSPowerShell.NetCore -Force

Import-Module AWSPowerShell.NetCore

Setup credentials and login to AWS Console

Set-AWSCredential -AccessKey <YOUR-ACCESS-KEY> -SecretKey <YOUR-ACCESS-SECRET-KEY> -StoreAs awsprofile

Set-AWSCredential -ProfileName awsprofile

Initialize-AWSDefaultConfiguration -ProfileName awsprofile -Region us-east-1

$keypair = New-EC2KeyPair -KeyName KeyPair

$keypair.KeyMaterial | Out-File -Encoding ascii C:\AWS\ec2-demo-key.pem

Create Security Groups — For opening WinRM Port (inbound / outbound)

New-EC2SecurityGroup -GroupName EC2DemoSecurityGroup -GroupDescription “Security group for EC2 demo”

Get-EC2SecurityGroup -GroupNames EC2DemoSecurityGroup

Grant-EC2SecurityGroupIngress -GroupName EC2DemoSecurityGroup -IpPermissions @{IpProtocol = “tcp”; FromPort = 80; ToPort = 80; IpRanges = @(“0.0.0.0/0”)}

Grant-EC2SecurityGroupIngress -GroupName EC2DemoSecurityGroup -IpPermissions @{IpProtocol = “tcp”; FromPort = 3389; ToPort = 3389; IpRanges = @(“0.0.0.0/0”)}

Grant-EC2SecurityGroupIngress -GroupName EC2DemoSecurityGroup -IpPermissions @{IpProtocol = “tcp”; FromPort = 5985; ToPort = 5985; IpRanges = @(“0.0.0.0/0”)}

Grant-EC2SecurityGroupIngress -GroupName EC2DemoSecurityGroup -IpPermissions @{IpProtocol = “tcp”; FromPort = 5986; ToPort = 5986; IpRanges = @(“0.0.0.0/0”)}Get-EC2SecurityGroup -GroupNames EC2DemoSecurityGroup | Select -ExpandProperty:IpPermission

Fetch the available AMI ID for the provided region

Get-EC2Image -Owners amazon -Filters @{Name = “name”; Values = “Windows_Server-2016*English*”} | select imageid,name | ft –auto

Create new instances

New-EC2Instance -ImageId ami-000bf92d1a21bf9ac -MinCount 1 -MaxCount 1 -KeyName KeyPair -SecurityGroups EC2DemoSecurityGroup -InstanceType t2.micro$instance = Get-EC2Instance -Filter @{Name = “reservation-id”; Values = “r-092d7d1c85675c615”}

$instance.RunningInstance.State

$instance.RunningInstance.publicdnsname

Fetch default passwords assigned to VMs

$password = Get-EC2PasswordData -InstanceId $instance.RunningInstance.instanceid -PemFile C:\aws\ec2-demo-key.pem -Decrypt

$password

And you’re done!

With years of polished, niche expertise in Azure DevOps Automation services, our team of automation experts has rendered support to several SMBs in automating workloads, saving significant costs on cloud operations.

Got a workload that’s costing you additional business hours and budget? Get in touch with our DevOps automation experts for automating cloud operations.