Thisrepositoryandlayerisnolongerreceivingupdatesorsupport.I'vebeentoobusytokeepupwithchangesintheLambdaenvironmentandthishasfallenbehind.Useatyourownrisk.
RunBashinAWSLambdaviaLayers.ThisLayeris100%BashandhandlesallcommunicationwiththeLambdaRuntimeAPI.ThisallowsyoutorunfullBashscriptsandcommandsinsideofAWSLambda.ThisLayeralsoincludescommonCLItoolsusedinBashscripts.
SeetheHowTosectiontounderstandhowtousetheselayers.Alsoseetheexample-basic.shfileforanexampleofhowtowriteaBashscriptcompatiblewiththisLayer.
ARNarn:aws:lambda:<region>:744348701589:layer:bash:8HowToGettingStartedAWSLambdaConsoleLogintoyourAWSAccountandgototheLambdaConsole.CreateanewfunctionandgiveitanameandanIAMRole.Forthe"Runtime"selection,selectUsecustomruntimeinfunctioncodeorlayer.Inthe"Designer"sectionofyourfunctiondashboard,selecttheLayersbox.Scrolldowntothe"ReferencedLayers"sectionandclickAddalayer.SelecttheProvidealayerversionARNoption,thencopy/pastetheLayerARNforyourregion.ClicktheAddbutton.ClickSaveintheupperright.UploadyourcodeandstartusingBashinAWSLambda!AWSCLICreateafunctionthatusestheprovidedruntimeandtheLayerARNforyourregion.$awslambdacreate-function\--function-namebashFunction\--rolebashFunctionRole\--handlerindex.handler\--runtimeprovided\--layersarn:aws:lambda:<region>:744348701589:layer:bash:8\--zip-filefileb://function.zipStartusingBashinAWSLambda!UpdatingVersionsAWSLambdaConsoleInthe"Designer"sectionofyourfunctiondashboard,selecttheLayersbox.Scrolldowntothe"ReferencedLayers"sectionandclickAddalayer.SelecttheProvidealayerversionARNoption,thencopy/pastetheLayerARNforyourregion.ClicktheAddbutton.Stillunderthe"ReferencedLayers"section,selectthepreviousversionandclickRemove.ClickSaveintheupperright.AWSCLIUpdateyourfunction'sconfigrationandaddtheLayerARNforyourregion.$awslambdaupdate-function-configuration\--function-namebashFunction\--layersarn:aws:lambda:<region>:744348701589:layer:bash:8WritingScriptsLikeanyotherLambdafunctioncode,yourmainscript'snamemustmatchthefirstpartofyourhandler.Insideyourmainscript,youmustdefineafunctionthatmatchesthesecondpartofthehandler.Youmusthaveset-ebethefirstlineinsideyourfunction.Putting#!/bin/bashatthetopofyourfileisnotnecessary.SoifyourLambdahandlerisindex.handler,yourfileandcontentsshouldlooklike:
$catindex.shhandler(){set-e...}Theeventdataissenttoyourfunctionasthefirstparameter.Toaccessit,youshoulduse$1.Soifyouneedtheeventdata,youshouldsetittoavariable.Forexample,EVENT_DATA=$1.
handler(){set-eEVENT_DATA=$1}Allthepre-installedtoolsarealreadyinyour$PATHsoyoucanusethemasexpected.AnycommandoutputisautomaticallysenttoCloudWatch,justlikenormalLambdafunctions.
handler(){set-eEVENT_DATA=$1awss3ls$(echo$EVENT_DATA|jq."bucket")}Ifyouneedtosendaresponseback,youshouldsendtheresponsetostderr.(seetheCaveatssectionforanexplanation)Tosendoutputtostderryoushoulduse>&2.ThiswillbepickedupandreturnedfromtheLambdafunction.
handler(){set-eEVENT_DATA=$1awss3ls$(echo$EVENT_DATA|jq."bucket")echo"{\"success\":true}">&2}CaveatsBashbehavesinwaysunlikeotherprogramminglanguages.Assuch,therearesomerequirementsontheuser'sendthatmustbedone.
set-emustbesetinsideyourfunction
Bydefault,abashscriptwon'texitwhenitencountersanerror.Inorderforthelayertocorrectlycatchtheerrorandreportit(aswellasstopthescriptfromexecuting),wemustsetthefunctiontoexitonerror.
Youmustsendyourreturnvaluetostderr
InsideanormalBashfunction,anythingthatissenttostdoutispartofthereturnvalueforthatfunction.Inordertoproperlycapturetheuser'sreturnvalueandstillsendstdouttoCloudWatch,thisLayerusesstderrasthereturnvalue.Tosendsomethingtostderrsimplyappend>&2totheendofthecommand.Seetheexamplescriptsforhelp.
Notes$HOMEissetto/tmp.ThisisbecausetheLambdafilesystemisread-onlyexceptforthe/tmpdirectory.Someprogramsrequire$HOMEtobewriteable(liketheAWSCLIandsomeSSHcommands),sothisallowsthemtoworkwithoutissue.
FilestoconfiguretheAWSCLIshouldbeputin/tmp/.aws.Bydefault,theCLIusesthesameregionandIAMRoleasyourlambdafunction.Ifyouneedtosetsomethingdifferent,youcanusethe/tmp/.aws/configand/tmp/.aws/credentialsfilesaccordingly.
Whenusingcurl,youshouldusethe-sflag.Withoutthesilentflag,curlwillsendtheprogressbarofyourrequesttostderr.Thiswillshowupinyourresponse.Soit'susuallybesttodisabletheprogressbar.
TheAWSCLIappearstobemuchslowerthanmostoftheAWSSDKs.TakethisintoconsiderationwhencomparingBashwithanotherlanguageandevaluatingexecutiontimes.
Ifacommandisloggingunwantedmessagestostderrthatarebeingpickedupinyourresponse,youcanseeifthereissomethingsimiliartoa--silentflag.Ifthereisnot,youcanremovethemessagestostderrbyredirectingto/dev/null(2>/dev/null)orredirectingstderrtostdoutforthatcommand(2>&1)tosendthemtoCloudWatch.
Withthismethodthereisnocontextinthefunction,onlyeventdata.Theeventdataissenttoyourfunctionasthefirstparameter.Sotoaccesstheeventdata,use$1,forexampleEVENT_DATA=$1.Inordertogivesomedetailsthatwereavailabeinthecontext,Iexportafewadditionalvariables.
AWS_LAMBDA_REQUEST_ID-AWSLambdaRequestID
AWS_LAMBDA_DEADLINE_MS-Time,inepoch,thatyourfunctionmustexitby
AWS_LAMBDA_FUNCTION_ARN-FullAWSLambdafunctionARN
AWS_LAMBDA_TRACE_ID-Thesamplingdecision,traceID,andparentsegmentIDofAWSXRay
BuildingTobuildalayer,simplyrunmakebuild.Thiswillcreateaziparchiveofthelayerintheexport/directory.
PublishingTopublishthelayertothepublic,simplyrunmakepublish.Thiswillcreateanewversionofthelayerfromtheexport/layer.zipfile(createfromtheBuildstep)andgiveitaglobalreadpermission.
AddingNewExecutablesSomeexecutablesareabletorunbythemselvesandsomerequireadditionaldependenciesthatarepresentontheserver.It'shardtocoverherecasehere,butiftheexecutablerunbyitselfitcaneasilybeadded.Ifithasdependencies,youmustexplorewhatthosedependenciesareandhowtoaddthemtothelayeraswell.
YoucaneitheraddtheexecutablefromanAmazonLinuxAMIorfromthelambci/lambda:build-python3.6Dockerimage.
Disclaimer:Iusuallydon'taddinexecutablesfrompullrequestsforsecurityreasons.IfyouwouldliketoseeanexecutableinthislayermakeanissueandI'lltrytoaddit.
IncludedExecutables$aws$bc$git$jq$rsync$scp$sftp$ssh$sshpass$time$traceroute$tree$wget$vim$zip$unzipAlreadyincludedintheLambdaenvironment:
$awk$cat$curl$cut$date$diff$grep$gzip$head$md5sum$pwd$sed$tar$tail$tee$xargsIfyouwouldliketoseemore,pleasecreateanissue.
Shout-outtotheLambCIteamfortheirworkonlambci/git-lambda-layerwhichsomeofthegitandsshbuildprocesswastakenfrom.
评论