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")}");
        }
 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));
 }
 public static TimeSpan ReadTimeSpan(this LambdaConfig config, string key, TimeSpan defaultValue, Action <TimeSpan> validate = null)
 => config.Read(key, fallback: _ => defaultValue, convert: value => TimeSpan.FromSeconds(float.Parse(value)), validate: validate);
 public static int ReadInt(this LambdaConfig config, string key, int defaultValue, Action <int> validate = null)
 => config.Read(key, fallback: _ => defaultValue, convert: int.Parse, validate: validate);
 public static string ReadText(this LambdaConfig config, string key, string defaultValue, Action <string> validate = null)
 => config.Read(key, fallback: _ => defaultValue, convert: v => v, validate: validate);
Example #6
0
 //--- Abstract Methods ---
 public abstract Task InitializeAsync(LambdaConfig config);
Example #7
0
        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()}'");
                        }
                    }
                }
            }
        }