Unable to get IAM security credentials from EC2 の対処方法

EC2 で DynamoDB のテーブルを参照するアプリを開発していると、以下のようなエラーメッセージに遭遇しました。

warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100]
      Unhandled exception rendering component: Unable to get IAM security credentials from EC2 Instance Metadata Service.
      Amazon.Runtime.AmazonServiceException: Unable to get IAM security credentials from EC2 Instance Metadata Service.
         at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.FetchCredentials()
         at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentials()
         at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentialsAsync()
         at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext)

原因

このエラーの原因は、EC2インスタンスからAWSのサービスを呼び出す際、IAMを指定していないところにあります。

解決方法

解決方法としては、AccessKeyとSecretKeyを指定して、AWS サービスを呼び出します。

以下のコードは、DynamoDBClientを生成しています。。


string AccessKey = "xxxxxxxxxx";
string SecretKey = "yyyyyyyyyy";

AmazonDynamoDBClient client = new AmazonDynamoDBClient(AccessKey, SecretKey);

コメント

タイトルとURLをコピーしました