IAM examples using SDK for Swift - AWS SDK for Swift

IAM examples using SDK for Swift

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Swift with IAM.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use AttachRolePolicy.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func attachRolePolicy(role: String, policyArn: String) async throws { let input = AttachRolePolicyInput( policyArn: policyArn, roleName: role ) do { _ = try await client.attachRolePolicy(input: input) } catch { throw error } }

The following code example shows how to use CreateAccessKey.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func createAccessKey(userName: String) async throws -> IAMClientTypes.AccessKey { let input = CreateAccessKeyInput( userName: userName ) do { let output = try await iamClient.createAccessKey(input: input) guard let accessKey = output.accessKey else { throw ServiceHandlerError.keyError } return accessKey } catch { throw error } }

The following code example shows how to use CreatePolicy.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func createPolicy(name: String, policyDocument: String) async throws -> IAMClientTypes.Policy { let input = CreatePolicyInput( policyDocument: policyDocument, policyName: name ) do { let output = try await iamClient.createPolicy(input: input) guard let policy = output.policy else { throw ServiceHandlerError.noSuchPolicy } return policy } catch { throw error } }
  • For API details, see CreatePolicy in AWS SDK for Swift API reference.

The following code example shows how to use CreateRole.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func createRole(name: String, policyDocument: String) async throws -> String { let input = CreateRoleInput( assumeRolePolicyDocument: policyDocument, roleName: name ) do { let output = try await client.createRole(input: input) guard let role = output.role else { throw ServiceHandlerError.noSuchRole } guard let id = role.roleId else { throw ServiceHandlerError.noSuchRole } return id } catch { throw error } }
  • For API details, see CreateRole in AWS SDK for Swift API reference.

The following code example shows how to use CreateServiceLinkedRole.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func createServiceLinkedRole(service: String, suffix: String? = nil, description: String?) async throws -> IAMClientTypes.Role { let input = CreateServiceLinkedRoleInput( awsServiceName: service, customSuffix: suffix, description: description ) do { let output = try await client.createServiceLinkedRole(input: input) guard let role = output.role else { throw ServiceHandlerError.noSuchRole } return role } catch { throw error } }

The following code example shows how to use CreateUser.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func createUser(name: String) async throws -> String { let input = CreateUserInput( userName: name ) do { let output = try await client.createUser(input: input) guard let user = output.user else { throw ServiceHandlerError.noSuchUser } guard let id = user.userId else { throw ServiceHandlerError.noSuchUser } return id } catch { throw error } }
  • For API details, see CreateUser in AWS SDK for Swift API reference.

The following code example shows how to use DeleteAccessKey.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func deleteAccessKey(user: IAMClientTypes.User? = nil, key: IAMClientTypes.AccessKey) async throws { let userName: String? if user != nil { userName = user!.userName } else { userName = nil } let input = DeleteAccessKeyInput( accessKeyId: key.accessKeyId, userName: userName ) do { _ = try await iamClient.deleteAccessKey(input: input) } catch { throw error } }

The following code example shows how to use DeletePolicy.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func deletePolicy(policy: IAMClientTypes.Policy) async throws { let input = DeletePolicyInput( policyArn: policy.arn ) do { _ = try await iamClient.deletePolicy(input: input) } catch { throw error } }
  • For API details, see DeletePolicy in AWS SDK for Swift API reference.

The following code example shows how to use DeleteRole.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func deleteRole(role: IAMClientTypes.Role) async throws { let input = DeleteRoleInput( roleName: role.roleName ) do { _ = try await iamClient.deleteRole(input: input) } catch { throw error } }
  • For API details, see DeleteRole in AWS SDK for Swift API reference.

The following code example shows how to use DeleteUser.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func deleteUser(user: IAMClientTypes.User) async throws { let input = DeleteUserInput( userName: user.userName ) do { _ = try await iamClient.deleteUser(input: input) } catch { throw error } }
  • For API details, see DeleteUser in AWS SDK for Swift API reference.

The following code example shows how to use DeleteUserPolicy.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

func deleteUserPolicy(user: IAMClientTypes.User, policyName: String) async throws { let input = DeleteUserPolicyInput( policyName: policyName, userName: user.userName ) do { _ = try await iamClient.deleteUserPolicy(input: input) } catch { throw error } }

The following code example shows how to use DetachRolePolicy.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func detachRolePolicy(policy: IAMClientTypes.Policy, role: IAMClientTypes.Role) async throws { let input = DetachRolePolicyInput( policyArn: policy.arn, roleName: role.roleName ) do { _ = try await iamClient.detachRolePolicy(input: input) } catch { throw error } }

The following code example shows how to use GetPolicy.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func getPolicy(arn: String) async throws -> IAMClientTypes.Policy { let input = GetPolicyInput( policyArn: arn ) do { let output = try await client.getPolicy(input: input) guard let policy = output.policy else { throw ServiceHandlerError.noSuchPolicy } return policy } catch { throw error } }
  • For API details, see GetPolicy in AWS SDK for Swift API reference.

