Auto Scaling Group with User Data Not working with cfn-signal

0

I have an Auto Scaling Group provisioned by SAM (Serverless Application Model) which is similar to Cloud Formation, which provisions Windows EC2 instances. I have User Data being executed as powershell scripts. Sometimes some scripts fail. If the PS scripts fail as part of User Data Execution, I want to roll back the stack. For that, I understand there is cfn-init and cfn-signal. I have the following code in the CloudFormation AutoScalingGroup template. MyASG: Type: AWS::AutoScaling::AutoScalingGroup CreationPolicy: ResourceSignal: Count: 2 Timeout: PT15M UpdatePolicy: AutoScalingRollingUpdate: MinInstancesInService: '1' MaxBatchSize: '1' PauseTime: PT12M5S WaitOnResourceSignals: true SuspendProcesses: - HealthCheck - ReplaceUnhealthy - AZRebalance - AlarmNotification - ScheduledActions

In my UserData, I have the following lines. ` UserData: Fn::Base64: !Sub | <powershell> # START LOG. $LASTEXITCODE=0 $ErrorActionPreference = "Stop" cfn-init.exe -v --stack ${AWS::StackName} --resource MyASG --region ${AWS::Region} cfn-signal.exe -e **%ERRORLEVEL% **--stack ${AWS::StackName} --resource MyASG --region ${AWS::Region} $timestamp = Get-Date -Format yyyyMMdd-hhmm Start-Transcript -Path $logpath -Force

          #  DOWNLOAD FILES
          write "Downloading files from S3 bucket"
          Get-S3Object -BucketName $bucketName -KeyPrefix ${myS3Path}/ | Read-S3Object -Folder "C:\"

          #  CALL Custom Exe to execute some PS scripts
          write "running command '.\custom.exe 
          .\custom.exe "config.yml" --region "${AWS::Region}"
          if ( $LASTEXITCODE -ne 0 ) { write-error "Custom exe exit code '$LASTEXITCODE' does not indicate success. Refer to logs for details." }              
        </powershell>  `

In the above PS code, if I use %ERRORLEVEL% in cfn-signal, it always returns 0 success signals, even when there are no errors (verified the userdataexecution.log), whereas if I use 0 in place of %ERRORLEVEL%, it always returns 2 of 2 success signals, even when there are errors. I am confused on how to use this whole thing or get this working or achieve this functionality. Any help will be appreciated.

gefragt vor einem Jahr104 Aufrufe
Keine Antworten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen