I have a simple test WebAPI that reads/writes to a DynamoDB table.
When running on my local machine, I load the SSO credentials, assume a role, and execute the read/write requests to the Db. This all works fine.
var roleArnToAssume = "arn:aws:iam::<redacted>:role/myRole_AppDynamoDbAccess";
var stsClient = new AmazonSecurityTokenServiceClient(LoadSsoCredentials(), RegionEndpoint.EUWest1);
var assumeRoleReq = new AssumeRoleRequest()
{
DurationSeconds = 3600,
RoleSessionName = "Session1",
RoleArn = roleArnToAssume
};
var assumeRoleRes = await stsClient.AssumeRoleAsync(assumeRoleReq);
var config = new AmazonDynamoDBConfig()
{
RegionEndpoint = RegionEndpoint.USEast1,
AllowAutoRedirect = true
};
var client = new AmazonDynamoDBClient(assumeRoleRes.Credentials, config);
services.AddSingleton<IAmazonDynamoDB>(client);
services.AddScoped<ICustomerRepository, CustomerRepository>();
Now I need to move this to run on an EC2 instance, where of course I don’t have a local AWS profile. I have already set-up my EC2 instance with the role above.
But I can’t find the right information on how I need to adapt my code to run on EC2.
Ideally, I need the code to handle both scenarios.
Any help and guidance would be appreciated.