

 AWS SDK for .NET V3가 유지 관리 모드로 전환되었습니다.

[AWS SDK for .NET V4](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/welcome.html)로 마이그레이션하는 것이 좋습니다. 마이그레이션 방법에 대한 자세한 내용과 정보는 [유지 관리 모드 공지](https://aws.amazon.com/blogs/developer/aws-sdk-for-net-v3-maintenance-mode-announcement/)를 참조하세요.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# IAM 역할을 사용하여 액세스 권한 부여
<a name="net-dg-hosm"></a>

이 자습서에서는 AWS SDK for .NET 를 사용하여 Amazon EC2 인스턴스에서 IAM 역할을 활성화하는 방법을 보여줍니다.

## 개요
<a name="hosm-overview"></a>

에 대한 모든 요청은에서 발급한 자격 증명을 사용하여 암호화 방식으로 서명해야 AWS 합니다 AWS. 따라서 Amazon EC2 인스턴스에서 실행되는 애플리케이션에 대한 자격 증명을 관리할 전략이 필요합니다. 이 자격 증명을 안전하게 배포, 저장, 교체해야 할 뿐 아니라 애플리케이션에 접근할 수 있는 상태로 유지해야 합니다.

IAM 역할을 사용하면 이러한 자격 증명을 효과적으로 관리할 수 있습니다. IAM 역할을 생성하고 애플리케이션에 필요한 권한으로 구성한 다음 해당 역할을 EC2 인스턴스에 연결합니다. IAM 역할 사용의 이점에 대한 자세한 내용은 [Amazon EC2 사용 설명서](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/)의 [Amazon EC2에 대한 IAM 역할](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html)을 참조하세요. 또한 IAM 사용 설명서의 [IAM 역할](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html)에 대한 정보도 참조하세요.

를 사용하여 빌드된 애플리케이션의 경우 애플리케이션이 AWS 서비스에 대한 클라이언트 객체를 구성할 AWS SDK for .NET때 객체는 여러 잠재적 소스에서 자격 증명을 검색합니다. 검색 순서는 [보안 인증 정보 및 프로파일 확인](creds-assign.md)에 나와 있습니다.

클라이언트 객체가 다른 소스에서 보안 인증을 찾을 수 없는 경우, IAM 역할에 구성된 것과 동일한 권한이 있고 EC2 인스턴스의 메타데이터에 있는 임시 보안 인증을 검색합니다. 이러한 자격 증명은 클라이언트 객체 AWS 에서를 호출하는 데 사용됩니다.

## 이 자습서 소개
<a name="about-hosm-tutorial"></a>

이 자습서를 따르면 AWS SDK for .NET (및 기타 도구)를 사용하여 IAM 역할이 연결된 Amazon EC2 인스턴스를 시작한 다음 IAM 역할의 권한을 사용하여 인스턴스에서 애플리케이션을 볼 수 있습니다.

**Topics**
+ [개요](#hosm-overview)
+ [이 자습서 소개](#about-hosm-tutorial)
+ [샘플 Amazon S3 애플리케이션 생성](#net-dg-hosm-sample-s3-app)
+ [IAM 역할 생성](#net-dg-hosm-create-the-role)
+ [EC2 인스턴스 시작 및 IAM 역할 연결](#net-dg-hosm-launch-ec2-instance)
+ [EC2 인스턴스에 연결](#net-dg-hosm-connect)
+ [EC2 인스턴스에서 샘플 애플리케이션 실행](#net-dg-hosm-run-the-app)
+ [정리](#net-dg-hosm-cleanup)

## 샘플 Amazon S3 애플리케이션 생성
<a name="net-dg-hosm-sample-s3-app"></a>

이 샘플 애플리케이션은 Amazon S3에서 객체를 검색합니다. 애플리케이션을 실행하려면 다음이 필요합니다.
+ 텍스트 파일이 포함된 Amazon S3 버킷입니다.
+ AWS 버킷에 액세스할 수 있는 개발 시스템의 자격 증명입니다.

Amazon S3 버킷 생성 및 객체 업로드 방법에 대한 자세한 내용은 [Amazon Simple Storage Service 사용 설명서](https://docs.aws.amazon.com/AmazonS3/latest/userguide/)를 참조하세요. 자격 AWS 증명에 대한 자세한 내용은 섹션을 참조하세요[를 사용하여 SDK 인증 구성 AWS](creds-idc.md).

다음 코드를 사용하여 .NET Core 프로젝트를 생성합니다. 그런 다음 개발 머신에서 애플리케이션을 테스트합니다.

**참고**  
개발 머신에 .NET Core 런타임이 설치되어 있으므로 애플리케이션을 게시하지 않고도 애플리케이션을 실행할 수 있습니다. 이 자습서의 후반부에서 EC2 인스턴스를 생성할 때 인스턴스에 .NET Core 런타임을 설치하도록 선택할 수 있습니다. 이렇게 하면 비슷한 환경에서 더 작은 파일을 전송할 수 있습니다.  
 하지만 인스턴스에 .NET Core 런타임을 설치하지 않도록 선택할 수도 있습니다. 이 작업 과정을 선택하는 경우 애플리케이션을 인스턴스로 전송할 때 모든 종속성이 포함되도록 애플리케이션을 게시해야 합니다.

### SDK 레퍼런스
<a name="w2aac19c15c19c25c17c13b1"></a>

NuGet 패키지:
+ [AWSSDK.S3](https://www.nuget.org/packages/AWSSDK.S3)

프로그래밍 요소:
+ 네임스페이스 [Amazon.S3](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/NS3.html)

  클래스 [AmazonS3Client](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TS3Client.html)
+ 네임스페이스 [Amazon.S3.Model](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/NS3Model.html)

  클래스 [GetObjectResponse](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TGetObjectResponse.html)

### 코드
<a name="w2aac19c15c19c25c17c15b1"></a>

```
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;

namespace S3GetTextItem
{
  // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  // Class to retrieve a text file from an S3 bucket and write it to a local file
  class Program
  {
    static async Task Main(string[] args)
    {
      // Parse the command line and show help if necessary
      var parsedArgs = CommandLine.Parse(args);
      if(parsedArgs.Count == 0)
      {
        PrintHelp();
        return;
      }

      // Get the application arguments from the parsed list
      string bucket =
        CommandLine.GetArgument(parsedArgs, null, "-b", "--bucket-name");
      string item =
        CommandLine.GetArgument(parsedArgs, null, "-t", "--text-object");
      string outFile =
        CommandLine.GetArgument(parsedArgs, null, "-o", "--output-filename");
      if(   string.IsNullOrEmpty(bucket)
         || string.IsNullOrEmpty(item)
         || string.IsNullOrEmpty(outFile))
        CommandLine.ErrorExit(
          "\nOne or more of the required arguments is missing or incorrect." +
          "\nRun the command with no arguments to see help.");

      // Create the S3 client object and get the file object from the bucket.
      var response = await GetObject(new AmazonS3Client(), bucket, item);

      // Write the contents of the file object to the given output file.
      var reader = new StreamReader(response.ResponseStream);
      string contents = reader.ReadToEnd();
      using (var s = new FileStream(outFile, FileMode.Create))
      using (var writer = new StreamWriter(s))
        writer.WriteLine(contents);
    }


    //
    // Method to get an object from an S3 bucket.
    private static async Task<GetObjectResponse> GetObject(
      IAmazonS3 s3Client, string bucket, string item)
    {
        Console.WriteLine($"Retrieving {item} from bucket {bucket}.");
        return await s3Client.GetObjectAsync(bucket, item);
    }


    //
    // Command-line help
    private static void PrintHelp()
    {
      Console.WriteLine(
        "\nUsage: S3GetTextItem -b <bucket-name> -t <text-object> -o <output-filename>" +
        "\n  -b, --bucket-name: The name of the S3 bucket." +
        "\n  -t, --text-object: The name of the text object in the bucket." +
        "\n  -o, --output-filename: The name of the file to write the text to.");
    }
  }


  // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  // Class that represents a command line on the console or terminal.
  // (This is the same for all examples. When you have seen it once, you can ignore it.)
  static class CommandLine
  {
    //
    // Method to parse a command line of the form: "--key value" or "-k value".
    //
    // Parameters:
    // - args: The command-line arguments passed into the application by the system.
    //
    // Returns:
    // A Dictionary with string Keys and Values.
    //
    // If a key is found without a matching value, Dictionary.Value is set to the key
    //  (including the dashes).
    // If a value is found without a matching key, Dictionary.Key is set to "--NoKeyN",
    //  where "N" represents sequential numbers.
    public static Dictionary<string,string> Parse(string[] args)
    {
      var parsedArgs = new Dictionary<string,string>();
      int i = 0, n = 0;
      while(i < args.Length)
      {
        // If the first argument in this iteration starts with a dash it's an option.
        if(args[i].StartsWith("-"))
        {
          var key = args[i++];
          var value = key;

          // Check to see if there's a value that goes with this option?
          if((i < args.Length) && (!args[i].StartsWith("-"))) value = args[i++];
          parsedArgs.Add(key, value);
        }

        // If the first argument in this iteration doesn't start with a dash, it's a value
        else
        {
          parsedArgs.Add("--NoKey" + n.ToString(), args[i++]);
          n++;
        }
      }

      return parsedArgs;
    }

    //
    // Method to get an argument from the parsed command-line arguments
    //
    // Parameters:
    // - parsedArgs: The Dictionary object returned from the Parse() method (shown above).
    // - defaultValue: The default string to return if the specified key isn't in parsedArgs.
    // - keys: An array of keys to look for in parsedArgs.
    public static string GetArgument(
      Dictionary<string,string> parsedArgs, string defaultReturn, params string[] keys)
    {
      string retval = null;
      foreach(var key in keys)
        if(parsedArgs.TryGetValue(key, out retval)) break;
      return retval ?? defaultReturn;
    }

    //
    // Method to exit the application with an error.
    public static void ErrorExit(string msg, int code=1)
    {
      Console.WriteLine("\nError");
      Console.WriteLine(msg);
      Environment.Exit(code);
    }
  }

}
```

원하는 경우 개발 머신에서 사용하는 보안 인증을 일시적으로 제거하여 애플리케이션이 어떻게 반응하는지 확인할 수 있습니다. (하지만 작업을 마치면 보안 인증을 복원해야 합니다.)

## IAM 역할 생성
<a name="net-dg-hosm-create-the-role"></a>

Amazon S3에 액세스할 수 있는 적절한 권한이 있는 IAM 역할을 생성합니다.

1. [IAM 콘솔](https://console.aws.amazon.com/iam/)을 엽니다.

1. 탐색 창에서 **역할**을 선택한 후 **역할 생성**을 선택합니다.

1. **AWS 서비스**를 선택하고, **EC2**를 찾아 선택하고, **다음: 권한**을 선택합니다.

1. **권한 정책 연결**에서 **AmazonS3ReadOnlyAccess**를 찾아 선택합니다. 원하는 경우 정책을 검토한 후 **다음: 태그**를 선택합니다.

1. 원하는 경우 태그를 추가한 후 **다음: 검토**를 선택합니다.

1. 역할 이름 및 설명을 입력한 후 **역할 생성**을 선택합니다. EC2 인스턴스를 시작할 때 필요하므로 이 이름을 기억해 두십시오.

## EC2 인스턴스 시작 및 IAM 역할 연결
<a name="net-dg-hosm-launch-ec2-instance"></a>

앞에서 생성한 IAM 역할로 EC2 인스턴스를 시작합니다. 다음과 같은 방법으로 수행할 수 있습니다.
+ **EC2 콘솔 사용**

  EC2 콘솔을 사용하여 인스턴스를 실행하려면 [Amazon EC2 사용 설명서](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/)의 [새로운 인스턴스 시작 마법사를 사용하여 인스턴스 시작](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-instance-wizard.html)을 참조하세요.

  시작 페이지를 살펴볼 때 **IAM 인스턴스 프로파일**에서 이전에 생성한 IAM 역할을 지정할 수 있도록 최소한 **고급 세부 정보** 창을 확장해야 합니다.
+ **사용 AWS SDK for .NET**

  이에 대한 자세한 내용은 해당 주제의 끝 부분에 있는 [추가 고려 사항](run-instance.md#run-instance-additional)을 포함하여 [Amazon EC2 인스턴스 시작](run-instance.md)을 참조하세요.

IAM 역할이 연결된 EC2 인스턴스를 시작하려면 IAM 사용자의 구성에 특정 권한이 포함되어야 합니다. 필요한 권한에 대한 자세한 내용은 [Amazon EC2 사용 설명서](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/)의 [IAM 역할을 인스턴스에 전달할 수 있는 사용자 권한 부여](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#permission-to-pass-iam-roles)를 참조하세요.

## EC2 인스턴스에 연결
<a name="net-dg-hosm-connect"></a>

EC2 인스턴스에 연결하면 샘플 애플리케이션을 전송한 다음 애플리케이션을 실행할 수 있습니다. 인스턴스를 시작하는 데 사용한 키 페어의 비공개 부분이 포함된 파일, 즉 PEM 파일이 필요합니다.

인스턴스에 연결하는 방법에 대한 자세한 내용은 [Amazon EC2 사용 설명서](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/)의 [Linux 인스턴스에 연결](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connect-to-linux-instance.html) 또는 [Windows 인스턴스에 연결](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/connecting_to_windows_instance.html)을 참조하세요. 연결할 때는 개발 머신에서 인스턴스로 파일을 전송할 수 있는 방식으로 연결합니다.

Windows에서 Visual Studio를 사용하는 경우, Visual Studio용 도구 키트를 사용하여 인스턴스에 연결할 수도 있습니다. 자세한 내용은 AWS Toolkit for Visual Studio 사용 설명서의 [ Amazon EC2 인스턴스에 연결을](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/tkv-ec2-ami.html#connect-ec2) 참조하세요.

## EC2 인스턴스에서 샘플 애플리케이션 실행
<a name="net-dg-hosm-run-the-app"></a>

1. 애플리케이션 파일을 로컬 드라이브에서 인스턴스로 복사합니다.

   전송하는 파일은 애플리케이션을 빌드한 방법과 인스턴스에 .NET Core 런타임이 설치되어 있는지 여부에 따라 달라집니다. 인스턴스로 파일을 전송하는 방법에 대한 자세한 내용은 [Amazon EC2 사용 설명서](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/)의 [Linux 인스턴스에 연결](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connect-to-linux-instance.html)(해당 하위 섹션 참조) 또는 [Windows 인스턴스로 파일 전송](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connect-to-linux-instanceWindowsFileTransfer.html)을 참조하세요.

1. 애플리케이션을 시작하고 개발 머신에서와 동일한 결과로 실행되는지 확인합니다.

1. 애플리케이션이 IAM 역할에서 제공하는 보안 인증을 사용하는지 확인합니다.

   1. [Amazon EC2 콘솔](https://console.aws.amazon.com/ec2/)을 엽니다.

   1. 인스턴스를 선택하고 **작업**, **인스턴스 설정**, **IAM 역할 연결/바꾸기**를 통해 IAM 역할을 분리합니다.

   1. 애플리케이션을 다시 실행하여 권한 부여 오류가 반환되는지 확인합니다.

## 정리
<a name="net-dg-hosm-cleanup"></a>

이 자습서를 마치고 생성한 EC2 인스턴스를 더 이상 사용하지 않으려면 인스턴스를 종료하여 원치 않는 비용이 발생하지 않도록 하십시오. [Amazon EC2 콘솔](https://console.aws.amazon.com/ec2/)에서 또는 [Amazon EC2 인스턴스 종료](terminate-instance.md)에 설명된 대로 프로그래밍 방식으로 이 작업을 수행할 수 있습니다. 필요에 따라 이 자습서를 위해 생성한 다른 리소스도 삭제할 수 있습니다. 여기에는 IAM 역할, EC2 키 페어 및 PEM 파일, 보안 그룹 등이 포함될 수 있습니다.