%3CLINGO-SUB%20id%3D%22lingo-sub-1571136%22%20slang%3D%22en-US%22%3EIngesting%20log%20files%20from%20AWS%20S3%20using%20AWS%20Lambda%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1571136%22%20slang%3D%22en-US%22%3E%3CH2%20id%3D%22toc-hId--1295364878%22%20id%3D%22toc-hId--1295364878%22%3EDo%20you%20have%20data%20going%20into%20S3%20Buckets%20as%20files%20that%20you%20want%20to%20ingest%20into%20Azure%20Sentinel%3F%26nbsp%3B%3C%2FH2%3E%0A%3CP%3EIn%20this%20blog%2C%20I%20will%20show%20you%20how%20to%20create%20an%20AWS%20Lambda%20function%20running%20PowerShell%20to%20ingest%20the%20data%20into%20Azure%20Sentinel.%26nbsp%3B%20The%20reason%20you%20might%20need%20or%20want%20to%20do%20this%20is%20AWS%20Lamdba%20allows%20for%20a%20trigger%20on%20S3%20CreateObject.%26nbsp%3B%20The%20function%20can%20automatically%20be%20called%20every%20time%20a%20new%20file%20is%20added%20to%20the%20bucket.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22lia-spoiler-container%22%3E%3CA%20class%3D%22lia-spoiler-link%22%20href%3D%22%23%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3ESpoiler%3C%2FA%3E%3CNOSCRIPT%3E(Highlight%20to%20read)%3C%2FNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-border%22%3E%3CDIV%20class%3D%22lia-spoiler-content%22%3EIf%20you%20want%20to%20get%20right%20to%20the%20code.%26nbsp%3B%20go%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2FAzure-Sentinel%2Ftree%2Fmaster%2FDataConnectors%2FS3-Lambda%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FDIV%3E%3CNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-noscript-container%22%3E%3CDIV%20class%3D%22lia-spoiler-noscript-content%22%3EIf%20you%20want%20to%20get%20right%20to%20the%20code.%26nbsp%3B%20go%26nbsp%3Bhere.%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FNOSCRIPT%3E%3C%2FDIV%3E%3C%2FDIV%3E%0A%3CH3%20id%3D%22toc-hId--604803404%22%20id%3D%22toc-hId--604803404%22%3ESetup%3C%2FH3%3E%0A%3CP%3ETo%20deploy%20this%2C%20you%20will%20need%20a%20machine%20prepared%20with%20the%20following%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EPowerShell%20Core%20%E2%80%93%20I%20recommend%20PowerShell%207%20found%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FPowerShell%2FPowerShell%2Freleases%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E.Net%20Core%203.1%20SDK%20found%20%3CA%20href%3D%22https%3A%2F%2Fdotnet.microsoft.com%2Fdownload%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EAWSLambdaPSCore%20module%20%E2%80%93%20You%20can%20install%20this%20either%20from%20the%20PowerShell%20Gallery%2C%20or%20you%20can%20install%20it%20by%20using%20the%20following%20PowerShell%20Core%20shell%20command%3A%3CUL%3E%0A%3CLI%3EInstall-Module%20AWSLambdaPSCore%20-Scope%20CurrentUser%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3ESee%20the%20documentation%20here%20%3CA%20href%3D%22https%3A%2F%2Fdocs.aws.amazon.com%2Flambda%2Flatest%2Fdg%2Fpowershell-devenv.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.aws.amazon.com%2Flambda%2Flatest%2Fdg%2Fpowershell-devenv.html%3C%2FA%3E%3C%2FP%3E%0A%3CP%3EI%20recommend%20you%20review%20%3CA%20href%3D%22https%3A%2F%2Fdocs.aws.amazon.com%2Flambda%2Flatest%2Fdg%2Fpowershell-package.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.aws.amazon.com%2Flambda%2Flatest%2Fdg%2Fpowershell-package.html%3C%2FA%3E%20to%20review%20the%20cmdlets%20that%20are%20part%20of%20AWSLambdaPSCore.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1882709429%22%20id%3D%22toc-hId-1882709429%22%3EReview%20of%20the%20Code%3C%2FH3%3E%0A%3CP%3EOur%20script%20will%20be%20triggered%20when%20a%20log%20file%20is%20created%20in%20an%20S3%20bucket.%20To%20create%20the%20base%20script%2C%20run%20the%20following%20PowerShell%20core%20command%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CSPAN%3ENew-AWSPowerShellLambda%20-Template%20S3Event%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20default%20template%20for%20and%20S3Event%20trigger%20is%20below%3A%3C%2FP%3E%0A%3CPRE%3E%23%20PowerShell%20script%20file%20to%20be%20executed%20as%20a%20AWS%20Lambda%20function.%3CBR%20%2F%3E%23%3CBR%20%2F%3E%23%20When%20executing%20in%20Lambda%20the%20following%20variables%20will%20be%20predefined.%3CBR%20%2F%3E%23%20%24LambdaInput%20-%20A%20PSObject%20that%20contains%20the%20Lambda%20function%20input%20data.%3CBR%20%2F%3E%23%20%24LambdaContext%20-%20An%20Amazon.Lambda.Core.ILambdaContext%20object%20that%20contains%20information%20about%20the%20currently%20running%20Lambda%20environment.%3CBR%20%2F%3E%23%3CBR%20%2F%3E%23%20The%20last%20item%20in%20the%20PowerShell%20pipeline%20will%20be%20returned%20as%20the%20result%20of%20the%20Lambda%20function.%3CBR%20%2F%3E%23%3CBR%20%2F%3E%23%20To%20include%20PowerShell%20modules%20with%20your%20Lambda%20function%2C%20like%20the%20AWS.Tools.S3%20module%2C%20add%20a%20%22%23Requires%22%20statement%3CBR%20%2F%3E%23%20indicating%20the%20module%20and%20version.%20If%20using%20an%20AWS.Tools.*%20module%20the%20AWS.Tools.Common%20module%20is%20also%20required.%3CBR%20%2F%3E%23%3CBR%20%2F%3E%23%20The%20following%20link%20contains%20documentation%20describing%20the%20structure%20of%20the%20S3%20event%20object.%3CBR%20%2F%3E%23%20%3CA%20href%3D%22https%3A%2F%2Fdocs.aws.amazon.com%2FAmazonS3%2Flatest%2Fdev%2Fnotification-content-structure.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.aws.amazon.com%2FAmazonS3%2Flatest%2Fdev%2Fnotification-content-structure.html%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%23Requires%20-Modules%20%40%7BModuleName%3D'AWS.Tools.Common'%3BModuleVersion%3D'4.0.6.0'%7D%3CBR%20%2F%3E%23Requires%20-Modules%20%40%7BModuleName%3D'AWS.Tools.S3'%3BModuleVersion%3D'4.0.6.0'%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20Uncomment%20to%20send%20the%20input%20event%20to%20CloudWatch%20Logs%3CBR%20%2F%3E%23%20Write-Host%20(ConvertTo-Json%20-InputObject%20%24LambdaInput%20-Compress%20-Depth%205)%3CBR%20%2F%3E%3CBR%20%2F%3Eforeach%20(%24record%20in%20%24LambdaInput.Records)%20%7B%3CBR%20%2F%3E%20%20%24bucket%20%3D%20%24record.s3.bucket.name%3CBR%20%2F%3E%20%20%24key%20%3D%20%24record.s3.object.key%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20Write-Host%20%22Processing%20event%20for%3A%20bucket%20%3D%20%24bucket%2C%20key%20%3D%20%24key%22%3CBR%20%2F%3E%20%20%3CBR%20%2F%3E%20%20%23%20TODO%3A%20Add%20logic%20to%20handle%20S3%20event%20record%2C%20for%20example%3CBR%20%2F%3E%20%20%24obj%20%3D%20Get-S3Object%20-Bucket%20%24bucket%20-Key%20%24key%3CBR%20%2F%3E%20%20Write-Host%20%22Object%20%24key%20is%20%24(%24obj.Size)%20bytes%22%3CBR%20%2F%3E%7D%3C%2FPRE%3E%0A%3CP%3EAs%20you%20can%20see%20for%20each%20CreateObject%20event%20from%20S3%20the%20default%20script%20pulls%20the%20bucket%20name%20(%3CSTRONG%3E%24bucket%20%3D%20%24record.s3.bucket.name%3C%2FSTRONG%3E)%20and%20filename%20(%3CSTRONG%3E%24key%20%3D%20%24record.s3.object.key%3C%2FSTRONG%3E)%20Then%3CSTRONG%3E%20Write-Host%3C%2FSTRONG%3E%20will%20send%20an%20output%20to%20CloudWatch%20logs.%26nbsp%3B%20Lastly%20it%20gets%20the%20file%20size%20using%20the%20%3CSTRONG%3EGet-S3object%3C%2FSTRONG%3E%20cmdlet.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EI%20used%20this%20basic%20template%20as%20a%20starter%20template.%26nbsp%3B%20First%2C%20I%20added%20a%20function%20called%20%3CSTRONG%3EWrite-OMSLogFile%3C%2FSTRONG%3E%20which%20I%20got%20from%20Travis%20Roberts%20on%20GitHub%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ftsrob50%2FLogAnalyticsAPIFunction%2Fblob%2Fmaster%2FWrite-OMSLogfile.ps1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%26nbsp%3B%20In%20short%2C%20the%20function%20takes%20a%20hash%20table%20with%20the%20log%20data%20and%20a%20few%20parameters%20and%20uploads%20it%20to%20the%20Log%20Analytics%20REST%20API.%26nbsp%3B%20The%20documentation%20on%20this%20API%20is%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Frest%2Fapi%2Floganalytics%2Fcreate-request%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%26nbsp%3B%20The%20function%20needs%20three%20parameters%20you%20will%20need%20to%20provide%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ELine%20158%20-%20%3CSTRONG%3E%24WorkspaceId%3C%2FSTRONG%3E%20%E2%80%93%20you%20will%20need%20to%20enter%20the%20workspace%20id%3C%2FLI%3E%0A%3CLI%3ELine%20159%20-%20%3CSTRONG%3E%24WorkspaceKey%3C%2FSTRONG%3E%20%E2%80%93%20you%20will%20need%20to%20enter%20the%20workspace%20key%3C%2FLI%3E%0A%3CLI%3ELine%20160%20-%3CSTRONG%3E%20%24CustomLogName%3C%2FSTRONG%3E%20%E2%80%93%20you%20will%20need%20to%20enter%20the%20Custom%20Log%20name%20you%20want%20to%20use%20for%20the%20data.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3BAfter%20we%20have%20those%20setup%2C%20the%20first%20action%20we%20need%20to%20do%20is%20download%20the%20file%20from%20S3%20to%20tmp%20storage.%3C%2FP%3E%0A%3CPRE%3EWrite-Host%20%22Downloading%20%24key%20to%20%2Ftmp%2F%24key%22%3CBR%20%2F%3ERead-S3Object%20-BucketName%20%24bucket%20-Key%20%24key%20-File%20%22%2Ftmp%2F%24key%22%3CBR%20%2F%3EWrite-Host%20%22Downloaded%20%24key%20to%20%2Ftmp%2F%24key%22%3C%2FPRE%3E%0A%3CP%3E%3CBR%20%2F%3EOnce%20we%20have%20the%20file%2C%20we%20need%20to%20process%20the%20file%20based%20on%20file%20type.%26nbsp%3B%20I%20have%20made%20some%20simple%20assumptions.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EIf%20its%20.csv%20its%20CSV%20data%20which%20is%20easy%20to%20import%20to%20a%20hash%20table%20using%3CSTRONG%3E%20Import-CSV%3C%2FSTRONG%3E.%3C%2FLI%3E%0A%3CLI%3EIf%20its%20.json%2C%20its%20JSON%20data%20which%20is%20easy%20to%20import%20to%20hash%20table%20using%20%3CSTRONG%3EGet-Content%20%7C%20ConvertFrom-JSON%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3ELastly%2C%20if%20it%E2%80%99s%20a%20.log%20file%20the%20data%20contains%20%3CA%20href%3D%22https%3A%2F%2Fcommunity.microfocus.com%2Ft5%2FArcSight-Connectors%2FArcSight-Common-Event-Format-CEF-Implementation-Standard%2Fta-p%2F1645557%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ECEF%3C%2FA%3E%20formatted%20data%20which%20takes%20some%20special%20processing.%0A%3CUL%3E%0A%3CLI%3EI%20used%20some%20REGEX%20and%20splitting%20to%20break%20apart%20the%20CEF%20Message%20and%20then%20build%20a%20hash%20table%20of%20records.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3EOr%20its%20not%20supported%20in%20this%20function%20yet.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%3E%23Determine%20if%20CSV%20or%20JSON%20or%20whatever%3CBR%20%2F%3E%24FileName%20%3D%20%22%2Ftmp%2F%24key%22%3CBR%20%2F%3Eif%20(%24fileName%20-like%20%22*.csv%22)%20%7B%3CBR%20%2F%3E%20%20Write-Host%20%22Handling%20CSV%20File%22%3CBR%20%2F%3E%20%20%24data%20%3D%20import-csv%20%24filename%3CBR%20%2F%3E%7D%3CBR%20%2F%3Eelseif%20(%24filename%20-like%20%22*.json%22)%20%7B%3CBR%20%2F%3E%20%20Write-Host%20%22Handling%20JSON%20File%22%3CBR%20%2F%3E%20%20%24Data%20%3D%20Get-Content%20%24filename%20%7C%20ConvertFrom-Json%3CBR%20%2F%3E%7D%3CBR%20%2F%3Eelseif%20(%24filename%20-like%20%22*.log%22)%20%7B%3CBR%20%2F%3E%20%20Write-Host%20%22Handling%20Log%20File%22%3CBR%20%2F%3E%20%20%23Assuming%20CEF%20formatted%20logs%3CBR%20%2F%3E%20%20%24cefdata%20%3D%20Get-Content%20%24filename%3CBR%20%2F%3E%20%20%24data%20%3D%20%40()%3CBR%20%2F%3E%20%20%24cefmsg%20%3D%20%40%7B%7D%3CBR%20%2F%3E%20%20foreach%20(%24line%20in%20%24cefdata)%20%7B%3CBR%20%2F%3E%20%20%20%20if%20(%24line%20-like%20%22*CEF%3A*%22)%20%7B%3CBR%20%2F%3E%20%20%20%20%20%20%23Write-Host%20%22Handling%20CEF%20Data%22%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFtimegenerated%20%3D%20(%24line%20-split%20'(%3F%3CTIME%3E(%3F%3A%5Cw%2B%20%2B)%7B2%2C3%7D(%3F%3A%5Cd%2B%3A)%7B2%7D%5Cd%2B%7C%5Cd%7B4%7D-%5Cd%7B2%7D-%5Cd%7B2%7DT%5Cd%7B2%7D%3A%5Cd%7B2%7D%3A%5Cd%7B2%7D.%5B%5Cw%5C-%5C%3A%5C%2B%5D%7B3%2C12%7D)')%5B1%5D%3CBR%20%2F%3E%20%20%20%20%20%20%23%24CEFHost%20%3D%20((%24line%20-split%20'(%3F%3CTIME%3E(%3F%3A%5Cw%2B%20%2B)%7B2%2C3%7D(%3F%3A%5Cd%2B%3A)%7B2%7D%5Cd%2B%7C%5Cd%7B4%7D-%5Cd%7B2%7D-%5Cd%7B2%7DT%5Cd%7B2%7D%3A%5Cd%7B2%7D%3A%5Cd%7B2%7D.%5B%5Cw%5C-%5C%3A%5C%2B%5D%7B3%2C12%7D)')%5B2%5D%20-split%20%22CEF%3A%22)%5B0%5D%3CBR%20%2F%3E%20%20%20%20%20%20%23%24CEFVersion%20%3D%20%24line.Split(%22CEF%3A%20%22).Split(%22%7C%22)%5B1%5D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFDeviceVendor%20%3D%20%24line.split(%22%7C%22)%5B1%5D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFDeviceProduct%20%3D%20%24line.split(%22%7C%22)%5B2%5D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFDeviceVersion%20%3D%20%24line.split(%22%7C%22)%5B3%5D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFDeviceEventClassId%20%3D%20%24line.split(%22%7C%22)%5B4%5D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFName%20%3D%20%24line.split(%22%7C%22)%5B5%5D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFSeverity%20%3D%20%24line.split(%22%7C%22)%5B6%5D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFExtension%20%3D%20%24line.split(%22%7C%22)%5B7%5D%20-split%20'(%5B%5E%3D%5Cs%5D%2B%3D(%3F%3A%5B%5C%5C%5D%3D%7C%5B%5E%3D%5D)%2B)(%3F%3A%5Cs%7C%24)'%3CBR%20%2F%3E%20%20%20%20%20%20foreach%20(%24extenstion%20in%20%24CEFExtension)%20%7B%3CBR%20%2F%3E%20%20%20%20%20%20%20%20if%20(%24extenstion%20-like%20%22*%3D*%22)%20%7B%20%24cefmsg%20%2B%3D%20%40%7B%24extenstion.Split(%22%3D%22)%5B0%5D%20%3D%20%24extenstion.Split(%22%3D%22)%5B1%5D%20%7D%20%7D%3CBR%20%2F%3E%20%20%20%20%20%20%7D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFmsg%20%2B%3D%20%40%7BTimeGenerated%20%3D%20%24CEFtimegenerated%20%7D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFmsg%20%2B%3D%20%40%7BDeviceVendor%20%3D%20%24CEFDeviceVendor%20%7D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFmsg%20%2B%3D%20%40%7BDeviceProduct%20%3D%20%24CEFDeviceProduct%20%7D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFmsg%20%2B%3D%20%40%7BDeviceVersion%20%3D%20%24CEFDeviceVersion%20%7D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFmsg%20%2B%3D%20%40%7BDeviceEventClassID%20%3D%20%24CEFDeviceEventClassId%20%7D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFmsg%20%2B%3D%20%40%7BActivity%20%3D%20%24CEFName%20%7D%3CBR%20%2F%3E%20%20%20%20%20%20%24CEFmsg%20%2B%3D%20%40%7BLogSeverity%20%3D%20%24CEFSeverity%20%7D%3CBR%20%2F%3E%20%20%20%20%20%20%24data%20%2B%3D%20%24CEFmsg%3CBR%20%2F%3E%20%20%20%20%20%20%24cefmsg%20%3D%20%40%7B%7D%3CBR%20%2F%3E%20%20%20%20%7D%3CBR%20%2F%3E%20%20%7D%3CBR%20%2F%3E%20%20Write-Host%20%22Finished%20Handling%20Log%20file%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3Eelse%20%7B%20Write-Host%20%22%24filename%20is%20not%20supported%20yet%22%20%7D%3C%2FTIME%3E%3C%2FTIME%3E%3C%2FPRE%3E%0A%3CP%3E%3CBR%20%2F%3ENext%20we%20can%20upload%20the%20data%20to%20Log%20Analytics.%26nbsp%3B%20The%20API%20supports%20up%20to%2030MB%20per%20upload.%26nbsp%3B%20Since%20FileSize%20is%20not%20equal%20to%20HashTable%20size%20which%20is%20normally%20less%20than%20JSON%20actual%20size%2C%20we%20need%20to%20convert%20the%20data%20to%20JSON%20and%20measure%20the%20actual%20size.%26nbsp%3B%20I%20created%20a%20loop%20to%20add%20each%20record%20to%20a%20temp%20object%20until%20that%20object%20is%20greater%20than%2025MB%20then%20upload%20that%20chunk%20and%20start%20over.%20I%20used%2025MB%20as%20a%20safety.%26nbsp%3B%20If%20its%20less%20than%2025MB%20to%20start%20with%20then%20just%20upload%20the%20data%20as%20is.%3C%2FP%3E%0A%3CPRE%3E%23Test%20Size%3B%20Log%20A%20limit%20is%2030MB%3CBR%20%2F%3E%24tempdata%20%3D%20%40()%3CBR%20%2F%3E%24tempDataSize%20%3D%200%3CBR%20%2F%3EWrite-Host%20%22Checking%20if%20upload%20is%20over%2025MB%22%3CBR%20%2F%3Eif%20(((%24Data%20%7C%20Convertto-json%20-depth%2020).Length)%20-gt%2025MB)%20%7B%3CBR%20%2F%3E%20%20Write-Host%20%22Upload%20needs%20to%20be%20split%22%3CBR%20%2F%3E%20%20foreach%20(%24record%20in%20%24data)%20%7B%3CBR%20%2F%3E%20%20%20%20%24tempdata%20%2B%3D%20%24record%3CBR%20%2F%3E%20%20%20%20%24tempDataSize%20%2B%3D%20(%24record%20%7C%20ConvertTo-Json%20-depth%2020).Length%3CBR%20%2F%3E%20%20%20%20if%20(%24tempDataSize%20-gt%2025MB)%20%7B%3CBR%20%2F%3E%20%20%20%20%20%20%20Write-OMSLogfile%20-dateTime%20(Get-Date)%20-type%20%24CustomLogName%20-logdata%20%24tempdata%20-CustomerID%20%24workspaceId%20-SharedKey%20%24workspaceKey%3CBR%20%2F%3E%20%20%20%20%20%20%20Write-Host%20%22Sending%20data%20%3D%20%24TempDataSize%22%3CBR%20%2F%3E%20%20%20%20%20%20%20%24tempdata%20%3D%20%24null%3CBR%20%2F%3E%20%20%20%20%20%20%20%24tempdata%20%3D%20%40()%3CBR%20%2F%3E%20%20%20%20%20%20%20%24tempDataSize%20%3D%200%3CBR%20%2F%3E%20%20%20%20%7D%3CBR%20%2F%3E%20%20%7D%3CBR%20%2F%3E%20%20Write-Host%20%22Sending%20left%20over%20data%20%3D%20%24Tempdatasize%22%3CBR%20%2F%3E%20%20Write-OMSLogfile%20-dateTime%20(Get-Date)%20-type%20%24CustomLogName%20-logdata%20%24tempdata%20-CustomerID%20%24workspaceId%20-SharedKey%20%24workspaceKey%3CBR%20%2F%3E%7D%3CBR%20%2F%3EElse%20%7B%3CBR%20%2F%3E%20%20%23Send%20to%20Log%20A%20as%20is%3CBR%20%2F%3E%20%20Write-Host%20%22Upload%20does%20not%20need%20to%20be%20split%2C%20sending%20to%20Log%20A%22%3CBR%20%2F%3E%20%20Write-OMSLogfile%20-dateTime%20(Get-Date)%20-type%20%24CustomLogName%20-logdata%20%24Data%20-CustomerID%20%24workspaceId%20-SharedKey%20%24workspaceKey%3CBR%20%2F%3E%7D%3C%2FPRE%3E%0A%3CP%3ELastly%20we%20need%20to%20clean%20up%20the%20file%20we%20downloaded.%3C%2FP%3E%0A%3CPRE%3ERemove-Item%20%24FileName%20-Force%3C%2FPRE%3E%0A%3CH3%20id%3D%22toc-hId-75254966%22%20id%3D%22toc-hId-75254966%22%3EDeploy%20the%20code%3C%2FH3%3E%0A%3CP%3EThe%20Lambda%20function%20will%20need%20an%20execution%20role%20defined%20that%20grants%20access%20to%20the%20S3%20bucket%20and%20CloudWatch%20logs.%26nbsp%3B%20To%20create%20an%20execution%20role%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EOpen%20the%20%3CA%20href%3D%22https%3A%2F%2Fconsole.aws.amazon.com%2Fiam%2Fhome%23%2Froles%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eroles%3C%2FA%3E%20page%20in%20the%20IAM%20console.%3C%2FLI%3E%0A%3CLI%3EChoose%20Create%20role.%3C%2FLI%3E%0A%3CLI%3ECreate%20a%20role%20with%20the%20following%20properties.%0A%3CUL%3E%0A%3CLI%3ETrusted%20entity%20%E2%80%93%20AWS%20Lambda.%3C%2FLI%3E%0A%3CLI%3EPermissions%20%E2%80%93%20AWSLambdaExecute.%3C%2FLI%3E%0A%3CLI%3ERole%20name%20%E2%80%93%20lambda-s3-role.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EThe%20AWSLambdaExecute%20policy%20has%20the%20permissions%20that%20the%20function%20needs%20to%20manage%20objects%20in%20Amazon%20S3%20and%20write%20logs%20to%20CloudWatch%20Logs.%26nbsp%3B%20Copy%20the%20Amazon%20Resource%20Name%20(ARN)%20of%20the%20role%20created%20as%20you%20will%20need%20it%20for%20the%20next%20step.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENOTE%3A%20The%20AWSLambdaExecute%20permission%20might%20not%20allow%20access%20to%20the%20file%20in%20S3.%26nbsp%3B%20You%20can%20add%20to%20the%20role%20a%20Permission%20for%20the%20S3%20bucket(s)%20to%20allow%20this.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20deploy%20the%20PowerShell%20script%2C%20you%20can%20create%20a%20Package%20(zip%20file)%20to%20upload%20to%20the%20AWS%20console%20or%20you%20can%20use%20the%20Publish-AWSPowerShell%20cmdlet.%26nbsp%3B%26nbsp%3B%20We%20will%20use%20the%20cmdlet.%3C%2FP%3E%0A%3CPRE%3EPublish-AWSPowerShellLambda%20-Name%20YourLambdaNameHere%20-ScriptPath%20%26lt%3Bpath%26gt%3B%2FS3Event.ps1%20-Region%20%26lt%3Bregion%26gt%3B%20-IAMRoleArn%20%26lt%3Barn%20of%20role%20created%20earlier%26gt%3B%20-ProfileName%20%26lt%3Bprofile%26gt%3B%3C%2FPRE%3E%0A%3CP%3EYou%20might%20need%20%E2%80%93ProfileName%20if%20your%20configuration%20of%20.aws%2Fcredentials%20file%20doesn't%20contain%20a%20default.%26nbsp%3B%20See%20-ERR%3AREF-NOT-FOUND-this%20document%20for%20information%20on%20setting%20up%20your%20AWS%20credentials.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20the%20sample%20output%20from%20my%20running%20of%20the%20cmdlet.%3C%2FP%3E%0A%3CPRE%3E%E2%80%A6....%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Fwin%2Flib%2Fnetcoreapp3.1%2FModules%2FMicrosoft.WSMan.Management%2FMicrosoft.WSMan.Management.psd1%20(deflated%2042%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Fwin%2Flib%2Fnetcoreapp3.1%2FModules%2FCimCmdlets%2FCimCmdlets.psd1%20(deflated%2044%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Fwin%2Flib%2Fnetcoreapp3.1%2FModules%2FMicrosoft.PowerShell.Diagnostics%2FMicrosoft.PowerShell.Diagnostics.psd1%20(deflated%2042%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Fwin%2Flib%2Fnetcoreapp3.1%2FModules%2FMicrosoft.PowerShell.Diagnostics%2FEvent.format.ps1xml%20(deflated%2058%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Fwin%2Flib%2Fnetcoreapp3.1%2FModules%2FMicrosoft.PowerShell.Diagnostics%2FDiagnostics.format.ps1xml%20(deflated%2054%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Fwin%2Flib%2Fnetcoreapp3.1%2FModules%2FMicrosoft.PowerShell.Diagnostics%2FGetEvent.types.ps1xml%20(deflated%2055%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Fwin%2Flib%2Fnetcoreapp3.1%2FModules%2FMicrosoft.PowerShell.Security%2FMicrosoft.PowerShell.Security.psd1%20(deflated%2042%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Fwin%2Flib%2Fnetcoreapp3.1%2FModules%2FPSDiagnostics%2FPSDiagnostics.psm1%20(deflated%2059%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Fwin%2Flib%2Fnetcoreapp3.1%2FModules%2FPSDiagnostics%2FPSDiagnostics.psd1%20(deflated%2042%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Funix%2Flib%2Fnetcoreapp3.1%2FModules%2FMicrosoft.PowerShell.Utility%2FMicrosoft.PowerShell.Utility.psd1%20(deflated%2059%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Funix%2Flib%2Fnetcoreapp3.1%2FModules%2FMicrosoft.PowerShell.Management%2FMicrosoft.PowerShell.Management.psd1%20(deflated%2060%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Funix%2Flib%2Fnetcoreapp3.1%2FModules%2FMicrosoft.PowerShell.Host%2FMicrosoft.PowerShell.Host.psd1%20(deflated%2035%25)%3CBR%20%2F%3E...%20zipping%3A%26nbsp%3B%26nbsp%3B%20adding%3A%20runtimes%2Funix%2Flib%2Fnetcoreapp3.1%2FModules%2FMicrosoft.PowerShell.Security%2FMicrosoft.PowerShell.Security.psd1%20(deflated%2039%25)%3CBR%20%2F%3ECreated%20publish%20archive%20(%2Fprivate%2Fvar%2Ffolders%2Fhv%2F786wbs4n13ldvz9765nx8nv40000gn%2FT%2FS3toSentinel%2Fbin%2FRelease%2Fnetcoreapp3.1%2FS3toSentinel.zip).%3CBR%20%2F%3ECreating%20new%20Lambda%20function%20S3toSentinel%3CBR%20%2F%3ENew%20Lambda%20function%20created%3C%2FPRE%3E%0A%3CP%3EOnce%20created%2C%20login%20to%20the%20-ERR%3AREF-NOT-FOUND-AWS%20console.%26nbsp%3B%26nbsp%3B%20In%20Find%20services%2C%20search%20for%20Lambda.%26nbsp%3B%20Click%20on%20%3CSTRONG%3ELambda%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorNicholas%20DiCola%20(SECURITY%20JEDI)_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20id%3D%22tinyMceEditorNicholas%20DiCola%20(SECURITY%20JEDI)_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EClick%20on%20the%20lambda%20function%20name%20you%20used%20with%20the%20cmdlet.%26nbsp%3BClick%3CSTRONG%3E%20Add%20Trigger%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorNicholas%20DiCola%20(SECURITY%20JEDI)_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture2.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F210698iE2B2B45BB5D86B52%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22Picture2.png%22%20alt%3D%22Picture2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3ESelect%20%3CSTRONG%3ES3%3C%2FSTRONG%3E.%26nbsp%3B%20Select%20the%20%3CSTRONG%3Ebucket%3C%2FSTRONG%3E.%26nbsp%3B%20Acknowledge%20the%20warning%20at%20the%20bottom.%26nbsp%3B%20Click%20%3CSTRONG%3E%3CSTRONG%3EAdd%3C%2FSTRONG%3E%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorNicholas%20DiCola%20(SECURITY%20JEDI)_2%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture3.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F210699i922C54C89AF1ED6D%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22Picture3.png%22%20alt%3D%22Picture3.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYour%20lambda%20function%20is%20ready%20to%20send%20data%20to%20Log%20Analytics.%26nbsp%3B%E2%80%83%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1732199497%22%20id%3D%22toc-hId--1732199497%22%3ETest%20the%20Code%3C%2FH3%3E%0A%3CP%3ETo%20test%20your%20function%2C%20upload%20a%20support%20file%20to%20the%20S3%20bucket%20defined%20in%20the%20trigger.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorNicholas%20DiCola%20(SECURITY%20JEDI)_3%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture4.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F210700iC9F1B03629A81815%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22Picture4.png%22%20alt%3D%22Picture4.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20view%20the%20results%20of%20your%20test%2C%20go%20the%20Lambda%20function.%26nbsp%3B%20Click%20%3CSTRONG%3EMonitoring%3C%2FSTRONG%3E%20tab.%26nbsp%3B%20Click%20view%20logs%20in%20%3CSTRONG%3ECloudWatch%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorNicholas%20DiCola%20(SECURITY%20JEDI)_4%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture5.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F210701i9AAD82C8B1BFBCEA%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22Picture5.png%22%20alt%3D%22Picture5.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20%3CSTRONG%3ECloudWatch%3C%2FSTRONG%3E%2C%20you%20will%20see%20each%20log%20stream%20from%20the%20runs.%26nbsp%3B%20Select%20the%20latest.%26nbsp%3B%26nbsp%3BHere%20you%20can%20see%20anything%20from%20the%20script%20from%20the%20Write-Host%20cmdlet.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorNicholas%20DiCola%20(SECURITY%20JEDI)_5%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture7.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F210703i307D53237C7D43F1%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22Picture7.png%22%20alt%3D%22Picture7.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorNicholas%20DiCola%20(SECURITY%20JEDI)_6%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3EGo%20to%20portal.azure.com%20and%20verify%20your%20data%20is%20in%20the%20custom%20log.%26nbsp%3B%26nbsp%3BRun%20a%20query%20for%20your%20%3CCUSTOMLOGNAME%3E_CL.%26nbsp%3B%20If%20this%20is%20a%20new%20custom%20log%20it%20may%20take%20a%20few%20minutes%20for%20results%20to%20show%20up.%26nbsp%3B%20For%20more%20on%20queries%20see%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-monitor%2Flog-query%2Fget-started-queries%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-monitor%2Flog-query%2Fget-started-queries%3C%2FA%3E%3C%2FCUSTOMLOGNAME%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture8.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F210704i184E1B1D827C0998%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22Picture8.png%22%20alt%3D%22Picture8.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorNicholas%20DiCola%20(SECURITY%20JEDI)_7%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3ENOTE%3A%20To%20process%20large%20files%2C%20you%20may%20need%20to%20increase%20the%20Lambda%20function%20timeout.%26nbsp%3B%26nbsp%3BFor%20more%20information%20on%20timeouts%20see%20%3CA%20href%3D%22https%3A%2F%2Fdocs.aws.amazon.com%2Flambda%2Flatest%2Fdg%2Fgettingstarted-limits.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.aws.amazon.com%2Flambda%2Flatest%2Fdg%2Fgettingstarted-limits.html%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture9.png%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F210864i47D0538FA73C37FC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Picture9.png%22%20alt%3D%22Picture9.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%20Steps%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ECreate%20an%20analytic%20rule%20on%20your%20data%20-%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fsentinel%2Ftutorial-detect-threats-custom%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fsentinel%2Ftutorial-detect-threats-custom%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3ECreate%20a%20workbook%20on%20your%20data%20-%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsentinel%2Ftutorial-monitor-your-data%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsentinel%2Ftutorial-monitor-your-data%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EI%20hope%20this%20will%20help%20some%20folks%20solve%20a%20unique%20challenge!%26nbsp%3B%26nbsp%3BYou%20can%20find%20the%20full%20code%20and%20README%20-ERR%3AREF-NOT-FOUND-here.%3C%2FP%3E%0A%3CP%3EThanks%20to%26nbsp%3B-ERR%3AREF-NOT-FOUND-%40Chi_Nguyen%2C%20Joel%20Stidley%2C%20and%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2FAzure-Sentinel%2Ftree%2Fmaster%2FDataConnectors%2FS3-Lambda%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%40sarahyo%3C%2FA%3E%26nbsp%3B%26nbsp%3Bfor%20the%20help%20on%20this.%3C%2FP%3E%0A%3CP%3EBest%2C%3C%2FP%3E%0A%3CP%3ENicholas%20DiCola%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2FMasterSecJedi%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESecurity%20Jedi%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1571136%22%20slang%3D%22en-US%22%3E%3CP%3EDo%20you%20have%20data%20going%20into%20S3%20Buckets%20as%20files%20that%20you%20want%20to%20ingest%20into%20Azure%20Sentinel%3F%26nbsp%3B%3CBR%20%2F%3EIn%20this%20blog%2C%20I%20will%20show%20you%20how%20to%20create%20an%20AWS%20Lambda%20function%20running%20PowerShell%20to%20ingest%20the%20data%20into%20Azure%20Sentinel.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1571136%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Sentinel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESecurity%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E

