Exemplo n.º 1
0
        /// <summary>
        /// This method takes the response from a HTTP end-point and save to S3.
        /// </summary>
        public async Task <string> FunctionHandler(CloudWatchLogsEvent evnt, ILambdaContext context)
        {
            context.Logger.LogLine("Starting...");
            try
            {
                var httpClient = new HttpClient();
                context.Logger.LogLine("Downloading remote data...");
                var response = await httpClient.GetByteArrayAsync(Environment.GetEnvironmentVariable("HTTP_ENDPOINT"));

                context.Logger.LogLine("Uploading to S3...");
                await S3Client.PutObjectAsync(new PutObjectRequest()
                {
                    InputStream             = new MemoryStream(response),
                    AutoCloseStream         = true,
                    AutoResetStreamPosition = true,
                    BucketName = Environment.GetEnvironmentVariable("BUCKET_NAME"),
                    Key        = Environment.GetEnvironmentVariable("KEY_PREFIX") + DateTime.Now.Ticks
                });

                context.Logger.LogLine("Done");
                return("Done");
            }
            catch (Exception e)
            {
                context.Logger.LogLine(e.Message);
                context.Logger.LogLine(e.StackTrace);
                throw;
            }
        }
        public async void TestErrorNotificationFunction()
        {
            var function = new Function();
            var context  = new TestLambdaContext();

            var evnt = new CloudWatchLogsEvent()
            {
                Awslogs = new CloudWatchLogsEvent.Log
                {
                    EncodedData = "H4sIAAAAAAAAAHWPwQqCQBCGX0Xm7EFtK+smZBEUgXoLCdMhFtKV3akI8d0bLYmibvPPN3wz00CJxmQnTO41whwWQRIctmEcB6sQbFC3CjW3XW8kxpOpP+OC22d1Wml1qZkQGtoMsScxaczKN3plG8zlaHIta5KqWsozoTYw3/djzwhpLwivWFGHGpAFe7DL68JlBUk+l7KSN7tCOEJ4M3/qOI49vMHj+zCKdlFqLaU2ZHV2a4Ct/an0/ivdX8oYc1UVX860fQDQiMdxRQEAAA=="
                }
            };

            var notification = await function.FunctionHandler(evnt, context);

            Assert.True(notification);
        }
Exemplo n.º 3
0
        public void Handler(CloudWatchLogsEvent cloudWatchLogsEvent, ILambdaContext context)
        {
            // Level 1: decode and decompress data
            var logsData = cloudWatchLogsEvent.AwsLogs.Data;

            Console.WriteLine($"THIS IS THE DATA: {logsData}");
            var decompressedData = DecompressLogData(logsData);

            Console.WriteLine($"THIS IS THE DECODED, UNCOMPRESSED DATA: {decompressedData}");

            // Level 2: Parse log records
            var athenaFriendlyJson = ParseLog(decompressedData);

            // Level 3: Save data to S3
            PutObject(athenaFriendlyJson);

            // Level 4: Create athena schema to query data
        }
Exemplo n.º 4
0
        public void Handler(CloudWatchLogsEvent cloudWatchLogsEvent, ILambdaContext context)
        {
            // Level 1: decode and decompress data
            Console.WriteLine($"THIS IS THE DATA: {cloudWatchLogsEvent.AwsLogs.Data}");
            var data = DecompressLogData(cloudWatchLogsEvent.AwsLogs.Data);

            Console.WriteLine($"THIS IS THE DECODED, UNCOMPRESSED DATA: {data}");

            // Level 2: Frame and filter log records
            var events       = JsonConvert.DeserializeObject <DecompressedEvents>(data).LogEvents;
            var framedEvents = events.Select(x => x.Message.Split('\u03BB').ToList()).ToList();
            var userJson     = UserJson(framedEvents);
            var tweetJson    = TweetJson(framedEvents);

            // Level 3: Save data to S3
            PutObject(userJson, "users");
            PutObject(tweetJson, "tweet-info");

            // Level 4: Create athena schema to query data
        }
Exemplo n.º 5
0
        /// <summary>
        /// Notify contents waritten out on CloudWatch Logs
        /// </summary>
        /// <param name="logEvent"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <bool> FunctionHandler(CloudWatchLogsEvent logEvent, ILambdaContext context)
        {
            var slackWebhookUrl       = Environment.GetEnvironmentVariable("SLACK_WEBHOOK_URL");
            var cloudWatchLogGroupUrl = Environment.GetEnvironmentVariable("CLOUDWATCH_LOG_GROUP_URL");
            var cloudWatchMetricsUrl  = Environment.GetEnvironmentVariable("CLOUDWATCH_METRICS_URL");

            var payload = new
            {
                channel  = "dev",
                username = "******",
                text     = $"{GetMessage(logEvent.Awslogs.DecodeData())}" + Environment.NewLine +
                           $"Logs: <{cloudWatchLogGroupUrl}|Click here>" + Environment.NewLine +
                           $"Metrics: <{cloudWatchMetricsUrl}|Click here>",
            };

            var jsonString = JsonConvert.SerializeObject(payload);

            var content = new FormUrlEncodedContent(new Dictionary <string, string>
            {
                { "payload", jsonString }
            });

            try
            {
                using (var client = new HttpClient())
                {
                    await client.PostAsync(slackWebhookUrl, content);
                }
            }
            catch (Exception e)
            {
                context.Logger.LogLine("error!!!!" + Environment.NewLine + $"{e.Message}" + Environment.NewLine + $"{e.StackTrace}");
                throw;
            }

            return(true);
        }
Exemplo n.º 6
0
 // lambda entry point
 public static Task <StringResponse> Execute(CloudWatchLogsEvent e) =>
 logCloudwatchLog.Execute(e);
Exemplo n.º 7
0
 public string FunctionHandler(CloudWatchLogsEvent inputData)
 {
     return(inputData.Awslogs.DecodeData());
 }
Exemplo n.º 8
0
 /// <summary>
 /// To use this handler to respond to an AWS event, reference the appropriate package from
 /// https://github.com/aws/aws-lambda-dotnet#events
 /// </summary>
 /// <returns></returns>
 public static async Task FunctionHandler(CloudWatchLogsEvent cloudWatchLogsEvent)
 {
     var updateReleasesService = _serviceProvider.GetService <IUpdateReleasesService>();
     await updateReleasesService.UpdateAsync();
 }