title: Problem using DynamoDB for Session State handling in a containerised (Fargate/ECS) .NET ASP web app - role authentication
body: <p>I'm migrating a .NET Framework web app to AWS, and switching to containers. It's old tech, but we're taking baby steps towards modernizing the platform. For step is introducing better InfraCode, CI/CD automation, and resilience/HA, so moving to Terraform driven IaC and containerization with load balancing makes a lot of sense.</p>
  <p>First thing to tackle is that session state started failing the moment we put our web portal (.NET ASP) behind an ALB load balancer. I figure using the default 'in memory' state and turning on session stickiness is a suboptimal path to go down (and it wasn't working with a brief attempt anyway) so we've jumped into trying to use DynamoDB for managing state.</p>
  <p>We're following this guide:</p>
  <p><a href="https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/dynamodb-session-net-sdk.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/dynamodb-session-net-sdk.html</a></p>
  <p>The ECS Task role has the rights defined for accessing the DynamoDB table, which has been pre-created with the expected default table name.</p>
  <p>The site failed to load complaining about the configuration in web.config when it loads up.</p>
  <p>&quot; Unable to get IAM security credentials from EC2 Instance Metadata Service. &quot;</p>
  <p>I looked into how the AWS SDK for .NET tries to authenticate, how it cycles through options starting off with looking for a local AWS Profile and Keys (including env vars for key and secret access key, and it looks like it should look for an ECS Task role, and then a EC2 Role (instance profile). Steps 7/8 in the page below:</p>
  <p><a href="https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/creds-assign.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/creds-assign.html</a></p>
  <p>We're not going to go down the path of using keys in production, so the AWSProfile and ProfilePath config are not added to the provider config. Our web.config section looks like this:</p>
  <pre><code>&lt;sessionState timeout=&quot;10&quot; mode=&quot;Custom&quot; customProvider=&quot;DynamoDBSessionStoreProvider&quot; xdt:Transform=&quot;Replace&quot;&gt; 
  &lt;providers&gt;
      &lt;add name=&quot;DynamoDBSessionStoreProvider&quot;
           type=&quot;Amazon.SessionProvider.DynamoDBSessionStateStore&quot;
           Region=&quot;ap-southeast-2&quot; /&gt;   
  &lt;/providers&gt; 
  &lt;/sessionState&gt;
  </code></pre>
  <p>My error talks about the EC2 metadata service instead of ECS (do they use the same metadata service??) - could this just be that the ECS task can't reach the meta data service to find the role to use?</p>
  <p>Does anyone have experience of using this provider in Windows containers with an ECS role? Has anyone seen this error? If anyone out there has any insights I'd be really grateful to hear them!</p>
  <p>Thank you!</p>