Do you have data going into S3 Buckets as files that you want to ingest into Azure Sentinel? 

In this blog, I will show you how to create an AWS Lambda function running PowerShell to ingest the data into Azure Sentinel.  The reason you might need or want to do this is AWS Lamdba allows for a trigger on S3 CreateObject.  The function can automatically be called every time a new file is added to the bucket.

 

Spoiler
If you want to get right to the code.  go here.

Setup

To deploy this, you will need a machine prepared with the following:

  • PowerShell Core – I recommend PowerShell 7 found here
  • .Net Core 3.1 SDK found here
  • AWSLambdaPSCore module – You can install this either from the PowerShell Gallery, or you can install it by using the following PowerShell Core shell command:
    • Install-Module AWSLambdaPSCore -Scope CurrentUser

See the documentation here https://docs.aws.amazon.com/lambda/latest/dg/powershell-devenv.html

I recommend you review https://docs.aws.amazon.com/lambda/latest/dg/powershell-package.html to review the cmdlets that are part of AWSLambdaPSCore.

 

Review of the Code

Our script will be triggered when a log file is created in an S3 bucket. To create the base script, run the following PowerShell core command:

 

New-AWSPowerShellLambda -Template S3Event

 

The default template for and S3Event trigger is below:

# PowerShell script file to be executed as a AWS Lambda function.
#
# When executing in Lambda the following variables will be predefined.
# $LambdaInput - A PSObject that contains the Lambda function input data.
# $LambdaContext - An Amazon.Lambda.Core.ILambdaContext object that contains information about the currently running Lambda environment.
#
# The last item in the PowerShell pipeline will be returned as the result of the Lambda function.
#
# To include PowerShell modules with your Lambda function, like the AWS.Tools.S3 module, add a "#Requires" statement
# indicating the module and version. If using an AWS.Tools.* module the AWS.Tools.Common module is also required.
#
# The following link contains documentation describing the structure of the S3 event object.
# https://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html

