protected override async Task InitializeAsync(ILambdaConfigSource envSource, ILambdaContext context) { await base.InitializeAsync(envSource, context); // retrieve the optional parameter name for the slack token _slackVerificationTokenName = envSource.Read("SLACKTOKENNAME"); }
//--- Constructors --- /// <summary> /// Create new instance of <see cref="LambdaFunctionDependencyProvider"/>, which provides the implementation for the required dependencies for <see cref="ALambdaFunction"/>. /// </summary> /// <param name="utcNowCallback">A function that return the current <c>DateTime</c> in UTC timezone. Defaults to <see cref="DateTime.UtcNow"/> when <c>null</c>.</param> /// <param name="logCallback">An action that logs a string message. Defaults to <see cref="LambdaLogger.Log"/> when <c>null</c>.</param> /// <param name="configSource">A <see cref="ILambdaConfigSource"/> instance from which the Lambda function configuration is read. Defaults to <see cref="LambdaSystemEnvironmentSource"/> instance when <c>null</c>.</param> /// <param name="jsonSerializer">A <see cref="ILambdaSerializer"/> instance for serializing and deserializing JSON data. Defaults to <see cref="LambdaLogger.Log"/> when <c>null</c>.</param> /// <param name="kmsClient">A <see cref="IAmazonKeyManagementService"/> client instance. Defaults to <see cref="AmazonKeyManagementServiceClient"/> when <c>null</c>.</param> /// <param name="sqsClient">A <see cref="IAmazonSQS"/> client instance. Defaults to <see cref="AmazonSQSClient"/> when <c>null</c>.</param> /// <param name="eventsClient">A <see cref="IAmazonCloudWatchEvents"/> client instance. Defaults to <see cref="AmazonCloudWatchEventsClient"/> when <c>null</c>.</param> /// <param name="debugLoggingEnabled">A boolean indicating if debug logging is enabled.</param> public LambdaFunctionDependencyProvider( Func <DateTime> utcNowCallback = null, Action <string> logCallback = null, ILambdaConfigSource configSource = null, ILambdaSerializer jsonSerializer = null, IAmazonKeyManagementService kmsClient = null, IAmazonSQS sqsClient = null, IAmazonCloudWatchEvents eventsClient = null, bool?debugLoggingEnabled = null ) { _nowCallback = utcNowCallback ?? (() => DateTime.UtcNow); _logCallback = logCallback ?? LambdaLogger.Log; ConfigSource = configSource ?? new LambdaSystemEnvironmentSource(); JsonSerializer = jsonSerializer ?? new LambdaJsonSerializer(); KmsClient = kmsClient ?? new AmazonKeyManagementServiceClient(); SqsClient = sqsClient ?? new AmazonSQSClient(); EventsClient = eventsClient ?? new AmazonCloudWatchEventsClient(); // determine if debug logging is enabled if (debugLoggingEnabled.HasValue) { _debugLoggingEnabled = debugLoggingEnabled.Value; } else { // read environment variable to determine if request/response messages should be serialized to the log for debugging purposes var value = System.Environment.GetEnvironmentVariable("DEBUG_LOGGING_ENABLED") ?? "false"; _debugLoggingEnabled = value.Equals("true", StringComparison.OrdinalIgnoreCase); } }
protected ALambdaFunction(LambdaFunctionConfiguration configuration) { _now = configuration.UtcNow ?? (() => DateTime.UtcNow); _kmsClient = configuration.KmsClient ?? throw new ArgumentNullException(nameof(configuration.KmsClient)); _snsClient = configuration.SnsClient ?? throw new ArgumentNullException(nameof(configuration.SnsClient)); _sqsClient = configuration.SqsClient ?? throw new ArgumentNullException(nameof(configuration.SqsClient)); _envSource = configuration.EnvironmentSource ?? throw new ArgumentNullException(nameof(configuration.EnvironmentSource)); _started = UtcNow; }
//--- Constructors --- /// <summary> /// Creates new instance of <see cref="LambdaQueueFunctionDependencyProvider"/>, which provides the implementation for the required dependencies for <see cref="ALambdaQueueFunction{TMessage}"/>. /// </summary> /// <param name="utcNowCallback">A function that return the current <c>DateTime</c> in UTC timezone. Defaults to <see cref="DateTime.UtcNow"/> when <c>null</c>.</param> /// <param name="logCallback">An action that logs a string message. Defaults to <see cref="LambdaLogger.Log"/> when <c>null</c>.</param> /// <param name="configSource">A <see cref="ILambdaConfigSource"/> instance from which the Lambda function configuration is read. Defaults to <see cref="LambdaSystemEnvironmentSource"/> instance when <c>null</c>.</param> /// <param name="jsonSerializer">A <see cref="ILambdaSerializer"/> instance for serializing and deserializing JSON data. Defaults to <see cref="LambdaLogger.Log"/> when <c>null</c>.</param> /// <param name="kmsClient">A <see cref="IAmazonKeyManagementService"/> client instance. Defaults to <see cref="AmazonKeyManagementServiceClient"/> when <c>null</c>.</param> /// <param name="sqsClient">A <see cref="IAmazonSQS"/> client instance. Defaults to <see cref="AmazonSQSClient"/> when <c>null</c>.</param> public LambdaQueueFunctionDependencyProvider( Func <DateTime> utcNowCallback = null, Action <string> logCallback = null, ILambdaConfigSource configSource = null, ILambdaSerializer jsonSerializer = null, IAmazonKeyManagementService kmsClient = null, IAmazonSQS sqsClient = null ) : base(utcNowCallback, logCallback, configSource, jsonSerializer, kmsClient, sqsClient) { }
//--- Constructors --- /// <summary> /// Create new instance of <see cref="LambdaFunctionDependencyProvider"/>, which provides the implementation for the required dependencies for <see cref="ALambdaFunction"/>. /// </summary> /// <param name="utcNowCallback">A function that return the current <c>DateTime</c> in UTC timezone. Defaults to <see cref="DateTime.UtcNow"/> when <c>null</c>.</param> /// <param name="logCallback">An action that logs a string message. Defaults to <see cref="LambdaLogger.Log"/> when <c>null</c>.</param> /// <param name="configSource">A <see cref="ILambdaConfigSource"/> instance from which the Lambda function configuration is read. Defaults to <see cref="LambdaSystemEnvironmentSource"/> instance when <c>null</c>.</param> /// <param name="jsonSerializer">A <see cref="ILambdaSerializer"/> instance for serializing and deserializing JSON data. Defaults to <see cref="LambdaLogger.Log"/> when <c>null</c>.</param> /// <param name="kmsClient">A <see cref="IAmazonKeyManagementService"/> client instance. Defaults to <see cref="AmazonKeyManagementServiceClient"/> when <c>null</c>.</param> /// <param name="sqsClient">A <see cref="IAmazonSQS"/> client instance. Defaults to <see cref="AmazonSQSClient"/> when <c>null</c>.</param> public LambdaFunctionDependencyProvider( Func <DateTime> utcNowCallback = null, Action <string> logCallback = null, ILambdaConfigSource configSource = null, ILambdaSerializer jsonSerializer = null, IAmazonKeyManagementService kmsClient = null, IAmazonSQS sqsClient = null ) { _nowCallback = utcNowCallback ?? (() => DateTime.UtcNow); _logCallback = logCallback ?? LambdaLogger.Log; ConfigSource = configSource ?? new LambdaSystemEnvironmentSource(); JsonSerializer = jsonSerializer ?? new JsonSerializer(); KmsClient = kmsClient ?? new AmazonKeyManagementServiceClient(); SqsClient = sqsClient ?? new AmazonSQSClient(); }
protected virtual async Task InitializeAsync(ILambdaConfigSource envSource, ILambdaContext context) { // read configuration from environment variables DeploymentTier = envSource.Read("TIER"); ModuleName = envSource.Read("MODULE"); _deadLetterQueueUrl = envSource.Read("DEADLETTERQUEUE"); _loggingTopicArn = envSource.Read("LOGGINGTOPIC"); var framework = envSource.Read("LAMBDARUNTIME"); LogInfo($"TIER = {DeploymentTier}"); LogInfo($"MODULE = {ModuleName}"); LogInfo($"DEADLETTERQUEUE = {_deadLetterQueueUrl ?? "NONE"}"); LogInfo($"LOGGINGTOPIC = {_loggingTopicArn ?? "NONE"}"); // read optional git-sha file var gitsha = File.Exists("gitsha.txt") ? File.ReadAllText("gitsha.txt") : null; LogInfo($"GITSHA = {gitsha ?? "NONE"}"); // convert environment variables to lambda parameters _appConfig = new LambdaConfig(new LambdaDictionarySource(await ReadParametersFromEnvironmentVariables())); // initialize rollbar var rollbarAccessToken = _appConfig.ReadText("RollbarToken", defaultValue: null); const string proxy = ""; const string platform = "lambda"; _rollbarClient = RollbarClient.Create(new RollbarConfiguration( // NOTE (2018-08-06, bjorg): the rollbar access token determines the rollbar project // the error report is associated with; when rollbar intergration is disabled, // use the module name instead so the logging recipient can determine the module // the log entry belongs to. rollbarAccessToken ?? ModuleName, proxy, DeploymentTier, platform, framework, gitsha )); _rollbarEnabled = (rollbarAccessToken != null); LogInfo($"ROLLBAR = {(_rollbarEnabled ? "ENABLED" : "DISABLED")}"); }
//--- Constructors --- /// <summary> /// Create a new instance using the provided <see cref="ILambdaConfigSource"/> instance to read configuration values. /// </summary> /// <param name="source">The <see cref="ILambdaConfigSource"/> instance to read configuration values from.</param> /// <returns>A new <see cref="LambdaConfig"/> instance.</returns> /// <exception cref="ArgumentNullException"> /// Thrown when <paramref name="source"/> is null. /// </exception> public LambdaConfig(ILambdaConfigSource source) => _source = source ?? throw new ArgumentNullException(nameof(source));
private LambdaConfig(LambdaConfig parent, string key, ILambdaConfigSource source) { _parent = parent ?? throw new ArgumentNullException(nameof(parent)); _key = key ?? throw new ArgumentNullException(nameof(key)); _source = source ?? throw new ArgumentNullException(nameof(source)); }
//--- Methods --- /// <summary> /// The <see cref="InitializePrologueAsync(ILambdaConfigSource)"/> method is invoked to prepare the Lambda function /// for initialization. This is the first of three methods that are invoked to initialize the Lambda function. /// </summary> /// <param name="envSource">The <see cref="ILambdaConfigSource"/> instance from which to read the configuration settings.</param> /// <returns>The task object representing the asynchronous operation.</returns> protected override async Task InitializePrologueAsync(ILambdaConfigSource envSource) { await base.InitializePrologueAsync(envSource); _corsOrigin = envSource.Read("CORS_ORIGIN"); }
protected virtual async Task InitializeAsync(ILambdaConfigSource envSource, ILambdaContext context) { // read configuration from environment variables DeploymentTier = envSource.Read("TIER"); ModuleName = envSource.Read("MODULE"); _deadLetterQueueUrl = envSource.Read("DEADLETTERQUEUE"); _loggingTopicArn = envSource.Read("LOGGINGTOPIC"); var framework = envSource.Read("LAMBDARUNTIME"); LogInfo($"TIER = {DeploymentTier}"); LogInfo($"MODULE = {ModuleName}"); LogInfo($"DEADLETTERQUEUE = {_deadLetterQueueUrl ?? "NONE"}"); LogInfo($"LOGGINGTOPIC = {_loggingTopicArn ?? "NONE"}"); // read optional git-sha file var gitsha = File.Exists("gitsha.txt") ? File.ReadAllText("gitsha.txt") : null; LogInfo($"GITSHA = {gitsha ?? "NONE"}"); // read module parameter values from parameters file var parameters = await ParseParameters("/", File.ReadAllText("parameters.json")); // create config where environment variables take precedence over those found in the parameter file _appConfig = new LambdaConfig(new LambdaMultiSource(new[] { envSource, new LambdaDictionarySource("", parameters) })); // initialize rollbar var rollbarAccessToken = _appConfig.ReadText("RollbarToken", defaultValue: null); const string proxy = ""; const string platform = "lambda"; _rollbarClient = RollbarClient.Create(new RollbarConfiguration( // NOTE (2018-08-06, bjorg): the rollbar access token determines the rollbar project // the error report is associated with; when rollbar intergration is disabled, // use the module name instead so the logging recipient can determine the module // the log entry belongs to. rollbarAccessToken ?? ModuleName, proxy, DeploymentTier, platform, framework, gitsha )); _rollbarEnabled = (rollbarAccessToken != null); LogInfo($"ROLLBAR = {(_rollbarEnabled ? "ENABLED" : "DISABLED")}"); // local functions async Task <Dictionary <string, string> > ParseParameters(string parameterPrefix, string json) { var functionParameters = JsonConvert.DeserializeObject <Dictionary <string, LambdaFunctionParameter> >(json); var flatten = new Dictionary <string, string>(); await Flatten(functionParameters, parameterPrefix, "STACK_", flatten); return(flatten); // local functions async Task Flatten(Dictionary <string, LambdaFunctionParameter> source, string prefix, string envPrefix, Dictionary <string, string> target) { foreach (var kv in source) { var value = kv.Value.Value; switch (kv.Value.Type) { case LambdaFunctionParameterType.Collection: await Flatten((Dictionary <string, LambdaFunctionParameter>) value, prefix + kv.Key + "/", envPrefix + kv.Key.ToUpperInvariant() + "_", target); break; case LambdaFunctionParameterType.Secret: { var secret = (string)value; var plaintextStream = (await _kmsClient.DecryptAsync(new DecryptRequest { CiphertextBlob = new MemoryStream(Convert.FromBase64String(secret)), EncryptionContext = kv.Value.EncryptionContext })).Plaintext; target.Add(prefix + kv.Key, Encoding.UTF8.GetString(plaintextStream.ToArray())); break; } case LambdaFunctionParameterType.Stack: target.Add(prefix + kv.Key, envSource.Read(envPrefix + kv.Key.ToUpperInvariant())); break; case LambdaFunctionParameterType.Text: target.Add(prefix + kv.Key, (string)value); break; default: throw new NotSupportedException($"unsupported parameter type: '{kv.Value.Type.ToString()}'"); } } } } }