finish lab 1

This commit is contained in:
Ric Harvey 2024-04-16 16:16:03 +01:00
parent 2c5e63343a
commit 29e5ba28e9
Signed by: ric
GPG key ID: 7FDEB4C0C7D5CB61
2 changed files with 75 additions and 0 deletions

Binary file not shown.

View file

@ -115,6 +115,8 @@ Now lets run some code.
> [!Note] > [!Note]
> You can replace the commands for ```terraform``` from ```tofu``` if you are running that version > You can replace the commands for ```terraform``` from ```tofu``` if you are running that version
#### init
First lets initialise our terraform/tofu environment: First lets initialise our terraform/tofu environment:
```bash ```bash
@ -124,3 +126,76 @@ tofu init
![tofu init output on screen](./img/tofu-init.png) ![tofu init output on screen](./img/tofu-init.png)
This has installed all the providers and modules you need for running your code, basically it's downloaded any dependancies.
#### plan
Now we are going to run ```tofu plan```, this will test that your code will actually run and compare the code to any previous deployed resources saved in a state file (more on this later).
```bash
tofu plan
```
This command will generate a lot of output, if you get any RED text you have an issue and you'll need to debug. The code in the repository should be fine however, if not please open an issue.
![tofu plan output](./img/tofu-plan.png)
#### apply
Now lets apply this IaC and deploy the following:
- A VPC
- 6 Subnets
- 3 Public
- 3 Private
- An EC2 instance with a Security Group
- Allows TCP ports 22,80 and 443 ingress
- Allows all egress
Lets run the following code:
```bash
tofu apply
```
![tofu apply output and prompt](./img/tofu-apply-1.png)
At the prompt you need to type **yes** in order to actually deploy the infrastructure, anything else will result in the program terminating. Once complete you should see the following output:
![tofu apply output after sucessfully deploying](./img/tofu-apply-2.png)
The Outputs seen on the screen are controlled by the ```outputs.tf``` file in the repo. If you browse in the AWS console to **EC2 > Instances** you should see that there is indeed a new EC2 instance running in your account.
![Running EC2 instance in the aws console](./img/deployed-ec2.png)
#### destroy
Right time is money as they say, lets shut this down and stop spending it. Luckily in tofu/terraform this is super easy to do and we can run the following command. Tofu will reference the ```.tfstate``` file and see what it needs to do to shut everything down.
```bash
tofu destroy
```
This command can take a while to run, be patient and it'll do it's job. You'll get prompted along the way yo confirm you really want to delete everything, make sure to answer **yes** again.
![tofu destroy prompt to continue](./img/tofu-destroy-1.png)
Once complete, you'll get some confirmation like the picture below:
![tofu destroy completed](./img/tofu-destroy-2.png)
> [!Note]
> Don't worry about the warning you'll be ok with the acl not being deleted, it won't cost anything
### Recap
What we've learnt during this exercise:
- How to install terraform/tofu in cloud shell
- terraform/tofu basic commands
- init
- plan
- apply
- destroy
Now lets move onto the next lab to learn how to customise this and build upon it.