Create an environment
This guide will walk you through creating a new lower environment. A lower environment is a non-production environment in the non-prod AWS account, that can be used for testing and developing new features for Luna.
WARNING
This guide assumes the following infrastructure has been deployed to the non-prod AWS account:
- core-infrastructure (terraform)
- VPC
- WAF
- Redshift
If these are not in place - some of the services will not be able to deploy.
Prerequisites
- Terraform v1.3.1
- AWS Admin Access
- non-prod for deployments
- prod for datacopybacks
- Access to Gitlab
How to Deploy a Service / Client / Infrastructure
env/ branch creation
Before we begin it is import to understand how to deploy the various services, clients and infrastructure. These deployments should be done via the Gitlab CI. This ensures whatever is deployed has secure package versions and is passing tests and linting. Since this will be on a new environment you will need to create the environment branch on Gitlab before you can deploy. To do this follow these commands
git checkout main
git pull
git checkout -b env/<the-name-of-your-environment>
git push --set-upstream origin env/<the-name-of-your-environment> -o ci.skipWhere <the-name-of-your-environment> is the name of your environment. for example nebula or milkyway
This should create the branch and skip the pipeline it creates. We will deploy in a certain order.
WARNING
Environment names should be one word to adhere to the branch naming conventions.
Please try to NOT exceed 11 characters in the environment name as this can cause some AWS resource naming limitations.
Deployment guide
To Deploy a service / client / infrastructure stack to your new environment. Make sure you are on the env/<the-name-of-your-environment> branch and ensure you have made a change to a file in that stack's directory. For example, to deploy the apps service, ensure there is a change in the services/apps/ directory. This can be as little as a new line in the README.md or the ci.yml
Commit the change and push it up to trigger a deployment via the pipeline.
For example, deploying the apps service would look like this:
git checkout env/<the-name-of-your-environment>
cd services/apps
... make change to a file ...
git add .
git commit -m 'deploy apps service'
git pushCode Changes
core-infrastructure
When adding a new environment you will need to add the additional callbacks urls to the development cognito instance. This can be done via Terraform.
- Add the following urls to the
platform/core-infrastructure/targets/development/main.tfto theadditional_auth_callbacksarray
"https://your-env-name.dev.luna.and.sh", "https://your-env-name.dev.luna.and.sh/", "https://your-env-name.dev.luna.and.sh/callback", "https://your-env-name.dev.luna.and.sh/callback/"Where your-env-name is the name of your environment
- Add a new chatbot-stream certificate for your environment in
platform/core-infrastructure/targets/development/main.tfin thedomains_mapobject
"chatbot-stream.v2.your-env-name.dev.luna.and.sh" = {
with_auth = false
record_in_parent = false # A record will be created by the application code
hosted_zone = false
parent_override = "dev.luna.and.sh" # without this the module will assume it's parent to be v2.dev.luna.and.sh.
},Where your-env-name is the name of your environment
- Deploy the Terraform (ensure you have non-prod aws credentials set up in your terminal)
cd platform/core-infrastructure/targets/development
terraform init
terraform plan -out tf.planVerify your plan with a tech lead before proceeding. If everything looks fine do the following to apply the changes.
terraform apply tf.plan- Revert manually some changes
Terraform will change the pre-token lambda for the development cognito instance to use the development role and development database. We need to revert these changes as they break the lower envs. (This is a temporary work around whilst we fix the code)
- Change the
luna-development-pre-token-triggerlambda code via AWS console to fetch andis from thestagingtable instead ofdevelopment - Change the
luna-development-pre-token-triggerlambda role to be theluna-staging-cognito-lambda-role
environment files
You will need to adapt the following files for an environment variables for your new environment. Follow the examples for the other environments. If you are creating an environment with a name that already is defined in those files, you only need to change the chatbot stream certificate ARN.
- services/*/samconfig.toml
- clients/*/.env-cmdrc
- **/*/application.yml
Note: For the chatbot service, take a note of the certificate ARN that was created by the terraform and add it to the samconfig.toml for that service.
Deployments
Before Deploying
You will need to create log groups for any service that contains metric filters for alarms. You can search in the services application.yml for these metric filters. To create the log groups follow these steps:
- Navigate to AWS > Cloudwatch > Log groups > Create Log Group.
- The naming will need to be /aws/lambda/lambda-function-name. e.g. /aws/lambda/yourenvname-email-queue-handler-function
- Use defaults and create log group
Manually create the one-time IAM bootstrap for the platform stack - the ENVIRONMENT-iam-role-deployer-platform role and its policy, trusted via GitLab OIDC. Follow the guide in platform/README.md
Deploying
Deploy the infrastructure, services, clients as described above.
WARNING
Note: If a deployment fails this first time you will need to delete the stack to retry. (limitation of cloud formation) Be sure to manually remove any delete skipped resources like DDB or S3.
There is an order in which you should deploy. The platform stack should be first, and is deployed via GitLab CI/OIDC as two CloudFormation templates in order: platform/infrastructure/bootstrap.yml (creates the deployments bucket) then platform/infrastructure/permissions.yml (creates the IAM deploy roles, which depends on the bootstrap bucket)
After this the following should be deployed:
authServicevpcinfratructurewafinfratructureredshiftinfratructure
After the Auth service most of the services can be deployed other than the following which rely on other services:
andirelies onconfig. So deployconfigbefore deployingfinance-gatewayrelies onexpenses. So deployexpensesbefore deployingopportunitiesrelies onorganisations. So deployorganisationsbefore deploying
Clients can be deployed after the platform stack. However, they also have some dependencies:
luna-front-endrelies on allmfaclients being deployed. So deployluna-front-endafter all mfa's are deployed.
Data Creation
Once everything has been deployed, the environment will need some data. This can be done with a datacopyback script.
datacopyback
To run a datacopyback command, ensure you have installed the luna cli. To do this follow these steps:
cd cli
yarn installThen you can run a datacopyback command using the luna binary. Ensure you have production AWS credentials set up in your local terminal. This command will take ~ 20 - 40 minutes
luna datacopyback copy all -x actuals-table -o ~/Desktop/data -e productionOnce you have the copied and sanitised data locally you can upload it to your new environment. Ensure you have non-prod AWS credentials set up in your local terminal.
luna datacopyback upload -i ~/Desktop/data -e your-env-nameWhere your-env-name is the name of your environment
INFO
This command will take a while. Your AWS crednetials may expire before it finishes. If this happens, just delete the completed datacopyback files so they do not get re-run. (or move them to another folder)
Once this is done you can refresh actuals for your environment for you selected time period. (we do not copy the actuals table as it is too large.) Before you do this, you should add your environment table to the cli-scripts-legacy config. cli-scripts-legacy/config.js Follow existing examples.
To refresh actuals, run the actuals-refresh script through the Luna CLI. Ensure you have non-prod AWS credentials set up in your local terminal, and that the legacy scripts' dependencies are installed (cd cli-scripts-legacy && yarn install).
luna -e your-env-name script run cli-scripts-legacy/one-off/index.js --modules cli-scripts-legacy -- actuals-refresh -f 2024-05-01 -t 2024-08-31 -e your-env-nameWhere your-env-name is the name of your environment
Test
Verify your new environment works by using it. It should be available at https://yourenvname.dev.luna.and.sh/
Where yourenvname is the name of your environment.
