Infrastructure definition using Terraform for Energy Calculation as a Service (ECaaS).
- Install Terraform: https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli
- Install AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
- Install AWS Vault: https://github.com/99designs/aws-vault
- Set up AWS access using aws-vault
In terraform, we use modules which may require variables to be set. Even the top level (root) terraform definitions may require variables.
These variables may be sensitive, so they should be stored securely and not checked into git.
To avoid having to type each var whenever you run terraform apply command, it is good idea to keep a private set of variables in tfvars file.
Better yet, to make things less verbose to run, store them in .auto.tfvars file.
Example .auto.tfvars file:
account_map = {
"integration" = "123456789012",
"staging" = "1111111111111",
}
prefix = "some-string-prefix"
some_list = [1, 5, 2]More info in official documentation
tfvars are currently stored alongside the state file in the S3 bucket epbr-{env}-terraform-state.
When updating the tfvars, make sure you update the file in the S3 bucket to avoid others being unable to deploy their changes.
There are handy just scripts which automate the process:
just tfvars-put ecaas-infrastructure {env} # where env is one of ecaas-integration, ecaas-staging or ecaas-production
just tfvars-get ecaas-infrastructure {env} # where env is one of ecaas-integration, ecaas-staging or ecaas-productionNOTE
tfvars-putuploads the local file{env}.tfvarsto the remote S3 state storage buckettfvars-getdownloads the remote tfvars file into{env}.tfvarsand also copies it into.auto.tfvarslocally
So a typical development flow might be:
tfvars-getfrom the remote- Update values in the file
{env}.tfvars tfvars-putto push the updates to the remotetfvars-getto update.auto.tfvarsfile- Run a
terraform applythat will use values from the.auto.tfvarsfile
Currently the tfvars are stored in plaintext on your machine to run the terraform scripts. We are planning on moving sensitive values to a more secure place. Until then, take care when handling the tfvars
- Don't check them into git!
- When adding new vars, mark them as
sensitive = truein Terraform - Don't store tfvars files on your machine - only download them to run the script, then delete
- Only pass them to others via the S3 bucket, as documented in previous section
The repo is subdivided into terraform for the following ECaaS infrastructure:
- /ecaas-infrastructure terraform for all resources in ecaas-integration, ecaas-staging and ecaas-production accounts
- /ci terraform for all resources in AWS EPB ci account
-
From root
cd ecaas-infrastructure -
Switch profile to environment you want to make changes in
just set-profile {AWS_profile_name_for_environment}Example:
just set-profile ecaas-integration -
Initialize your Terraform environment using the correct backend-config file
aws-vault exec {AWS_profile_name_for_environment} -- terraform init -backend-config=backend_{profile}.hclExamples:
aws-vault exec ecaas-ci-dev -- terraform init -backend-config=backend_ecaas_cicd.hclaws-vault exec ecaas-integration -- terraform init -backend-config=backend_ecaas_integration.hcl -
To run terraform you will need to download a copy of the parameters stored as tfvars in the environment. To do this run
just tfvars-get ecaas-infrastructure {AWS_profile_name_for_environment}Example:
just tfvars-get ecaas-infrastructure ecaas-integration -
Run a terraform plan and check all planned changes are expected:
aws-vault exec {AWS_profile_name_for_environment} -- terraform plan -out=tfplanExample:
aws-vault exec ecaas-integration -- terraform plan -
Once you are happy that all the changes are as expected, apply them
aws-vault exec {AWS_profile_name_for_environment} -- terraform apply tfplan -
(Optional) Once successfully applied, you should be able to see the changes in the AWS Management Console. Sanity check the changes have been applied as you expected.
When deployed infrastructure is no longer needed
-
aws-vault exec {AWS_profile_name_for_environment} -- terraform destroy -
Because the state of the S3 and DynamoDB are not stored in a permanent backend, those resources should be deleted through AWS console
You can see full documentation about working in our AWS accounts in tech docs.
- From root
cd ci - Follow the steps from applying Terraform changes to ecaas-infrastructure, but use your profile for the ecaas-ci
account, e.g.
just set-profile ecaas-ci - Download the latest version of
.auto.tfvarsfrom AWS using the just commandtfvars-get-for-ci - If you make changes to
.auto.tfvarsremember to upload it back to AWS usingtfvars-put-for-ci
Note: Do not do this if the infrastructure state management exists already - only for use in fresh AWS accounts
Before starting to terraform the infrastructure of an environment, you will need to set up an S3 bucket and DynamoDB table, so that terraform can store / lock the state.
The infrastructure used for an S3 backend is defined in the /state-init directory:
-
cd /state-init -
Initialize your Terraform enivronment
aws-vault exec {profile_name_for_AWS_environment} -- terraform initExample:
aws-vault exec ecaas-newaccount -- terraform init -
Create infrastructure
aws-vault exec {profile_name_for_AWS_environment} -- terraform applyExample:
aws-vault exec ecaas-newaccount -- terraform apply
If you've done work in a pair or ensemble why not add your co-author(s) to the commit? This way everyone involved is
given credit and people know who they can approach for questions about specific commits. To make this easy there is a
commit template with a list of regular contributors to this code base. You will find it at the root of this
project: commit_template.txt. Each row represents a possible co-author, however everyone is commented out by default (
using #), and any row that is commented out will not show up in the commit.
If your name is not in the commit_template.txt yet, edit the file and add a new row with your details, following the
format #Co-Authored-By: Name <email>, e.g. #Co-Authored-By: Maja <maja@gmail.com>. The email must match the email
you use for your GitHub account. To protect your privacy, you can activate and use your noreply GitHub addresses (find
it in GitHub under Settings > Emails > Keep my email addresses private).
To apply the commit template navigate to the root of the project in a terminal and
use: git config commit.template commit_template.txt. This will edit your local git config for the project and apply
the template to every future commit.
When creating a new commit, edit your commit (e.g. using vim, or a code editor) and delete the # in front of any
co-author(s) you want to credit. This means that it's probably easier and quicker to use git commit (instead
of git commit -m "" followed by a git commit --amend), as it will show you the commit template content for you to
edit.