Overview

In the following article, I described how to perform health checks.

I also described the command for restarting Virtuoso when it stops in the following article.

This time, I will try restarting Virtuoso in conjunction with Amazon SNS notifications.

Method

To send a command like sudo rm -rf /usr/local/var/lib/virtuoso/db/virtuoso.lck && ... to an EC2 instance, SSM (AWS Systems Manager) configuration was required.

IAM Roles and Policies

I created a new IAM role and granted the AmazonSSMFullAccess policy. Initially, I had granted the AmazonSSMManagedInstanceCore policy, but the following error occurred when executing the Lambda function described later, and it did not work properly.

Anerroroccurred(InvalidInstanceId)whencallingtheSendCommandoperation:Instances[[i-xxxxxx]]notinavalidstateforaccountxxxxxx

I selected and updated the created IAM role from “Modify IAM role” on the EC2 instance.

AWS SAM: Creating the Lambda Function

I used AWS SAM. Create a project with the following:

saminit

I created hello_world/app.py as follows. The instance_id part needs to be modified.

iidmmeppfoorrl#e#i#ri#c#srtcop#s}rttacnefoseiouriemI2SsCsDmSmsmmtiEmtbtbnpthpleme_pempnxpuoidi=eaeoeprfancoIDP.auCIttl""rtmatcncnnreindlnnoasnton(riOEnoe_ibicks(itndisscrldms"afur3haofeernueceetuae_=mtcitrsalty_Eetr=onammeiaatepoinioiC=s(ntmt=neepdsnndurmdz3ad2pfh'mcnt(sdco_t"ple.eo"esa=sete1=mIenr":lecs=icnunsINr._dIle:irElpn2scddbmdas0rc=dysof(Cie's.eooo_sm=)elc=pouie2ecitd[mttc=e{sioitoutevni-ae'mrool[='pemnhntpdectfxnsIam3ii'conmsespu_nl(ixccnnE.enAontateutrti'cxers{d-CcnsWms.na"nt[e,eexitir2lttSmegdn)e=['sncExsbantfi.a-a[e_cc'Spct2CxtensoiesnRn'tiee{Stoo'2xa_ct/nnecudC_d_stann)xtieabustnensoc,isansti'unSnest(d_S'modandeensstcra'_ih:mm,rdaxstaee/nscdeamyarttat_xlcso]l[nard)anuieoemm,lcdnidE:ncsdcc'mSo'dnOrcee}uav)acm]_fure_stlinrm[iotos'e/adia'nrprIt]dv(pnCvmuCDa)aStdooatotorS']mctCnu=n/M,}maiots=latone(tinintnI0"hbdoetn:)e/Infn'svd(rt]tii'o',anr]m]nst,ctuteaohInsedcose/o=du[(btiepn.vusgitt.ra,tnucroees_soit.dal]rc)tks&o&ftswuadroe)/usr/local/bin/virtuoso-t+configfile/usr/local/var/lib/virtuoso/db/virtuoso.ini'

I also added boto3 to hello_world/requirements.txt.

boto3

Local testing can be done with the following. If using a profile other than the default, provide the appropriate profile name for xxxx.

sambuild&&samlocalinvoke-profilexxxx

AWS SAM: Updating template.yaml

Modify template.yaml so that the Lambda function is executed in response to SNS notifications.

ATD#GROWreleuSascSMoFsVtVVTncjaobuoipiiesr-mranTMurTPurDVrDVmfivpelciertyrtteateapopilstmmcupoCHRA##Esuslusllrtrei:ieoeoepoaurv:ocuocuamitnoorss:ednnc-TreMsresret:ouSfnuy:oredthhenyoi:oi:enoAo:tSRAtUliixictSTPRpRpFA:sM:ieWiremt8sesNyret!et!oWoazsSeiree6i:SpoTsiGsiGrS>-Tb1et:s:::c_pvTepotoetoem:reo5:a::t6rer:epantanta:emurSvapu4osirir:Ar:AtSspt1teipyrpgStcttttVetl2errpteeagNi:e"te"teraaG8rvt.hsreSerVrIrvrtl:eulo:tmrsa:iVImVseteoroanye::rriapiirblsm3sntrmlrolfaeob.as:utRitneols_d9saaouocu:srssrasgwsolios::e_oesosets'-r:shc,:o:o22ehFtaisRRIR00stuanatneeAe11ttnrdthsssMs06apctlee:ttt--rst/esuaaRa01t:irLsrror90i/ota-ttlt--n/nhmeeeee03ggibarrr91i#sds.cR'VtatLAroihML-arelruoaf1mnaetbrmu:bt.u.ebnxdeAocdcxadrsoiatxnomnixFf/ffoxuoiaoun:nrnwnxcssacixtVtlbtsxiiaaoixornbuoixntcstnnue/vAossFwo#RseuikNornteA"vchdRRetNeritslohotenefasrsRStt-eNheasSerpoputeflroxuicpinceisca:ctttiiihdnootegnntf"-piSmsnNo:eSd/de/tlgao/ibpbtoilhvcouebb,/.mcsaoosmt/teahrwa/stdloawcbhsse//ngselevorebvraelrtslh.eerssstt-oappipclication-model/blob/master/versions/2016-10-31.md#awsserverlessfunction

Please modify the “ARN of the existing SNS topic” to the appropriate value. Also, the folder name storing the Lambda function shown earlier has been changed from hello_world to virtuoso_restart.

AWS SAM: Deployment

Deployment is done with the following:

sambuild&&samdeploy-profilexxxx

Summary

Since this was my first time using SSM (AWS Systems Manager), it took some time to resolve the errors, but I found it to be a convenient feature.

I hope this serves as a useful reference for others.