#Requires -Modules @{ModuleName='AWS.Tools.Common';ModuleVersion='4.0.6.0'}
#Requires -Modules @{ModuleName='AWS.Tools.S3';ModuleVersion='4.0.6.0'}

# Uncomment to send the input event to CloudWatch Logs
# Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)

foreach ($record in $LambdaInput.Records) {
$bucket = $record.s3.bucket.name
$key = $record.s3.object.key

Write-Host "Processing event for: bucket = $bucket, key = $key"

# TODO: Add logic to handle S3 event record, for example
$obj = Get-S3Object -Bucket $bucket -Key $key
Write-Host "Object $key is $($obj.Size) bytes"
}

As you can see for each CreateObject event from S3 the default script pulls the bucket name ($bucket = $record.s3.bucket.name) and filename ($key = $record.s3.object.key) Then Write-Host will send an output to CloudWatch logs.  Lastly it gets the file size using the Get-S3object cmdlet.

I used this basic template as a starter template.  First, I added a function called Write-OMSLogFile which I got from Travis Roberts on GitHub here.  In short, the function takes a hash table with the log data and a few parameters and uploads it to the Log Analytics REST API.  The documentation on this API is here.  The function needs three parameters you will need to provide:

  • Line 158 - $WorkspaceId – you will need to enter the workspace id
  • Line 159 - $WorkspaceKey – you will need to enter the workspace key
  • Line 160 - $CustomLogName – you will need to enter the Custom Log name you want to use for the data.

 After we have those setup, the first action we need to do is download the file from S3 to tmp storage.

