I have created my first Azure Functions application. From here I call a DynamoDB endpoint. The code I have written works on my local machine, but when deployed returns an exception with the message:
Request is missing Authentication Token
I have checked that appSettings.json
are correct after the deployment has completed but I cant think of any other reasons why the Auth token would not be added, is this something azure specific or have I created really brittle code that will only work on under specific conditions?
appsetting.json
{
"AWS": {
"AccessKey": "my_access_key",
"SecretKey": "my_secret_key",
"AWSRegion": "eu-west-2"
}
}
relevant code
public class Startup : FunctionsStartup
{
private IConfigurationRoot _configuration;
public Startup()
{
_configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
}
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
builder.Services.AddScoped<IRoadmapService, RoadmapService>();
builder.Services.AddRetroArcadeMachinesDataRead(_configuration);
builder.Services.AddAutoMapper(typeof(Startup), typeof(MappingConfiguration));
}
}
public static class StartupExtensions
{
public static IServiceCollection AddRetroArcadeMachinesDataRead(this IServiceCollection services, IConfigurationRoot configuration)
{
var awsOptions = GetAWSOptions(configuration);
services.AddDefaultAWSOptions(awsOptions);
services.AddAWSService<IAmazonDynamoDB>();
services.AddTransient<IDynamoDBContext, DynamoDBContext>();
services.AddSingleton<IRoadmapRepository, DynamoDbRoadmapRepository>();
return services;
}
private static AWSOptions GetAWSOptions(IConfigurationRoot configuration)
{
var awsOptions = configuration.GetAWSOptions();
var accessKey = configuration.GetValue<string>("AWS:AccessKey");
var secretKey = configuration.GetValue<string>("AWS:SecretKey");
awsOptions.Credentials = new BasicAWSCredentials(accessKey, secretKey);
awsOptions.Region = RegionEndpoint.EUWest2;
return awsOptions;
}
private static AWSOptions GetAWSOptionsAlternateWayThatGivesTheSameResult(IConfigurationRoot configuration)
{
var netSDKFile = new NetSDKCredentialsFile();
var profile = new CredentialProfile("basic_profile", new CredentialProfileOptions
{
AccessKey = configuration.GetValue<string>("AWS:AccessKey"),
SecretKey = configuration.GetValue<string>("AWS:SecretKey")
});
profile.Region = RegionEndpoint.EUWest2;
netSDKFile.RegisterProfile(profile);
var awsOptions = configuration.GetAWSOptions();
var isProfileExists = netSDKFile.TryGetProfile("basic_profile", out var credentialProfile);
if (isProfileExists)
{
awsOptions.Credentials =
new BasicAWSCredentials(credentialProfile.Options.AccessKey, credentialProfile.Options.SecretKey);
awsOptions.Region = RegionEndpoint.EUWest2;
}
return awsOptions;
}
}
public class DynamoDbRoadmapRepository : IRoadmapRepository
{
private readonly IAmazonDynamoDB _dynamoDBClient;
private readonly IDynamoDBContext _context;
public DynamoDbRoadmapRepository(IAmazonDynamoDB dynamoDBClient, IDynamoDBContext context)
{
_dynamoDBClient = dynamoDBClient;
_context = context;
}
public async Task<IEnumerable<RoadmapItemModel>> Get()
{
return await _context.ScanAsync<RoadmapItemModel>(new List<ScanCondition>()).GetRemainingAsync();
}
}