How to Start AWS EC2 instance using PowerShell

This shows how to start an EC2 instance from an on-premise PC using AWS PowerShell.

Install AWSPowerShell.NetCore Module

The AWS Tools for PowerShell can be installed by using the following command. The tool enables managing AWS services from the PowerShell scripting environment. If the NuGet dialog is shown, enter Yes, and follow on-screen instructions.

Install-Module -Name AWSPowerShell.NetCore

Create an AWS Profile on your PC

If AWS profile was not created before, create it on PC.

Once a profile has been stored, scripts can use the stored profile name without typing or showing SecretKey and AccessKey. This example creates AWSRunner profile for the given access key and secret key.

import-module AWSPowerShell.NetCore

$accessKey ="XXX"
$secretKey ="YYY"

$profile ="AWSRunner"

Set-AWSCredential -AccessKey $accessKey -SecretKey $secretKey -StoreAs $profile

Check if an AWS Profile created

If AWS profile was not created before, create it on PC.

Once profile has been stored, scripts can use the stored profile name without typing or showing SecretKey and AccessKey. This example creates AWSRunner profile for the given access key and secret key.

Get-AWSCredential -ListProfileDetail

Run:

Start an EC2 instance with InstanceId using ProfileName

Start an Instance with Instance Id and profile name. To run, AWS asks Region to narrow the scope of the search, so the default AWS Region was also set in this example.

import-module AWSPowerShell.NetCore

$InstanceId = "XXXXXXXXXXXXXXXXXXXXX"

$AWSregion = "us-east-1"

Set-DefaultAWSRegion $AWSregion

$profile = "AWSRunner"

Start-EC2Instance -InstanceId $InstanceId -ProfileName $profile

Check if the EC2 instance running

EC2 Instance Status can be checked by the PS command line as below.

$InstanceId = "XXXXXXXXXXXXXXXXXXXXX"
(Get-EC2InstanceStatus -InstanceID $InstanceId -ProfileName "AWSRunner").Status

Run:

More articles to read:

How to Create Serverless Service with AWS Lambda and CloudWatch

How to Host a Static Website on AWS S3

How to Create AWS EC2 Instance: a simple guide

Security Group Rules for Web Server EC2 Instance

Leave a Comment