Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Backend Configuration to Terraform #595

Draft
wants to merge 23 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6bbd79f
refact(terraform): restructured terraform directory and cloudfront mo…
alexrdrgz Jun 1, 2022
1787a62
feat(variables.tf): changed values of vars and added environment folder
alexrdrgz Jun 1, 2022
ed18fd1
feat(test.tfvars): made web domain name be set by env file
alexrdrgz Jun 1, 2022
f1ca598
chore(terraform): pushing to make sure all changes are pushed
alexrdrgz Jun 2, 2022
5053087
feat(variables.tf): refactored var values to be located in .tfvars file
alexrdrgz Jun 2, 2022
634f410
feat(terraform): refactored to use environment files
alexrdrgz Jun 8, 2022
62e78fc
feat(variables.tf): added new variables to pass to cloudfront gh modules
alexrdrgz Jun 9, 2022
7fe1ef9
feat(main.tf): refactored so github name is not stored in vars
alexrdrgz Jun 9, 2022
c6193bd
refact(staging.tfvars): deleted values that were used to test deployment
alexrdrgz Jun 9, 2022
3854524
refact(vazriables.tf): deleted unused var
alexrdrgz Jun 9, 2022
26c6ad2
refact(main.tf): changed value to be a placeholder value
alexrdrgz Jun 9, 2022
7b97c82
refact(--profile): deleted unneccessary file
alexrdrgz Jun 9, 2022
fe5a678
feat(varaiables.tf): added environment var for tagging purposes
alexrdrgz Jun 9, 2022
034cc6c
refact(staging.tfvars): added example value to tfvars file
alexrdrgz Jun 9, 2022
2bb6e52
refact(main.tf): got rid of string interpolation
alexrdrgz Jun 10, 2022
35bc95e
feat(variables.tf): changed policy to react-boilerplate-client cors p…
alexrdrgz Jun 10, 2022
59df629
feat(environment): added backend config files, added terraform plan a…
alexrdrgz Jun 16, 2022
c17ac10
feat(environment/.conf): replaced web domain with generic values
alexrdrgz Jun 23, 2022
1e1504a
feat(terraform): fixing merging conflicts
alexrdrgz Apr 4, 2023
aefc216
feat(readme): updated docs and naming conventions for npm scripts
alexrdrgz Apr 5, 2023
1191f80
feat(.gitigon): removed tf lock file, updated some documnetaion
alexrdrgz Apr 5, 2023
b6ebd29
featpushing latest chagesn
alexrdrgz Apr 27, 2023
8500ee6
feat(yarn.lock): removed from git tracking
alexrdrgz Apr 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn.lock

# terraform
.terraform
terraform.tfvars
terraform/.terraform.lock.hcl
40 changes: 23 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,31 +125,37 @@ Before deployments or initializing terraform you must have aws sso configured an

`aws sso login --profile BWTC-Developer`

Terraform also needs the project secrets saved in `terraform/staging/terraform.tfvars`. This file is not committed to version control since it can contain sensitive information such as database credentials and should be added locally. Create the `terraform/staging/terraform.tfvars` file with the following structure:
Terraform also needs the project secrets saved in `terraform/environment/staging.tfvars` and `terraform/environment/staging.backend.conf`. This new configurations will allow us to utilize terraform workspaces.

```
profile = "BWTC-Developer"
Terraform workspaces are a way to manage multiple instances of the same infrastructure stack in parallel, such as for different environments like development, staging, and production. They allow you to switch between workspaces to update configurations and make changes without affecting other environments or duplicating your code.

region = "us-west-2"
### Configuring Terraform Environment Files

web_domain_name = ""
To configure staging environment you will need to change two values

```
1. navigate to `terraform/environment/staging.backend.conf` and change the key, replace the folder name in `<test-staging-boilerplate-client-react>` with the name of your project and then add a `-staging`.

| Secret | Note |
| :-------------- | -----------------------------------------------------------------------------------------------: |
| profile | This must match the AWS credentials name on the development machine |
| region | This is usually `us-west-2` |
| web_domain_name | This will be the web domain name for the project, an example may be: `example.shift3sandbox.com` |
The value will look like this in the file
`key = "<test-staging-boilerplate-client-react>/terraform.tfstate"`

