Amazon EC2: Launch an Instance with a Persisted Idempotency Token
See more Amazon EC2 Examples
Live mode creates a billable instance. Before the first run, select an AMI compatible with the instance type, a subnet, a security group in that subnet's VPC, and an existing key pair, all in the chosen region. The sample requires IMDSv2. Public addressing follows the subnet configuration; this is not a promise of Internet reachability.
The private ec2-launch-request.json stores the region, exact illustrated parameters, and client token before submission, and the resulting instance ID after success. Reruns load the saved parameters; changing constants in the source does not change an existing request. Serialize access to this file and retain it after timeouts. Deliberate retries must use the same token and unchanged parameters in the same region/subnet; a different Availability Zone can create another instance. Reconcile uncertain outcomes by client token before creating a new intent. A recorded instance ID prevents this sample from resubmitting. Launch is asynchronous; use Wait for Instance State and Health Checks.
Dry run by default: change dryRun to false deliberately for the live operation. DryRunOperation means the permission check passed, not that a resource was changed or that the later operation is guaranteed to succeed. The example never automatically changes a dry run into a live request.
Uses the current EC2 Query API (Version=2016-11-15) over HTTPS. Chilkat.Rest and AuthAws sign the GET request with AWS Signature Version 4, service ec2. Set the region once; the code builds the corresponding commercial AWS endpoint. Other partitions require their own endpoint suffix. EC2 returns XML, unlike DynamoDB's JSON protocol. AddQueryParam encodes parameter values.
Load current IAM credentials from a private qa_data/aws/ec2.json with string fields access_key, secret_key, and, for temporary credentials, session_token. Prefer short-lived role credentials. The example explicitly reads this file; it does not discover profiles or refresh expired credentials. The session token is included in the signed query, so keep URLs and diagnostics private. A Socket enables TLS certificate verification before handing its connection to Rest.
IAM permissions for the illustrated requests: ec2:RunInstances. Use least-privilege permissions for the intended resources; EC2 describe operations generally require Resource *.
Transport completion, HTTP status, and XML validity are checked separately. API errors contain Errors/Error/Code and RequestID; successful responses use requestId. Check the error code rather than assuming every HTTP 4xx means bad input. Use bounded exponential backoff with jitter for retryable failures. EC2 is eventually consistent: a recent resource change may not appear immediately. Verify account, region, ID, and observed state before retrying a change whose outcome is uncertain.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loCredentials
LOCAL lcRegion
LOCAL loEndpoint
LOCAL loAws
LOCAL loRest
LOCAL loConnection
LOCAL loXml
LOCAL lcResponseXml
LOCAL lcErrorCode
LOCAL lnDryRun
LOCAL lcLaunchFile
LOCAL loFiles
LOCAL loLaunch
LOCAL lnFileStatus
LOCAL loUuid
LOCAL lcClientToken
LOCAL loTokenText
LOCAL lcInstanceId
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
* Private configuration, not a file in public web content or source control.
* Load current IAM credentials authorized for the EC2 operation.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/ec2.json")
IF (lnSuccess <> 1) THEN
? "Could not load private AWS credentials."
RELEASE loCredentials
CANCEL
ENDIF
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (loCredentials.StringOf("access_key") = "")) THEN
? "Missing or invalid access_key."
RELEASE loCredentials
CANCEL
ENDIF
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (loCredentials.StringOf("secret_key") = "")) THEN
? "Missing or invalid secret_key."
RELEASE loCredentials
CANCEL
ENDIF
* Set this once to the region containing the instances.
lcRegion = "us-west-2"
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("ec2.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loAws = CreateObject('Chilkat.AuthAws')
loAws.AccessKey = loCredentials.StringOf("access_key")
loAws.SecretKey = loCredentials.StringOf("secret_key")
loAws.Region = lcRegion
loAws.ServiceName = "ec2"
loAws.SignatureVersion = 4
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
lnSuccess = loRest.SetAuthAws(loAws)
IF (lnSuccess <> 1) THEN
? "Could not configure AWS signing."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
* Temporary IAM/STS credentials require their session token on every signed request.
* AddQueryParam includes it in SigV4's canonical query. Never log the request URL.
* Leave session_token absent or empty only for credentials that do not have one.
IF (loCredentials.HasMember("session_token") = 1) THEN
IF (loCredentials.JsonTypeOf("session_token") <> 1) THEN
? "session_token must be a string."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
IF (loCredentials.StringOf("session_token") <> "") THEN
lnSuccess = loRest.AddQueryParam("X-Amz-Security-Token",loCredentials.StringOf("session_token"))
IF (lnSuccess <> 1) THEN
? "Could not set the session-token parameter."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
ENDIF
ENDIF
* Supply a Socket to explicitly enable TLS certificate verification.
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
? "TLS connection failed; inspect diagnostics privately."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
lnSuccess = loRest.UseConnection(loConnection,1)
IF (lnSuccess <> 1) THEN
? "Could not attach the TLS connection."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
* EC2 uses the Query API and XML responses, not AWS JSON protocol headers.
* AddQueryParam URL-encodes the values; do not pre-encode IDs or tokens.
loXml = CreateObject('Chilkat.Xml')
loXml.Strict = 1
lnSuccess = loRest.AddQueryParam("Version","2016-11-15")
IF (lnSuccess <> 1) THEN
? "Could not set Version."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
CANCEL
ENDIF
lcResponseXml = ""
lcErrorCode = ""
lnDryRun = 1
* Serialize access to this private file. Use a new file only for a NEW intended launch.
lcLaunchFile = "ec2-launch-request.json"
loFiles = CreateObject('Chilkat.FileAccess')
loLaunch = CreateObject('Chilkat.JsonObject')
loLaunch.Strict = 1
lnFileStatus = loFiles.FileExists3(lcLaunchFile)
IF (lnFileStatus = 0) THEN
* Replace these sample settings before the first run. Later runs use the saved file.
loUuid = CreateObject('Chilkat.Crypt2')
lcClientToken = loUuid.GenerateUuid()
loTokenText = CreateObject('Chilkat.StringBuilder')
loTokenText.Append(lcClientToken)
IF (loTokenText.Length <> 36) THEN
? "Could not generate a launch token."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.UpdateString("client_token",lcClientToken)
IF (lnSuccess <> 1) THEN
? "Could not save client token."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.UpdateString("region",lcRegion)
IF (lnSuccess <> 1) THEN
? "Could not save region."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.UpdateString("image_id","ami-0123456789abcdef0")
IF (lnSuccess <> 1) THEN
? "Could not prepare image_id."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.UpdateString("instance_type","t3.micro")
IF (lnSuccess <> 1) THEN
? "Could not prepare instance_type."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.UpdateString("subnet_id","subnet-0123456789abcdef0")
IF (lnSuccess <> 1) THEN
? "Could not prepare subnet_id."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.UpdateString("security_group_id","sg-0123456789abcdef0")
IF (lnSuccess <> 1) THEN
? "Could not prepare security_group_id."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.UpdateString("key_name","my-key-pair")
IF (lnSuccess <> 1) THEN
? "Could not prepare key_name."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.UpdateString("min_count","1")
IF (lnSuccess <> 1) THEN
? "Could not prepare min_count."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.UpdateString("max_count","1")
IF (lnSuccess <> 1) THEN
? "Could not prepare max_count."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.UpdateString("http_tokens","required")
IF (lnSuccess <> 1) THEN
? "Could not prepare http_tokens."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.UpdateString("http_endpoint","enabled")
IF (lnSuccess <> 1) THEN
? "Could not prepare http_endpoint."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
ELSE
IF (lnFileStatus <> 1) THEN
? "Launch path is inaccessible or is not a regular file."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.LoadFile(lcLaunchFile)
IF (lnSuccess <> 1) THEN
? "Could not load the saved launch request."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
ENDIF
IF ((loLaunch.JsonTypeOf("region") <> 1) OR (loLaunch.StringOf("region") = "")) THEN
? "Missing or invalid saved region."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loLaunch.JsonTypeOf("client_token") <> 1) OR (loLaunch.StringOf("client_token") = "")) THEN
? "Missing or invalid saved client_token."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loLaunch.JsonTypeOf("image_id") <> 1) OR (loLaunch.StringOf("image_id") = "")) THEN
? "Missing or invalid saved image_id."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loLaunch.JsonTypeOf("instance_type") <> 1) OR (loLaunch.StringOf("instance_type") = "")) THEN
? "Missing or invalid saved instance_type."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loLaunch.JsonTypeOf("subnet_id") <> 1) OR (loLaunch.StringOf("subnet_id") = "")) THEN
? "Missing or invalid saved subnet_id."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loLaunch.JsonTypeOf("security_group_id") <> 1) OR (loLaunch.StringOf("security_group_id") = "")) THEN
? "Missing or invalid saved security_group_id."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loLaunch.JsonTypeOf("key_name") <> 1) OR (loLaunch.StringOf("key_name") = "")) THEN
? "Missing or invalid saved key_name."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loLaunch.JsonTypeOf("min_count") <> 1) OR (loLaunch.StringOf("min_count") = "")) THEN
? "Missing or invalid saved min_count."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loLaunch.JsonTypeOf("max_count") <> 1) OR (loLaunch.StringOf("max_count") = "")) THEN
? "Missing or invalid saved max_count."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loLaunch.JsonTypeOf("http_tokens") <> 1) OR (loLaunch.StringOf("http_tokens") = "")) THEN
? "Missing or invalid saved http_tokens."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loLaunch.JsonTypeOf("http_endpoint") <> 1) OR (loLaunch.StringOf("http_endpoint") = "")) THEN
? "Missing or invalid saved http_endpoint."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF (loLaunch.StringOf("region") <> lcRegion) THEN
? "Saved launch region differs; do not reuse this token in another region."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loLaunch.StringOf("min_count") <> "1") OR (loLaunch.StringOf("max_count") <> "1")) THEN
? "This demonstration launches exactly one instance."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF (loLaunch.HasMember("instance_id") = 1) THEN
IF ((loLaunch.JsonTypeOf("instance_id") <> 1) OR (loLaunch.StringOf("instance_id") = "")) THEN
? "Missing or invalid saved instance_id."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
? "Launch already recorded for instance: " + loLaunch.StringOf("instance_id")
? "No new launch request was sent."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
* Persist before sending, including dry runs. Never generate a new token for a retry.
lnSuccess = loLaunch.WriteFile(lcLaunchFile)
IF (lnSuccess <> 1) THEN
? "Could not persist launch parameters; no launch sent."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("Action","RunInstances")
IF (lnSuccess <> 1) THEN
? "Could not set Action."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("ClientToken",loLaunch.StringOf("client_token"))
IF (lnSuccess <> 1) THEN
? "Could not set ClientToken."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("ImageId",loLaunch.StringOf("image_id"))
IF (lnSuccess <> 1) THEN
? "Could not set ImageId."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("InstanceType",loLaunch.StringOf("instance_type"))
IF (lnSuccess <> 1) THEN
? "Could not set InstanceType."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("SubnetId",loLaunch.StringOf("subnet_id"))
IF (lnSuccess <> 1) THEN
? "Could not set SubnetId."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("SecurityGroupId.1",loLaunch.StringOf("security_group_id"))
IF (lnSuccess <> 1) THEN
? "Could not set SecurityGroupId.1."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("KeyName",loLaunch.StringOf("key_name"))
IF (lnSuccess <> 1) THEN
? "Could not set KeyName."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("MinCount",loLaunch.StringOf("min_count"))
IF (lnSuccess <> 1) THEN
? "Could not set MinCount."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("MaxCount",loLaunch.StringOf("max_count"))
IF (lnSuccess <> 1) THEN
? "Could not set MaxCount."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("MetadataOptions.HttpTokens",loLaunch.StringOf("http_tokens"))
IF (lnSuccess <> 1) THEN
? "Could not set MetadataOptions.HttpTokens."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("MetadataOptions.HttpEndpoint",loLaunch.StringOf("http_endpoint"))
IF (lnSuccess <> 1) THEN
? "Could not set MetadataOptions.HttpEndpoint."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF (lnDryRun = 1) THEN
lnSuccess = loRest.AddQueryParam("DryRun","true")
IF (lnSuccess <> 1) THEN
? "Could not set DryRun."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
ENDIF
lcResponseXml = loRest.FullRequestNoBody("GET","/")
IF (loRest.LastMethodSuccess <> 1) THEN
? "Request did not complete; inspect diagnostics privately."
* The operation might already have been accepted. Check state before retrying.
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loXml.LoadXml(lcResponseXml)
IF (loRest.ResponseStatusCode <> 200) THEN
? "EC2 HTTP status: " + STR(loRest.ResponseStatusCode)
IF (lnSuccess = 1) THEN
IF (loXml.HasChildWithTag("Errors|Error|Code") = 1) THEN
lcErrorCode = loXml.GetChildContent("Errors|Error|Code")
IF ((lnDryRun = 1) AND (lcErrorCode = "DryRunOperation")) THEN
? "Dry run authorized. No resource was changed."
? "Set dryRun to false only when ready to perform the operation."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
? "AWS error code: " + lcErrorCode
ENDIF
IF (loXml.HasChildWithTag("RequestID") = 1) THEN
? "AWS request ID: " + loXml.GetChildContent("RequestID")
ENDIF
ENDIF
* Keep full error messages and signed request diagnostics private.
* UnauthorizedOperation is not a successful dry run. Check IAM permissions.
* Retry throttled reads with bounded backoff; renew expired credentials.
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF (lnSuccess <> 1) THEN
? "Expected a well-formed XML response."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF (loXml.Tag <> "RunInstancesResponse") THEN
? "Unexpected EC2 response element."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF (lnDryRun = 1) THEN
? "Unexpected HTTP 200 for a dry run; do not treat this as authorization to proceed."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loXml.HasChildWithTag("requestId") <> 1) OR (loXml.GetChildContent("requestId") = "")) THEN
? "Missing or empty requestId."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
? "AWS request ID: " + loXml.GetChildContent("requestId")
IF (loXml.NumChildrenAt("instancesSet") <> 1) THEN
? "Expected exactly one launched instance; reconcile using the saved client token."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
IF ((loXml.HasChildWithTag("instancesSet|item|instanceId") <> 1) OR (loXml.GetChildContent("instancesSet|item|instanceId") = "")) THEN
? "Missing or empty instancesSet|item|instanceId."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lcInstanceId = loXml.GetChildContent("instancesSet|item|instanceId")
? "Launch accepted for instance: " + lcInstanceId
IF (loXml.HasChildWithTag("instancesSet|item|instanceState|name") = 1) THEN
? "State: " + loXml.GetChildContent("instancesSet|item|instanceState|name")
ENDIF
lnSuccess = loLaunch.UpdateString("instance_id",lcInstanceId)
IF (lnSuccess <> 1) THEN
? "Could not record the returned instance ID; keep it privately."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
lnSuccess = loLaunch.WriteFile(lcLaunchFile)
IF (lnSuccess <> 1) THEN
? "Launch accepted but the result could not be saved. Record the ID before retrying."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText
CANCEL
ENDIF
? "Saved instance ID. Wait for running state and health before use."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loLaunch
RELEASE loUuid
RELEASE loTokenText