Launch an Amazon EC2 Instance Using Windows PowerShell - AWS Tools for PowerShell

Launch an Amazon EC2 Instance Using Windows PowerShell

To launch an Amazon EC2 instance, you need the key pair and security group that you created in the previous sections. You also need the ID of an Amazon Machine Image (AMI). For more information, see the following documentation:

Important

If you launch an instance that is not within the Free Tier, you are billed after you launch the instance and charged for the time that the instance is running even if it remains idle.

Launching an Instance in a VPC

Warning

EC2-Classic was retired on August 15, 2022. We recommend that you migrate from EC2-Classic to a VPC. For more information, see the blog post EC2-Classic Networking is Retiring – Here's How to Prepare.

The following command creates a single m1.small instance in the specified private subnet. The security group must be valid for the specified subnet.

PS > New-EC2Instance ` -ImageId ami-c49c0dac ` -MinCount 1 -MaxCount 1 ` -KeyName myPSKeyPair ` -SecurityGroupId sg-5d293231 ` -InstanceType m1.small ` -SubnetId subnet-d60013bf ReservationId : r-b70a0ef1 OwnerId : 123456789012 RequesterId : Groups : {} GroupName : {} Instances : {}

Your instance is in the pending state initially, but is in the running state after a few minutes. To view information about your instance, use the Get-EC2Instance cmdlet. If you have more than one instance, you can filter the results on the reservation ID using the Filter parameter. First, create an object of type Amazon.EC2.Model.Filter. Next, call Get-EC2Instance that uses the filter, and then displays the Instances property.

PS > $reservation = New-Object 'collections.generic.list[string]' PS > $reservation.add("r-b70a0ef1") PS > $filter_reservation = New-Object Amazon.EC2.Model.Filter -Property @{Name = "reservation-id"; Values = $reservation} PS > (Get-EC2Instance -Filter $filter_reservation).Instances AmiLaunchIndex : 0 Architecture : x86_64 BlockDeviceMappings : {/dev/sda1} ClientToken : EbsOptimized : False Hypervisor : xen IamInstanceProfile : ImageId : ami-c49c0dac InstanceId : i-5203422c InstanceLifecycle : InstanceType : m1.small KernelId : KeyName : myPSKeyPair LaunchTime : 12/2/2018 3:38:52 PM Monitoring : Amazon.EC2.Model.Monitoring NetworkInterfaces : {} Placement : Amazon.EC2.Model.Placement Platform : Windows PrivateDnsName : PrivateIpAddress : 10.25.1.11 ProductCodes : {} PublicDnsName : PublicIpAddress : 198.51.100.245 RamdiskId : RootDeviceName : /dev/sda1 RootDeviceType : ebs SecurityGroups : {myPSSecurityGroup} SourceDestCheck : True SpotInstanceRequestId : SriovNetSupport : State : Amazon.EC2.Model.InstanceState StateReason : StateTransitionReason : SubnetId : subnet-d60013bf Tags : {} VirtualizationType : hvm VpcId : vpc-a01106c2

Launching a Spot Instance in a VPC

The following example script requests a Spot Instance in the specified subnet. The security group must be one you created for the VPC that contains the specified subnet.

$interface1 = New-Object Amazon.EC2.Model.InstanceNetworkInterfaceSpecification $interface1.DeviceIndex = 0 $interface1.SubnetId = "subnet-b61f49f0" $interface1.PrivateIpAddress = "10.0.1.5" $interface1.Groups.Add("sg-5d293231") Request-EC2SpotInstance ` -SpotPrice 0.007 ` -InstanceCount 1 ` -Type one-time ` -LaunchSpecification_ImageId ami-7527031c ` -LaunchSpecification_InstanceType m1.small ` -Region us-west-2 ` -LaunchSpecification_NetworkInterfaces $interface1