Sample code for 30+ languages & platforms
PureBasic

EC2 Start Instances

See more Amazon EC2 Examples

Demonstrates how to send a request to start Amazon EC2 instances.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkXml.pb"
IncludeFile "CkAuthAws.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Connect to the Amazon AWS server.
    ; such as https://ec2.amazonaws.com/
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"ec2.us-west-2.amazonaws.com",port,bTls,bAutoReconnect)

    ; Provide AWS credentials for the REST call.
    authAws.i = CkAuthAws::ckCreate()
    If authAws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
    CkAuthAws::setCkSecretKey(authAws, "AWS_SECRET_KEY")
    ; the region should match the region part of the domain passed to the REST connect method (above).
    CkAuthAws::setCkRegion(authAws, "us-west-2")
    CkAuthAws::setCkServiceName(authAws, "ec2")

    CkRest::ckSetAuthAws(rest,authAws)

    CkRest::ckAddQueryParam(rest,"Action","StartInstances")
    CkRest::ckAddQueryParam(rest,"Version","2016-11-15")
    ; If DryRun is true then checks whether you have the required permissions for the action, 
    ; without actually making the request, and provides an error response. If you have the required permissions, 
    ; the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.
    ; Omit the DryRun query parameter to actually start the instance.  (Or set DryRun equal to "false")
    CkRest::ckAddQueryParam(rest,"DryRun","true")
    CkRest::ckAddQueryParam(rest,"InstanceId.1","i-999719995399c9999")

    responseXml.s = CkRest::ckFullRequestNoBody(rest,"GET","/")
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

    ; A successful response will have a status code equal to 200.
    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "response status code = " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "response status text = " + CkRest::ckResponseStatusText(rest)
        Debug "response header: " + CkRest::ckResponseHeader(rest)
        Debug "response body: " + responseXml
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

    ; Examine the successful XML response.
    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::ckLoadXml(xml,responseXml)
    Debug CkXml::ckGetXml(xml)


    CkRest::ckDispose(rest)
    CkAuthAws::ckDispose(authAws)
    CkXml::ckDispose(xml)


    ProcedureReturn
EndProcedure