The following code example shows how to use GetRole.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func getRole(name: String) async throws -> IAMClientTypes.Role { let input = GetRoleInput( roleName: name ) do { let output = try await client.getRole(input: input) guard let role = output.role else { throw ServiceHandlerError.noSuchRole } return role } catch { throw error } }
  • For API details, see GetRole in AWS SDK for Swift API reference.

The following code example shows how to use ListAttachedRolePolicies.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

/// Returns a list of AWS Identity and Access Management (IAM) policies /// that are attached to the role. /// /// - Parameter role: The IAM role to return the policy list for. /// /// - Returns: An array of `IAMClientTypes.AttachedPolicy` objects /// describing each managed policy that's attached to the role. public func listAttachedRolePolicies(role: String) async throws -> [IAMClientTypes.AttachedPolicy] { var policyList: [IAMClientTypes.AttachedPolicy] = [] var marker: String? = nil var isTruncated: Bool repeat { let input = ListAttachedRolePoliciesInput( marker: marker, roleName: role ) let output = try await client.listAttachedRolePolicies(input: input) guard let attachedPolicies = output.attachedPolicies else { return policyList } for attachedPolicy in attachedPolicies { policyList.append(attachedPolicy) } marker = output.marker isTruncated = output.isTruncated } while isTruncated == true return policyList }

The following code example shows how to use ListGroups.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func listGroups() async throws -> [String] { var groupList: [String] = [] var marker: String? = nil var isTruncated: Bool repeat { let input = ListGroupsInput(marker: marker) let output = try await client.listGroups(input: input) guard let groups = output.groups else { return groupList } for group in groups { if let name = group.groupName { groupList.append(name) } } marker = output.marker isTruncated = output.isTruncated } while isTruncated == true return groupList }
  • For API details, see ListGroups in AWS SDK for Swift API reference.

The following code example shows how to use ListPolicies.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func listPolicies() async throws -> [MyPolicyRecord] { var policyList: [MyPolicyRecord] = [] var marker: String? = nil var isTruncated: Bool repeat { let input = ListPoliciesInput(marker: marker) let output = try await client.listPolicies(input: input) guard let policies = output.policies else { return policyList } for policy in policies { guard let name = policy.policyName, let id = policy.policyId, let arn = policy.arn else { throw ServiceHandlerError.noSuchPolicy } policyList.append(MyPolicyRecord(name: name, id: id, arn: arn)) } marker = output.marker isTruncated = output.isTruncated } while isTruncated == true return policyList }
  • For API details, see ListPolicies in AWS SDK for Swift API reference.

The following code example shows how to use ListRolePolicies.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func listRolePolicies(role: String) async throws -> [String] { var policyList: [String] = [] var marker: String? = nil var isTruncated: Bool repeat { let input = ListRolePoliciesInput( marker: marker, roleName: role ) let output = try await client.listRolePolicies(input: input) guard let policies = output.policyNames else { return policyList } for policy in policies { policyList.append(policy) } marker = output.marker isTruncated = output.isTruncated } while isTruncated == true return policyList }

The following code example shows how to use ListRoles.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func listRoles() async throws -> [String] { var roleList: [String] = [] var marker: String? = nil var isTruncated: Bool repeat { let input = ListRolesInput(marker: marker) let output = try await client.listRoles(input: input) guard let roles = output.roles else { return roleList } for role in roles { if let name = role.roleName { roleList.append(name) } } marker = output.marker isTruncated = output.isTruncated } while isTruncated == true return roleList }
  • For API details, see ListRoles in AWS SDK for Swift API reference.

The following code example shows how to use ListUsers.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public func listUsers() async throws -> [MyUserRecord] { var userList: [MyUserRecord] = [] var marker: String? = nil var isTruncated: Bool repeat { let input = ListUsersInput(marker: marker) let output = try await client.listUsers(input: input) guard let users = output.users else { return userList } for user in users { if let id = user.userId, let name = user.userName { userList.append(MyUserRecord(id: id, name: name)) } } marker = output.marker isTruncated = output.isTruncated } while isTruncated == true return userList }
  • For API details, see ListUsers in AWS SDK for Swift API reference.

The following code example shows how to use PutUserPolicy.

SDK for Swift
Note

This is prerelease documentation for an SDK in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

func putUserPolicy(policyDocument: String, policyName: String, user: IAMClientTypes.User) async throws { let input = PutUserPolicyInput( policyDocument: policyDocument, policyName: policyName, userName: user.userName ) do { _ = try await iamClient.putUserPolicy(input: input) } catch { throw error } }
  • For API details, see PutUserPolicy in AWS SDK for Swift API reference.