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

One comment

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.