Write-Host "Downloading $key to /tmp/$key"
Read-S3Object -BucketName $bucket -Key $key -File "/tmp/$key"
Write-Host "Downloaded $key to /tmp/$key"


Once we have the file, we need to process the file based on file type.  I have made some simple assumptions.

  • If its .csv its CSV data which is easy to import to a hash table using Import-CSV.
  • If its .json, its JSON data which is easy to import to hash table using Get-Content | ConvertFrom-JSON
  • Lastly, if it’s a .log file the data contains CEF formatted data which takes some special processing.
    • I used some REGEX and splitting to break apart the CEF Message and then build a hash table of records.
  • Or its not supported in this function yet.
#Determine if CSV or JSON or whatever
$FileName = "/tmp/$key"
if ($fileName -like "*.csv") {
Write-Host "Handling CSV File"
$data = import-csv $filename
}
elseif ($filename -like "*.json") {
Write-Host "Handling JSON File"
$Data = Get-Content $filename | ConvertFrom-Json
}
elseif ($filename -like "*.log") {
Write-Host "Handling Log File"
#Assuming CEF formatted logs
$cefdata = Get-Content $filename
$data = @()
$cefmsg = @{}
foreach ($line in $cefdata) {
if ($line -like "*CEF:*") {
#Write-Host "Handling CEF Data"
$CEFtimegenerated = ($line -split '(?<time>(?:\w+ +){2,3}(?:\d+:){2}\d+|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.[\w\-\:\+]{3,12})')[1]
#$CEFHost = (($line -split '(?<time>(?:\w+ +){2,3}(?:\d+:){2}\d+|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.[\w\-\:\+]{3,12})')[2] -split "CEF:")[0]
#$CEFVersion = $line.Split("CEF: ").Split("|")[1]
$CEFDeviceVendor = $line.split("|")[1]
$CEFDeviceProduct = $line.split("|")[2]
$CEFDeviceVersion = $line.split("|")[3]
$CEFDeviceEventClassId = $line.split("|")[4]
$CEFName = $line.split("|")[5]
$CEFSeverity = $line.split("|")[6]
$CEFExtension = $line.split("|")[7] -split '([^=\s]+=(?:[\\]=|[^=])+)(?:\s|$)'
foreach ($extenstion in $CEFExtension) {
if ($extenstion -like "*=*") { $cefmsg += @{$extenstion.Split("=")[0] = $extenstion.Split("=")[1] } }
}
$CEFmsg += @{TimeGenerated = $CEFtimegenerated }
$CEFmsg += @{DeviceVendor = $CEFDeviceVendor }
$CEFmsg += @{DeviceProduct = $CEFDeviceProduct }
$CEFmsg += @{DeviceVersion = $CEFDeviceVersion }
$CEFmsg += @{DeviceEventClassID = $CEFDeviceEventClassId }
$CEFmsg += @{Activity = $CEFName }
$CEFmsg += @{LogSeverity = $CEFSeverity }
$data += $CEFmsg
$cefmsg = @{}
}
}
Write-Host "Finished Handling Log file"
}
else { Write-Host "$filename is not supported yet" }