After adding the `terraform/staging/terraform.tfvars` file, `cd` into the `terraform/staging` directory and run the following commands to configure and build the AWS infrastructure for the sandbox environment
2. navigate to `terraform/environment/staging.tfvars` and set the webdomain to `project-name-environment.shift3sandbox.com`

```
terraform init
terraform apply
```
To initialize or switch to the staging environment run

`yarn env:staging`

To then deploy staging aws infrastructure run

`yarn provision:staging`

### Description of .backend.conf variables and values.

The infrastructure can be updated by changing the Terraform configuration files and running these commands again.
| Variable | Note |
| :------- | ------------------------------------------------------------------------------------------------------------: |
| profile | This must match the AWS credentials name on the development machine. You can locate the name in `.aws/config` |
| region | This is usually `us-west-2` |
| bucket | This will be a S3 bucket that holds your terraform state file |
| key | This will be the path to your Terraform state file located within an S3 bucket. |

### Environment Configuration

Expand Down
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,28 @@
"build": "tsc && vite build",
"predeploy:staging": "tsc && vite build --mode staging",
"predeploy:production": "tsc && vite build",
"deploy:staging": "aws s3 sync ./dist s3://<< replace-with-s3-bucket-name >> --profile shift3 --delete",
"deploy:production": "aws s3 sync ./dist s3://<< replace-with-s3-bucket-name >> --profile shift3 --delete",
"deploy:staging": "aws s3 sync ./dist s3://<< replace-with-s3-bucket-name >> --profile BWTC-Developer --delete",
"deploy:production": "aws s3 sync ./dist s3://<< replace-with-s3-bucket-name >> --profile client-profile --delete",
"test": "echo",
"test-ci": "echo",
"test-cov": "echo",
"test-cov-ci": "echo",
"eject": "react-scripts eject",
"prepare": "husky install",
"switch-state": "npm run terra:init -target=\"$npm_config_target\" -- -reconfigure",
"env:staging": "npm run switch-state -target=staging",
"env:production": "npm run switch-state -target=production",
"terra:init": "cd terraform && terraform init -backend-config=\"environment/$npm_config_target.backend.conf\"",
"terra:init-wrks": "cd terraform && if ! terraform workspace list | grep -q \"\\s$npm_config_name$\"; then terraform workspace new \"$npm_config_name\"; fi",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is terra:init-wrks needed here? Was this added to test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah and for the pre-provision

"terra:update": "cd terraform && terraform get -update",
"preprovision": "npm run terra:init && npm run terra:init-wrks -name=\"$npm_config_target\"",
"provision": "cd terraform && terraform workspace select \"$npm_config_target\" && terraform apply -var-file \"environment/$npm_config_target.tfvars\"",
"provision:staging": "npm run provision -target=staging",
"provision:production": "npm run provision -target=production",
"plan": "cd terraform && terraform workspace select \"$npm_config_target\" && terraform plan -var-file \"environment/$npm_config_target.tfvars\"",
"plan:dev": "npm run plan -target=development",
"plan:stg": "npm run plan -target=staging",
"plan:prod": "npm run plan -target=production",
"i18n": "i18next **/*.tsx"
},
"browserslist": {
Expand Down
21 changes: 0 additions & 21 deletions terraform/.terraform.lock.hcl

This file was deleted.

2 changes: 2 additions & 0 deletions terraform/environment/production.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# production site domain, livesite.com
web_domain_name = ""
4 changes: 4 additions & 0 deletions terraform/environment/productions.backend.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bucket = "production-terraform-state"
key = "<test-production-boilerplate-client-react>/terraform.tfstate"
region = "us-west-2"
profile = ""
4 changes: 4 additions & 0 deletions terraform/environment/staging.backend.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bucket = "shift3-terraform-state"
key = "<test-staging-boilerplate-client-react>/terraform.tfstate"
region = "us-west-2"
profile = "BWTC-Developer"
7 changes: 1 addition & 6 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ provider "aws" {
}

terraform {
backend "s3" {
bucket = "shift3-terraform-state"
key = "<initials or project name>-boilerplate-client-react/terraform.tfstate"
region = "us-west-2"
profile = "BWTC-Developer"
}
backend "s3" {}

required_providers {
aws = {
Expand Down
Loading