update docs

This commit is contained in:
Ric Harvey 2024-04-16 18:57:37 +01:00
parent ecac8cccba
commit 332503e5c9
Signed by: ric
GPG key ID: 7FDEB4C0C7D5CB61
3 changed files with 107 additions and 9 deletions

View file

@ -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. 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.

Binary file not shown.

View file

@ -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