Next we can upload the data to Log Analytics.  The API supports up to 30MB per upload.  Since FileSize is not equal to HashTable size which is normally less than JSON actual size, we need to convert the data to JSON and measure the actual size.  I created a loop to add each record to a temp object until that object is greater than 25MB then upload that chunk and start over. I used 25MB as a safety.  If its less than 25MB to start with then just upload the data as is.

#Test Size; Log A limit is 30MB
$tempdata = @()
$tempDataSize = 0
Write-Host "Checking if upload is over 25MB"
if ((($Data | Convertto-json -depth 20).Length) -gt 25MB) {
Write-Host "Upload needs to be split"
foreach ($record in $data) {
$tempdata += $record
$tempDataSize += ($record | ConvertTo-Json -depth 20).Length
if ($tempDataSize -gt 25MB) {
Write-OMSLogfile -dateTime (Get-Date) -type $CustomLogName -logdata $tempdata -CustomerID $workspaceId -SharedKey $workspaceKey
Write-Host "Sending data = $TempDataSize"
$tempdata = $null
$tempdata = @()
$tempDataSize = 0
}
}
Write-Host "Sending left over data = $Tempdatasize"
Write-OMSLogfile -dateTime (Get-Date) -type $CustomLogName -logdata $tempdata -CustomerID $workspaceId -SharedKey $workspaceKey
}
Else {
#Send to Log A as is
Write-Host "Upload does not need to be split, sending to Log A"
Write-OMSLogfile -dateTime (Get-Date) -type $CustomLogName -logdata $Data -CustomerID $workspaceId -SharedKey $workspaceKey
}

