From 332503e5c942dff9a6ad66e5e2520be9a5ebbcee Mon Sep 17 00:00:00 2001 From: Ric Harvey Date: Tue, 16 Apr 2024 18:57:37 +0100 Subject: [PATCH] update docs --- 2-simple-example/README.md | 2 +- 2-simple-example/img/.DS_Store | Bin 6148 -> 6148 bytes 3-remote-states/README.md | 114 ++++++++++++++++++++++++++++++--- 3 files changed, 107 insertions(+), 9 deletions(-) diff --git a/2-simple-example/README.md b/2-simple-example/README.md index 6e11283..fa8b838 100644 --- a/2-simple-example/README.md +++ b/2-simple-example/README.md @@ -1,4 +1,4 @@ -### Introduction +## Introduction Welcome to lab 2. You are going to find the code here very similar to lab 1, and thats by design, we'll build upon our experience and learn how to add variables and use environment files, so that you can reuse the code in say a deployment for dev, stage and prod. This is the power of IaC and prevents you having to rebuild everything from scratch. diff --git a/2-simple-example/img/.DS_Store b/2-simple-example/img/.DS_Store index 98775a175caf21c0141296e9c6f2ad0ec1ea32d2..e0cff1d148d4943fcb75d0907744e95f4760b601 100644 GIT binary patch delta 70 zcmZoMXfc@J&&a+pU^gQp`(z%bz2X84c?_uxK9f3 delta 31 ncmZoMXfc@J&&akhU^gQp+hiW5y_-3i|FKMLaM;Yw@s}R}pl%9L diff --git a/3-remote-states/README.md b/3-remote-states/README.md index 490a741..b3b803e 100644 --- a/3-remote-states/README.md +++ b/3-remote-states/README.md @@ -1,15 +1,113 @@ -1. Create a Stack +## Introduction -2. Configure a remote state +So far we've learnt how to create, update and destroy infrastructure and use variables in our code so we can reuse what we've written over multiple enviroments. One thing you may have noticed while running the examples is that theres a file (sometimes two) created in you working directory in CloudShell called ```terraform.tfstate``` (and maybe ```terraform.tfstate.backup```). This is the state files that terraform/tofu uses to keep track of whats actually been deployed and any data that might returned by the provider, such as public_ip address which we used in our previous example. -dynamoDB and S3 +Now the problem of this state file being local is that only you have access to it. If you work in a team or even from different computers your self you are going to want to store that statefile somewhere it can be easily read and updated. Now you mightthing great I'll commitit to git, but this is a bad idea, it can easily get out of sync and that can lead to all kinds of ugly problems. Fortunately terraform/tofu supplies us with a solution, these are called **backends** and there are lots of them, you can store your state in a consul cluster for example, an HTTP endpoint in Gitlab and the one we are going to use as these examples are in AWS is DynamoDB and S3. S3 will store the actual state file and we are going use DynamoDB to provide a lock flag that stops multiple people trying to update the stack at the same time, which would end in tears! -3. Plan +### Setting up AWS -4. Apply +As this is a terraform/tofu workshop we aren't going to use clickops to make these resources we are going to use IaC of course. So start by creating a new directory in the ```3-remote-states/``` directory called ```state``` -5. Reference Remote State +```bash +mkdir state +cd state +``` -6. Apply +Now lets make some really simple terraform for this, create a new file called ```main.tf``` + +```bash +vi main.tf +``` + +and lets add the following code: + +```terraform +provider "aws" { + region = "eu-west-1" +} + +resource "aws_s3_bucket" "tfstate_bucket" { + bucket = var.bucket_name + acl = "private" + + versioning { + enabled = true + } + + lifecycle { + prevent_destroy = false + } + + tags = { + Name = "${var.environment}-s3" + } +} + +resource "aws_dynamodb_table" "remotestate_table" { + name = var.table_name + hash_key = "LockID" + billing_mode = "PAY_PER_REQUEST" + + attribute { + name = "LockID" + type = "S" + } + + tags = { + Name = "${var.environment}-Dynamo" + } +} + +``` +You'll notice in here we are creating an S3 bucket and a table in dynamoDB. These are going to be used to save our state from our main stack. It also includes some variables and we'll define those in a moment. + +Now save and exit and create a new file called ```versions.tf``` + +```bash +vi versions.tf +``` + +Lets add the following content: + +```terraform +terraform { + required_version = ">= 1.0" + + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 4.66.1" + } + } +} +``` + +Save and exit and now lets create our ```variables.tf``` file: + +```bash +vi variables.tf +``` + +An we want to set up the following, I've delibarately left out the default values for the ```bucket_name``` and the ```table_name```. This is because I want you to use unquie values, you'll be prompted for these soon! + +> [!Note] +> S3 buckets are in a global name space so your bucket_name must be unquie! + +```terraform +variable "environment" { + description = "Default environment" + type = string + default = "demo" +} + +variable "bucket_name" { + description = "Name for S3 Bucket" + type = string +} + +variable "table_name" { + description = "Name for DynamoDB Table" + type = string +} +``` -7. Destroy