diff --git a/.DS_Store b/.DS_Store index b31779f..83706ca 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/3-remote-states/.DS_Store b/3-remote-states/.DS_Store new file mode 100644 index 0000000..b6c0f37 Binary files /dev/null and b/3-remote-states/.DS_Store differ diff --git a/3-remote-states/README.md b/3-remote-states/README.md index b3b803e..92bab66 100644 --- a/3-remote-states/README.md +++ b/3-remote-states/README.md @@ -111,3 +111,105 @@ variable "table_name" { } ``` +Now lets deploy these resources. + +```bash +tofu init +tofu plan +``` + +At this point you'll get prompted to enter and bucket and table name on the command line. As you can see I called mine **demo-tofu-bucket** and **demo-tofu-table**. You'll need to pick different values and remember them for the next part of this lab. + +![Prompt to enter bucket and table name](./img/plan-prompt.png) + +Once complete you should see two resources have been created sucessfully! + +![Completed creating the state resources](./img/state-complete.png) + +### Configuring remote state for a stack + +Now lets use these resources and create our example stack again. We are going to edit the ```versions.tf``` file in our code directory: + +```bash +cd ../code +vi versions.tf +``` + +We'll add this code, but remember to change your ```bucket``` and ```dynamodb_table``` values to what you set in the last section. + +```terraform + backend "s3" { + bucket = "demo-tofu-bucket" + key = "terraform-tofu-lab/terraform.state" + region = "eu-west-1" + acl = "bucket-owner-full-control" + dynamodb_table = "demo-tofu-table" + } +``` + + So the resulting file looks like this: + +```terraform + terraform { + required_version = ">= 1.0" + + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 4.66.1" + } + } + + backend "s3" { + bucket = "demo-tofu-bucket" + key = "terraform-tofu-lab/terraform.state" + region = "eu-west-1" + acl = "bucket-owner-full-control" + dynamodb_table = "demo-tofu-table" + } +} +``` + +Now lets go ahead and use it. We start with: + +```bash +tofu init +``` + +You should see that terraform/tofu automatically uses S3 as the new state backend as comfirmed here: + +![S3 state confirmation](./img/using-s3-state.png) + +Now you are clear to run **plan** and **apply** and enter **yes** at the prompt. + +```bash +tofu plan +tofu apply +``` + +Now you'll notice theres no longer a ```terraform.state``` file clogging up you disk space on CloudShell and if you use your browser to go to the AWS console and the the S3 service followed by your bucket name you created you should see a ```terraform.state``` file in there. + +![The state file in S3](./img/s3-state.png) + +You won't see much in dynamoDB unless you are currently in the middle of an **apply** but that value of ```LockID``` gets set when you are updating or deploying the stack to avoid others overwriting the file in S3. + +Thats it you're done! You now have remote state configued for use on AWS using DynamoDB and S3, this means more than just you can operate your stack deployed by terraform/tofu which is essential for teams. If you want to lookinto other ways of storing state check out the documentation here: [https://opentofu.org/docs/language/state/remote-state-data/](https://opentofu.org/docs/language/state/remote-state-data/) + +### Clean up + +Lets clean up your resources now: + +```bash +tofu destroy +``` + +Enter **yes** at the prompt. We'll leave your state S3 bucket and dynamoDB table there for now as we'll use it in lab 5. + +### Recap + +What we've learnt in this lab: + +- Why remote state is important +- How to create a bucket and dynamoDB table for use in storing state +- How to use those resources to store state from another stack + diff --git a/3-remote-states/img/.DS_Store b/3-remote-states/img/.DS_Store new file mode 100644 index 0000000..eed43bc Binary files /dev/null and b/3-remote-states/img/.DS_Store differ diff --git a/3-remote-states/img/plan-prompt.png b/3-remote-states/img/plan-prompt.png new file mode 100644 index 0000000..08776a9 Binary files /dev/null and b/3-remote-states/img/plan-prompt.png differ diff --git a/3-remote-states/img/s3-state.png b/3-remote-states/img/s3-state.png new file mode 100644 index 0000000..39a3c59 Binary files /dev/null and b/3-remote-states/img/s3-state.png differ diff --git a/3-remote-states/img/state-complete.png b/3-remote-states/img/state-complete.png new file mode 100644 index 0000000..ea615cb Binary files /dev/null and b/3-remote-states/img/state-complete.png differ diff --git a/3-remote-states/img/using-s3-state.png b/3-remote-states/img/using-s3-state.png new file mode 100644 index 0000000..915b2ea Binary files /dev/null and b/3-remote-states/img/using-s3-state.png differ