Lastly we need to clean up the file we downloaded.

Remove-Item $FileName -Force

Deploy the code

The Lambda function will need an execution role defined that grants access to the S3 bucket and CloudWatch logs.  To create an execution role:

  1. Open the roles page in the IAM console.
  2. Choose Create role.
  3. Create a role with the following properties.
    • Trusted entity – AWS Lambda.
    • Permissions – AWSLambdaExecute.
    • Role name – lambda-s3-role.

The AWSLambdaExecute policy has the permissions that the function needs to manage objects in Amazon S3 and write logs to CloudWatch Logs.  Copy the Amazon Resource Name (ARN) of the role created as you will need it for the next step.

 

NOTE: The AWSLambdaExecute permission might not allow access to the file in S3.  You can add to the role a Permission for the S3 bucket(s) to allow this.

 

To deploy the PowerShell script, you can create a Package (zip file) to upload to the AWS console or you can use the Publish-AWSPowerShell cmdlet.   We will use the cmdlet.

Publish-AWSPowerShellLambda -Name YourLambdaNameHere -ScriptPath <path>/S3Event.ps1 -Region <region> -IAMRoleArn <arn of role created earlier> -ProfileName <profile>

You might need –ProfileName if your configuration of .aws/credentials file doesn't contain a default.  See this document for information on setting up your AWS credentials.

 

