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.
The list of MP accounts to be included by the scheduler, whether production or for testing purposes, is determined by the following criteria based on information obtained from the github/ministryofjustice/modernisation-platform/environments json files.
- the account-type is "member"
- the account is not production
- the field "instance_scheduler_skip": ["true"] has NOT been added to the "environment" list in the json.
If the account does not meet any of the above criteria then it is excluded.
Testing changes to the go source code of the module can be done by creating a Pull Request with a new branch containing the changes. The log output of the github workflow build-test-push.yml will show the results.
For development & running the scheduler locally:
- AWS Vault with a profile configured to access the core-shared-services account
- Docker installed
- Golang
- SAM CLI - Install the SAM CLI
To install the SAM CLI on macOS:
brew tap aws/tap
brew install aws-sam-cli
Install dependencies & build the target
cd modernisation-platform-instance-scheduler
make
⚠️ Code changes require a rebuild before you're able to run them. Uselocal-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 .
Note that setting a local environment variable INSTANCE_SCHEDULING_SKIP_ACCOUNTS is no longer required and it is not used.