Amazon SNS: Create or Find a Standard Topic
See more Amazon SNS Examples
Uses the current Amazon SNS Query API (Version=2010-03-31) through Chilkat.Rest and AuthAws, with HTTPS, Signature Version 4, and service sns. The API version date remains current. Action and input parameters are URL-encoded into a POST body, avoiding message text and recipient addresses in the URL. Set the actual region once; other AWS partitions need their documented endpoint suffix and ARN partition.
Load private qa_data/aws/sns.json with nonempty string fields access_key, secret_key, and session_token when using temporary credentials. Prefer short-lived role credentials. This sample does not discover profiles, obtain credentials, or refresh them automatically. The session token is included in the signed form body on every request. A Socket explicitly enables TLS certificate verification before passing the connection to Rest.
Checks setup calls, transport completion, HTTP status, XML parsing, result root, and required fields. Normal output includes identifiers, not full messages or headers. Keep detailed diagnostics private. For retryable failures, use bounded backoff with jitter in the application; these examples do not automatically retry writes. A Publish timeout can leave an accepted message and retrying a standard-topic/SMS publish can duplicate it. A returned MessageId confirms SNS acceptance, not subscriber or handset delivery.
Creates a standard topic or returns the ARN of an existing topic owned by the requester with the same name. This idempotent request is not proof that the topic was newly created. The sample name uses letters, digits, hyphens or underscores; choose 1–256 characters. It does not configure FIFO attributes, encryption, access policies, or tags. Inspect existing attributes separately when reusing a topic.
Required IAM action: sns:CreateTopic. Grant only the intended resource access; direct SMS does not have a topic ARN resource. Topic policies and KMS key access can also affect requests.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
DECLARE @stringCompare int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @stringCompare OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Private credentials; do not put this file in public web content or source control.
DECLARE @credentials int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @credentials OUT
EXEC sp_OASetProperty @credentials, 'Strict', 1
EXEC sp_OAMethod @credentials, 'LoadFile', @success OUT, 'qa_data/aws/sns.json'
IF @success <> 1
BEGIN
PRINT 'Could not load private SNS credentials.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
RETURN
END
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch1 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch1 OUT, '', 1
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
IF (@iTmp0 <> 1) or (@stringMatch1 = 1)
BEGIN
PRINT 'Missing or invalid access_key.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
RETURN
END
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch2 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch2 OUT, '', 1
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'secret_key'
IF (@iTmp0 <> 1) or (@stringMatch2 = 1)
BEGIN
PRINT 'Missing or invalid secret_key.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
RETURN
END
DECLARE @region nvarchar(4000)
SELECT @region = 'us-west-2'
DECLARE @endpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'sns.'
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @region
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com'
DECLARE @aws int
EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @aws OUT
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
EXEC sp_OASetProperty @aws, 'AccessKey', @sTmp0
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
EXEC sp_OASetProperty @aws, 'SecretKey', @sTmp0
EXEC sp_OASetProperty @aws, 'Region', @region
EXEC sp_OASetProperty @aws, 'ServiceName', 'sns'
EXEC sp_OASetProperty @aws, 'SignatureVersion', 4
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
EXEC sp_OASetProperty @rest, 'ConnectTimeoutMs', 20000
EXEC sp_OASetProperty @rest, 'IdleTimeoutMs', 60000
EXEC sp_OASetProperty @rest, 'VerboseLogging', 0
EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @aws
IF @success <> 1
BEGIN
PRINT 'Could not configure AWS signing.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch3 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch3 OUT, '', 1
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'session_token'
IF (@iTmp0 <> 1) or (@stringMatch3 = 1)
BEGIN
PRINT 'session_token must be a nonempty string when supplied.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
END
DECLARE @xml int
EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT
EXEC sp_OASetProperty @xml, 'Strict', 1
DECLARE @value nvarchar(4000)
DECLARE @topicName nvarchar(4000)
SELECT @topicName = 'chilkat-standard-example'
-- Explicit certificate verification; keep this Socket alive while Rest uses it.
DECLARE @connection int
EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT
EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @connection, 'MaxReadIdleMs', 60000
EXEC sp_OASetProperty @connection, 'MaxSendIdleMs', 60000
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
IF @success <> 1
BEGIN
PRINT 'TLS connection failed; inspect diagnostics privately.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 1
IF @success <> 1
BEGIN
PRINT 'Could not attach TLS connection.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAMethod @rest, 'ClearAllQueryParams', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not clear parameters from the previous request.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
-- Build a fresh form for each request; do not retain previous parameters.
-- AddQueryParam encodes raw values into the POST body. Do not pre-encode them.
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'Action', 'CreateTopic'
IF @success <> 1
BEGIN
PRINT 'Could not set Action.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'Version', '2010-03-31'
IF @success <> 1
BEGIN
PRINT 'Could not set Version.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
-- Include temporary credentials in the signed form body on every request.
EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not set X-Amz-Security-Token.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
END
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'Name', @topicName
IF @success <> 1
BEGIN
PRINT 'Could not set Name.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
DECLARE @responseXml nvarchar(4000)
EXEC sp_OAMethod @rest, 'FullRequestFormUrlEncoded', @responseXml OUT, 'POST', '/'
EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Request did not complete. Its outcome may be uncertain; reconcile before retrying.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @responseXml
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
PRINT 'SNS HTTP status: ' + @iTmp0
IF @success = 1
BEGIN
EXEC sp_OAMethod @xml, 'GetChildContent', @value OUT, 'Error|Code'
EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 1
BEGIN
PRINT 'AWS error code: ' + @value
END
EXEC sp_OAMethod @xml, 'GetChildContent', @value OUT, 'RequestId'
EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 1
BEGIN
PRINT 'AWS request ID: ' + @value
END
END
-- Full messages, headers, and signing diagnostics may contain private data.
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
IF @success <> 1
BEGIN
PRINT 'Expected a well-formed XML response.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAGetProperty @xml, 'Tag', @sTmp0 OUT
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch4 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch4 OUT, 'CreateTopicResponse', 1
IF @stringMatch4 <> 1
BEGIN
PRINT 'Unexpected SNS response element.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAMethod @xml, 'GetChildContent', @value OUT, 'ResponseMetadata|RequestId'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @value
DECLARE @stringMatch5 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch5 OUT, '', 1
EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
IF (@iTmp0 <> 1) or (@stringMatch5 = 1)
BEGIN
PRINT 'Missing or empty ResponseMetadata|RequestId.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
PRINT 'AWS request ID: ' + @value
EXEC sp_OAMethod @xml, 'GetChildContent', @value OUT, 'CreateTopicResult|TopicArn'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @value
DECLARE @stringMatch6 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch6 OUT, '', 1
EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
IF (@iTmp0 <> 1) or (@stringMatch6 = 1)
BEGIN
PRINT 'Missing or empty CreateTopicResult|TopicArn.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
RETURN
END
PRINT 'Topic ARN: ' + @value
PRINT 'Standard topic is available (created or already owned).'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @connection
END
GO