Skip to content

Instantly share code, notes, and snippets.

@imeredith
Created June 16, 2012 13:05
Show Gist options
  • Save imeredith/2941290 to your computer and use it in GitHub Desktop.
Save imeredith/2941290 to your computer and use it in GitHub Desktop.
def createContext = {
ContextBuilder.newBuilder(new AWSEC2ProviderMetadata)
.credentials("<>", "<>")
.buildView(classOf[AWSEC2ComputeServiceContext])
.unwrap(classOf[RestContext[AWSEC2Client, AWSEC2AsyncClient]])
}
implicit def syncApi = createContext.getApi
def createInstance(imageId: String, hwType: String, group: String, key: String)(implicit c: AWSEC2Client, region: String = null) = {
val opts = AWSRunInstancesOptions.Builder.asType(hwType).withSecurityGroup(group).withKeyName(key)
val instances = c.getInstanceServices.runInstancesInRegion(region,null,imageId,1,1,opts)
instances.headOption
}
def destroyInstance(id: String)(implicit c: AWSEC2Client, region: String = null) = {
val instances = c.getInstanceServices.terminateInstancesInRegion(region, id)
instances.headOption
}
def runningInstance(id: String)(implicit c: AWSEC2Client, region: String = null): Option[RunningInstance] = {
val instance = c.getInstanceServices.describeInstancesInRegion(region, id).headOption
for {
i <- instance
runningInstance <- i.headOption
} yield runningInstance
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment