Skip to content

Instantly share code, notes, and snippets.

@JamesCalleja
Last active September 9, 2016 15:17
Show Gist options
  • Select an option

  • Save JamesCalleja/ba3c71a9ec4ffef6cb735d577a742ac9 to your computer and use it in GitHub Desktop.

Select an option

Save JamesCalleja/ba3c71a9ec4ffef6cb735d577a742ac9 to your computer and use it in GitHub Desktop.
AWS build new EC2 instance, add disks,
#search for all images owened by <user> select for *web*
$aim_web = Get-EC2Image -Owner 399828342872 | ?{$_.name -like '*web*'}
#create an EBSBlockDevice profile
$volume1 = New-Object Amazon.EC2.Model.EbsBlockDevice
$volume1.DeleteOnTermination = $true
$volume1.Encrypted = $true
$volume1.VolumeSize = 30
$volume1.VolumeType = 'standard'
#describe a mapping profile using disk above
$mapping1 = New-Object Amazon.EC2.Model.BlockDeviceMapping
$mapping1.DeviceName = 'xvdf'
$mapping1.Ebs = $volume1
$mapping2 = New-Object Amazon.EC2.Model.BlockDeviceMapping
$mapping2.DeviceName = 'xvdg'
$mapping2.Ebs = $volume1
$NewInstanceResponse = New-EC2Instance -ImageId ($aim_web).ImageId -BlockDeviceMapping $mapping1, $mapping2 -AssociatePublicIp $false -SecurityGroupID (Get-EC2SecurityGroup |?{$_.groupname -like '*web*'}).groupid -InstanceType t2.micro -AvailabilityZone eu-west-1a -Monitoring_Enabled $false -SubnetId subnet-4e11722a -InstanceInitiatedShutdownBehavior stop -InstanceProfile_Name SSM-RunCommand-Allow
$InstanceID = ($NewInstanceResponse.Instances).InstanceId
#build an array for tags to apply to the new instance
$tags = @()
$CreatedByTag = New-Object Amazon.EC2.Model.Tag
$CreatedByTag.Key = "CreatedBy"
$CreatedByTag.Value = "James"
$environmentTag = New-Object Amazon.EC2.Model.Tag
$environmentTag.Key = "Env"
$environmentTag.Value = "test"
$NameTag = New-Object Amazon.EC2.Model.Tag
$NameTag.key = 'Name'
$NameTag.value = 'WEB2_test_JC'
$tags += $NameTag
$tags += $CreatedByTag
$tags += $environmentTag
new-ec2tag -ResourceId $NewInstanceResponse.Instances[0].InstanceId -Tag $tags
#terminate the instance you just created
$Instance = get-EC2Instance -InstanceId $InstanceId
$filter = New-Object Amazon.EC2.Model.Filter
$filter.name = 'key:Name'
$filter.value = 'WEB2_'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment