Home

Awesome

Ministry of Justice Instance Scheduler

Standards Icon Format Code Icon Scorecards Icon SCA Icon Build test push Icon

A Go lambda function for stopping and starting instance, rds resources and autoscaling groups. The function is used by the Ministry of Justice Modernisation Platform and can be re-used in any environment with minimal changes.

Requirements

To install the SAM CLI on macOS:

brew tap aws/tap
brew install aws-sam-cli

Local development

Install dependencies & build the target

cd modernisation-platform-instance-scheduler
make

:warning: Code changes require a rebuild before you're able to run them. Use local-re-run.sh as a utility script when re-running local code changes.

Validate SAM template

aws-vault exec core-shared-services-production -- sam validate

Invoke Function

aws-vault exec core-shared-services-production -- sam local invoke --event event.json

Test Function in the Cloud

aws-vault exec core-shared-services-production -- sam sync --stack-name instance-scheduler --watch

Deploy on sprinkler-development using the local samconfig.toml and preventing prompts and failure when the stack is unchanged. The following command requires the instance-scheduler-ecr-repo ECR repository to be present in order to succeed.

ACCOUNT_ID=$(aws-vault exec sprinkler-development -- aws sts get-caller-identity --query Account --output text)
aws-vault exec sprinkler-development -- sam deploy --no-confirm-changeset --no-fail-on-empty-changeset --region eu-west-2 --image-repository $ACCOUNT_ID.dkr.ecr.eu-west-2.amazonaws.com/instance-scheduler-ecr-repo

Module initialisation. The following commands were used in order to generate the required go.mod and go.sum files prior to the first run of the tests.

cd instance-scheduler
go mod init github.com/ministryofjustice/modernisation-platform-instance-scheduler
go mod tidy
go mod download

Run Tests

cd instance-scheduler
aws-vault exec core-shared-services-production -- go test -v .

Configuration

References

  1. User Guide
  2. How the original Go SAM project was created
  3. AWS Serverless Application Model
  4. AWS Serverless Application Repository
  5. Terraform module for deployment of the lambda