Xojo Plugin 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"

Xojo Plugin Example

Dim rest As New Chilkat.Rest
Dim success As Boolean

Dim authAws As New Chilkat.AuthAws
authAws.AccessKey = "AWS_ACCESS_KEY"
authAws.SecretKey = "AWS_SECRET_KEY"
authAws.Region = "us-east-1"
authAws.ServiceName = "iam"
rest.SetAuthAws(authAws)

//  URL: https://iam.amazonaws.com/
Dim bTls As Boolean
bTls = True
Dim port As Int32
port = 443
Dim bAutoReconnect As Boolean
bAutoReconnect = True
success = rest.Connect("iam.amazonaws.com",port,bTls,bAutoReconnect)
If (success <> True) Then
    System.DebugLog("ConnectFailReason: " + Str(rest.ConnectFailReason))
    System.DebugLog(rest.LastErrorText)
    Return
End If

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

Dim sbResponseBody As New Chilkat.StringBuilder
success = rest.FullRequestNoBodySb("GET","/",sbResponseBody)
If (success <> True) Then
    System.DebugLog(rest.LastErrorText)
    Return
End If

Dim respStatusCode As Int32
respStatusCode = rest.ResponseStatusCode
If (respStatusCode >= 400) Then
    System.DebugLog("Response Status Code = " + Str(respStatusCode))
    System.DebugLog("Response Header:")
    System.DebugLog(rest.ResponseHeader)
    System.DebugLog("Response Body:")
    System.DebugLog(sbResponseBody.GetAsString())
    Return
End If

Dim xmlResponse As New Chilkat.Xml
xmlResponse.LoadSb(sbResponseBody,True)

Dim CreateRoleResponse_xmlns As String
Dim tagPath As String
Dim Path As String
Dim AssumeRolePolicyDocument As String
Dim RoleId As String
Dim RoleName As String
Dim Arn As String
Dim CreateDate As String
Dim RequestId As String

CreateRoleResponse_xmlns = xmlResponse.GetAttrValue("xmlns")
Path = xmlResponse.GetChildContent("CreateRoleResult|Role|Path")
AssumeRolePolicyDocument = xmlResponse.GetChildContent("CreateRoleResult|Role|AssumeRolePolicyDocument")
RoleId = xmlResponse.GetChildContent("CreateRoleResult|Role|RoleId")
RoleName = xmlResponse.GetChildContent("CreateRoleResult|Role|RoleName")
Arn = xmlResponse.GetChildContent("CreateRoleResult|Role|Arn")
CreateDate = xmlResponse.GetChildContent("CreateRoleResult|Role|CreateDate")
RequestId = xmlResponse.GetChildContent("ResponseMetadata|RequestId")

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>