IAM (Identity and Access Management) code examples for the SDK for .NET

IAM Basics

  1. Creates a user with no permissions.
  2. Creates a role and policy that grant s3:ListAllMyBuckets permission.
  3. Grants the user permission to assume the role.
  4. Creates an S3 client object as the user and tries to list buckets (this will fail).
  5. Gets temporary credentials by assuming the role.
  6. Creates a new S3 client object with the temporary credentials and lists the buckets (this will succeed).
  7. Deletes all the resources.

IAM Groups

  1. Creates an Amazon Identity and Access Management (IAM) group.
  2. Adds an IAM policy to the IAM group giving it full access to Amazon S3.
  3. Creates a new IAM user.
  4. Creates an IAM access key for the user.
  5. Adds the user to the IAM group.
  6. Lists the buckets on the account.
  7. Proves that the user has full Amazon S3 access by creating a bucket.
  8. List the buckets again to show the new bucket.
  9. Cleans up all the resources created.

Overview

Shows how to use the AWS SDK for .NET to work with AWS Identity and Access Management (IAM).

IAM is a web service for securely controlling access to AWS services. With IAM, you can centrally manage permissions in your AWS account.

⚠ Important

  • Running this code might result in charges to your AWS account.
  • Running the tests might result in charges to your AWS account.
  • We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see Grant least privilege.
  • This code is not tested in every AWS Region. For more information, see AWS Regional Services.

Code examples

Prerequisites

For prerequisites, see the README in the dotnetv3 folder.

Get started

Single actions

Code excerpts that show you how to call individual service functions.

Scenarios

Code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Run the examples

Instructions

For general instructions to run the examples, see the README in the dotnetv3 folder.

Some projects might include a settings.json file. Before compiling the project, you can change these values to match your own account and resources. Alternatively, add a settings.local.json file with your local settings, which will be loaded automatically when the application runs.

After the example compiles, you can run it from the command line. To do so, navigate to the folder that contains the .csproj file and run the following command:

dotnet run

Alternatively, you can run the example from within your IDE.

To run the examples, see the README in the dotnetv3 folder.

Hello IAM

This example shows you how to get started using IAM.

Create a group and add a user

This example shows you how to do the following:

  • Create a group and grant full Amazon S3 access permissions to it.
  • Create a new user with no permissions to access Amazon S3.
  • Add the user to the group and show that they now have permissions for Amazon S3, then clean up resources.

Create a user and assume a role

This example shows you how to create a user and assume a role.

  • Create a user with no permissions.
  • Create a role that grants permission to list Amazon S3 buckets for the account.
  • Add a policy to let the user assume the role.
  • Assume the role and list S3 buckets using temporary credentials, then clean up resources.

Tests

⚠ Running tests might result in charges to your AWS account.

To find instructions for running these tests, see the README in the dotnetv3 folder.

Additional resources


Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0