HOWTO: Create AWS EC2 spot instance with the specific IAM Role

Get IAMInstanceProfile

$iam=[Amazon.AWSClientFactory]::CreateAmazonIdentityManagementClient($awsKeyId, $awsKey)
$req = (new-object Amazon.IdentityManagement.Model.GetInstanceProfileRequest).WithInstanceProfileName('S3Reader')
$res = $iam.GetInstanceProfile($req1).GetInstanceProfileResult
$iamprofile = (new-object Amazon.EC2.Model.IAMInstanceProfile).WithArn($res.InstanceProfile.Arn).WithId($res.InstanceProfile.InstanceProfileId)

Create LaunchSpecification

$spec = (new-object Amazon.EC2.Model.LaunchSpecification).WithInstanceProfile($iamprofile).WithSecurityGroup('your security group')
$spec.ImageId = 'ami0000000'
$spec.InstanceType = 'm1.small'

Create spot request

$ec2=[Amazon.AWSClientFactory]::CreateAmazonEC2Client($awsKeyId, $awsKey)
$req = new-object Amazon.EC2.Model.RequestSpotInstancesRequest
$req.InstanceCount = 1
$req.SpotPrice = '0.02'
$req.LaunchSpecification = $spec
$ec2.RequestSpotInstances($req).RequestSpotInstancesResult.SpotInstanceRequest[0].SpotInstanceRequestId

One thought on “HOWTO: Create AWS EC2 spot instance with the specific IAM Role

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s