Here is the sample output from my running of the cmdlet.

…....
... zipping:   adding: runtimes/win/lib/netcoreapp3.1/Modules/Microsoft.WSMan.Management/Microsoft.WSMan.Management.psd1 (deflated 42%)
... zipping:   adding: runtimes/win/lib/netcoreapp3.1/Modules/CimCmdlets/CimCmdlets.psd1 (deflated 44%)
... zipping:   adding: runtimes/win/lib/netcoreapp3.1/Modules/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1 (deflated 42%)
... zipping:   adding: runtimes/win/lib/netcoreapp3.1/Modules/Microsoft.PowerShell.Diagnostics/Event.format.ps1xml (deflated 58%)
... zipping:   adding: runtimes/win/lib/netcoreapp3.1/Modules/Microsoft.PowerShell.Diagnostics/Diagnostics.format.ps1xml (deflated 54%)
... zipping:   adding: runtimes/win/lib/netcoreapp3.1/Modules/Microsoft.PowerShell.Diagnostics/GetEvent.types.ps1xml (deflated 55%)
... zipping:   adding: runtimes/win/lib/netcoreapp3.1/Modules/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.psd1 (deflated 42%)
... zipping:   adding: runtimes/win/lib/netcoreapp3.1/Modules/PSDiagnostics/PSDiagnostics.psm1 (deflated 59%)
... zipping:   adding: runtimes/win/lib/netcoreapp3.1/Modules/PSDiagnostics/PSDiagnostics.psd1 (deflated 42%)
... zipping:   adding: runtimes/unix/lib/netcoreapp3.1/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 (deflated 59%)
... zipping:   adding: runtimes/unix/lib/netcoreapp3.1/Modules/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 (deflated 60%)
... zipping:   adding: runtimes/unix/lib/netcoreapp3.1/Modules/Microsoft.PowerShell.Host/Microsoft.PowerShell.Host.psd1 (deflated 35%)
... zipping:   adding: runtimes/unix/lib/netcoreapp3.1/Modules/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.psd1 (deflated 39%)
Created publish archive (/private/var/folders/hv/786wbs4n13ldvz9765nx8nv40000gn/T/S3toSentinel/bin/Release/netcoreapp3.1/S3toSentinel.zip).
Creating new Lambda function S3toSentinel
New Lambda function created

