terraform-tofu-labs/1-getting-started/README.md

127 lines
4.2 KiB
Markdown
Raw Normal View History

2024-04-16 14:23:10 +00:00
## Introduction
2024-04-14 16:03:51 +00:00
2024-04-16 14:23:10 +00:00
In this first lab we are going to do the basics. We'll:
2024-04-14 16:03:51 +00:00
2024-04-16 14:23:10 +00:00
- Log into AWS
- Use cloudshell (as this has all your credentials set up already)
- Get your environment ready to run terraform/tofu
- Run a simple example
2024-04-14 16:03:51 +00:00
2024-04-16 14:23:10 +00:00
### Log into AWS
First of all head to [https://aws.amazon.com](https://aws.amazon.com) and sign into your console. The next thing to do is check that you are working inthe correct region. For this lab everything is coded for eu-west-1 (Ireland) so make sure you select the correct region at the top right hand of the page.
![eu-west-1 Ireland Region](./img/eu-west-1.png)
Once in this region we are setup for the following labs.
> [!NOTE]
> We'll be creating infrastructure throughout and AWS does charge for your actual usage
### Open CloudShell
For these labs we are going to use the AWS supplied CloudShell, now you can do this from your own machine, however, you'll need to setup the [AWS CLI tool](https://aws.amazon.com/cli/) and AWS credentials which is beyond the scope of these labs. The nice thing about CloudShell is that it's already configured with your credentials for accessing AWS resources. You also launch this by heading up to the main bar in the AWS console and clicking the icon highlighted below:
![Launch CloudShell](./img/cloudshell.png)
Once launched you can hit the arrow to break the shell out into it's own browser tab. I personally find it easier to work this way. It's also good to know there is no charge to run CloudShell. It'll look a little like this when opened:
![CloudShell in it's own browser window](./img/cloudshell2.png)
Right lets get you setup to run terraform or openTofu! You only need to do one of these so choose your tool of choice. I lean toward using tofu as it's fully open source, but if you want some of the newer features in terraform 1.8.0 and higher the guide is here for you also.
> [!Note]
> Periodically you CloudShell is rebuilt and anything thats not stored in your home directory will be deleted, for this reason the method's below store the tooling in your home directory.
#### Install Terraform
First we are going to install a tool that make's it easy to install the binaries for Terraform. Copy and Paste the below into your CloudShell:
```bash
2024-04-14 16:03:51 +00:00
git clone https://github.com/tfutils/tfenv.git ~/.tfenv
mkdir ~/bin
ln -s ~/.tfenv/bin/* ~/bin/
```
2024-04-16 14:23:10 +00:00
Find the latest version and install it (at the time of writing it was **1.8.0**):
2024-04-14 16:03:51 +00:00
2024-04-16 14:23:10 +00:00
```bash
2024-04-14 16:03:51 +00:00
tfenv list-remote
tfenv install 1.8.0
tfenv use 1.8.0
```
Now test it's all working:
2024-04-16 14:23:10 +00:00
```bash
2024-04-14 16:03:51 +00:00
terraform --version
```
2024-04-16 14:23:10 +00:00
The output should look likt e following screenshot:
2024-04-14 16:03:51 +00:00
2024-04-16 14:23:10 +00:00
![Terraform output of the version flag](./img/terraform-working.png)
#### Install Tofu
If like me you prefer to use open source tools this is how to make tofu persist in CloudShell, Copy and Paste the following into your terminal:
```bash
2024-04-14 16:03:51 +00:00
# Download the installer script:
curl --proto '=https' --tlsv1.2 -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.sh
# Alternatively: wget --secure-protocol=TLSv1_2 --https-only https://get.opentofu.org/install-opentofu.sh -O install-opentofu.sh
# Give it execution permissions:
chmod +x install-opentofu.sh
# Please inspect the downloaded script
# Run the installer:
./install-opentofu.sh --install-method rpm
# Remove the installer:
rm install-opentofu.sh
2024-04-16 14:23:10 +00:00
# Make Tofu remain in your path after reboots
mkdir ~/bin
sudo mv /usr/bin/tofu ~/bin
```
Now test it's all working:
2024-04-14 16:03:51 +00:00
```
2024-04-16 14:23:10 +00:00
tofu --version
```
The output should look likt e following screenshot:
![Tofu output of the version flag](./img/tofu-working.png)
Now your tooling is installed and should survice you reconnecting to CloudShell.
### Test Deployment
Before we deploy to AWS we need to pull the code down from [GitLab](https://gitlab.com) onto your CloudShell. To do this run the follwoing commands:
```bash
git clone https://gitlab.com/ric_harvey/terraform-tofu-labs.git
cd terraform-tofu-labs
```
The output will look like the following:
![Git Clone the examples](./img/git-clone.png)
Now lets run some code.
> [!Note]
> You can replace the commands for ```terraform``` from ```tofu``` if you are running that version
First lets initialise our terraform/tofu environment:
```bash
2024-04-16 14:28:54 +00:00
cd 1-getting-started/code/
2024-04-16 14:23:10 +00:00
tofu init
```
2024-04-16 14:28:54 +00:00
![tofu init output on screen](./img/tofu-init.png)