SQL Server AWS IAM: Create Role

Back to Index

Creates a new role for your AWS account.

Documentation: https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateRole.html

CURL Command

curl -X GET https://iam.amazonaws.com/ \
  -d "Action=CreateRole" \
  -d "RoleName=S3Access" \
  -d "PathPrefix=/" \
  -d 'AssumeRolePolicyDocument={"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":["ec2.amazonaws.com"]},"Action":["sts:AssumeRole"]}]}' \
  -d "Version=2010-05-08"

SQL Server Example

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @authAws int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.AuthAws', @authAws OUT

    EXEC sp_OASetProperty @authAws, 'AccessKey', 'AWS_ACCESS_KEY'
    EXEC sp_OASetProperty @authAws, 'SecretKey', 'AWS_SECRET_KEY'
    EXEC sp_OASetProperty @authAws, 'Region', 'us-east-1'
    EXEC sp_OASetProperty @authAws, 'ServiceName', 'iam'
    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, STR(@authAws)

    --  URL: https://iam.amazonaws.com/
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'iam.amazonaws.com', STR(@port), STR(@bTls), STR(@bAutoReconnect)
    IF STR(@success) <> 1
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ConnectFailReason', @iTmp0 OUT
        PRINT 'ConnectFailReason: ' + @iTmp0
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        RETURN
      END

    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'Action', 'CreateRole'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'RoleName', 'S3Access'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'PathPrefix', '/'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'AssumeRolePolicyDocument', '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":["ec2.amazonaws.com"]},"Action":["sts:AssumeRole"]}]}'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'Version', '2010-05-08'

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/', STR(@sbResponseBody)
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END
    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @respStatusCode OUT
    IF STR(@respStatusCode) >= 400
      BEGIN

        PRINT 'Response Status Code = ' + STR(@respStatusCode)

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Response Body:'
        EXEC sp_OAMethod @sbResponseBody, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @xmlResponse int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Xml', @xmlResponse OUT

    EXEC sp_OAMethod @xmlResponse, 'LoadSb', @success OUT, STR(@sbResponseBody), 1

    DECLARE @CreateRoleResponse_xmlns nvarchar(4000)

    DECLARE @tagPath nvarchar(4000)

    DECLARE @Path nvarchar(4000)

    DECLARE @AssumeRolePolicyDocument nvarchar(4000)

    DECLARE @RoleId nvarchar(4000)

    DECLARE @RoleName nvarchar(4000)

    DECLARE @Arn nvarchar(4000)

    DECLARE @CreateDate nvarchar(4000)

    DECLARE @RequestId nvarchar(4000)

    EXEC sp_OAMethod @xmlResponse, 'GetAttrValue', @CreateRoleResponse_xmlns OUT, 'xmlns'
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @Path OUT, 'CreateRoleResult|Role|Path'
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @AssumeRolePolicyDocument OUT, 'CreateRoleResult|Role|AssumeRolePolicyDocument'
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @RoleId OUT, 'CreateRoleResult|Role|RoleId'
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @RoleName OUT, 'CreateRoleResult|Role|RoleName'
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @Arn OUT, 'CreateRoleResult|Role|Arn'
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @CreateDate OUT, 'CreateRoleResult|Role|CreateDate'
    EXEC sp_OAMethod @xmlResponse, 'GetChildContent', @RequestId OUT, 'ResponseMetadata|RequestId'

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @authAws
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @xmlResponse


END
GO

Sample XML Response Body

<?xml version="1.0" encoding="utf-8" ?>
<CreateRoleResponse xmlns="https://iam.amazonaws.com/doc/2010-05-08/">
    <CreateRoleResult>
        <Role>
            <Path>/</Path>
            <AssumeRolePolicyDocument>%7B%22Version%22%3A%222012-10-17%22%2C%22Statement%22%3A%5B%7B%22Effect%22%3A%22Allow%22%2C%22Principal%22%3A%7B%22Service%22%3A%5B%22ec2.amazonaws.com%22%5D%7D%2C%22Action%22%3A%5B%22sts%3AAssumeRole%22%5D%7D%5D%7D</AssumeRolePolicyDocument>
            <RoleId>AROAINN6SXEVNWCLTXEZK</RoleId>
            <RoleName>S3Access</RoleName>
            <Arn>arn:aws:iam::954491834127:role/S3Access</Arn>
            <CreateDate>2018-05-20T12:50:13.634Z</CreateDate>
        </Role>
    </CreateRoleResult>
    <ResponseMetadata>
        <RequestId>5699b365-5c2c-11e8-8b73-3b21c350d521</RequestId>
    </ResponseMetadata>
</CreateRoleResponse>