Once created, login to the AWS console.   In Find services, search for Lambda.  Click on Lambda.

 
 

 

Click on the lambda function name you used with the cmdlet. Click Add Trigger

 

Picture2.png

Select S3.  Select the bucket.  Acknowledge the warning at the bottom.  Click Add.

 

Picture3.png

 

Your lambda function is ready to send data to Log Analytics.  

 

Test the Code

To test your function, upload a support file to the S3 bucket defined in the trigger.

 

Picture4.png

 

To view the results of your test, go the Lambda function.  Click Monitoring tab.  Click view logs in CloudWatch.

 

Picture5.png

 

In CloudWatch, you will see each log stream from the runs.  Select the latest.  Here you can see anything from the script from the Write-Host cmdlet.

 

Picture7.png

 

 

Go to portal.azure.com and verify your data is in the custom log.  Run a query for your <CustomLogName>_CL.  If this is a new custom log it may take a few minutes for results to show up.  For more on queries see https://docs.microsoft.com/azure/azure-monitor/log-query/get-started-queries

Picture8.png

 

 

NOTE: To process large files, you may need to increase the Lambda function timeout.  For more information on timeouts see https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html 

Picture9.png

 

Next Steps:

I hope this will help some folks solve a unique challenge!  You can find the full code and README here.

Thanks to @Chi_Nguyen, Joel Stidley, and @Sarah_Young  for the help on this.

Best,

Nicholas DiCola

Security Jedi