Sample code for 30+ languages & platforms
Lua

AWS Security Token Service (STS) GetSessionToken

See more AWS Security Token Service Examples

Returns a set of temporary credentials for an AWS account or IAM user.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local rest = chilkat.Rest.new()

-- Connect to the Amazon AWS REST server.
-- such as https://sts.us-west-2.amazonaws.com/
local bTls = true
local port = 443
local bAutoReconnect = true
local success = rest:connect("sts.us-west-2.amazonaws.com", port, bTls, bAutoReconnect)

-- Provide AWS credentials for the REST call.
local authAws = chilkat.AuthAws.new()
authAws.access_key = "AWS_ACCESS_KEY"
authAws.secret_key = "AWS_SECRET_KEY"
-- the region should match our URL above..
-- See https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_enable-regions.html
authAws.region = "us-west-2"
authAws.service_name = "sts"

rest:set_auth_aws(authAws)

rest:add_query_param("Version", "2011-06-15")
rest:add_query_param("Action", "GetSessionToken")
rest:add_query_param("DurationSeconds", "3600")

local responseXml = rest:full_request_no_body("GET", "/")
if not rest.last_method_success then
    print(rest.last_error_text)
    return
end

-- A successful response will have a status code equal to 200.
if rest.response_status_code ~= 200 then
    print("response status code = " .. rest.response_status_code)
    print("response status text = " .. rest.response_status_text)
    print("response header: " .. rest.response_header)
    print("response body: " .. responseXml)
    return
end

-- Examine the successful XML response (shown below)
local xml = chilkat.Xml.new()
xml:load_xml(responseXml)
print(xml:get_xml())

-- Sample response:

-- <?xml version="1.0" encoding="utf-8"?>
-- <GetSessionTokenResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
--     <GetSessionTokenResult>
--         <Credentials>
--             <AccessKeyId>AS........T4N</AccessKeyId>
--             <SecretAccessKey>05W........ARPMr</SecretAccessKey>
--             <SessionToken>IQoJb3J........llpIMI=</SessionToken>
--             <Expiration>2022-09-07T00:22:51Z</Expiration>
--         </Credentials>
--     </GetSessionTokenResult>
--     <ResponseMetadata>
--         <RequestId>8bad22cc-1c55-4265-a010-45d139359404</RequestId>
--     </ResponseMetadata>
-- </GetSessionTokenResponse>

-- Sample parse code:
local GetSessionTokenResponse_xmlns = xml:get_attr_value("xmlns")
local AccessKeyId = xml:get_child_content("GetSessionTokenResult|Credentials|AccessKeyId")
local SecretAccessKey = xml:get_child_content("GetSessionTokenResult|Credentials|SecretAccessKey")
local SessionToken = xml:get_child_content("GetSessionTokenResult|Credentials|SessionToken")
local Expiration = xml:get_child_content("GetSessionTokenResult|Credentials|Expiration")
local RequestId = xml:get_child_content("ResponseMetadata|RequestId")

-- Save the session token XML to a file for use by another Chilkat example..
success = xml:save_xml("qa_data/tokens/aws_session_token.xml")