DEV Community

Cover image for Step-by-Step Guide to Setting Up Terraform, AWS CLI, and Your AWS Environment
Ijay
Ijay

Posted on

Step-by-Step Guide to Setting Up Terraform, AWS CLI, and Your AWS Environment

In my previous post, I talked about what Infrastructure as Code is, why it matters, and why I decided to start learning Terraform.

Now it is time to move from understanding to actually setting things up.

Before building anything, having the right environment is important. If your setup is not correct, it can slow you down later. So in this post, I will walk through how I set up my Terraform environment and connected it to AWS.

Lets get started

Step 1: Setting Up AWS Account

I already had an AWS account, so I didn’t need to create a new one. However, I made sure not to use my root account.

Instead, I created an IAM user with programmatic access using the AWS console and used it for my setup instead of my root account. This is more secure and is the recommended way to work with AWS.

Step 2: Installing AWS CLI

Next, I installed AWS CLI on my system.

sudo apt update
sudo apt install awscli -y
Enter fullscreen mode Exit fullscreen mode

After installation, I checked that it was working by verifying the version. This confirmed that AWS CLI was installed correctly.

aws --version
Enter fullscreen mode Exit fullscreen mode

Step 3: Checking the credentials
After installing AWS CLI, I configured it using my IAM user credentials.

I added:

  • Access key

  • Secret key

  • Default region

I did this by running aws configure and following the prompts.

To confirm everything worked, I checked my AWS identity. It returned my account details, which showed that my setup was correct.

To check

terraform version
aws --version
aws sts get-caller-identity
aws configure list
Enter fullscreen mode Exit fullscreen mode

Output

Terraform v1.14.7
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v6.36.0
aws-cli/2.22.12 Python/3.12.6 Linux/6.6.87.2-microsoft-standard-WSL2 exe/x86_64.ubuntu.22
{
    "UserId": "AXXXXXXXXXXXXXXXXXXXV",
    "Account": "2xxxxxxxxxxxx4",
    "Arn": "arn:aws:iam::2xxxxxxxxxxx4:user/onyi"
}
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ****************5SNW shared-credentials-file
secret_key     ****************KJtJ shared-credentials-file
    region                eu-west-1      config-file    ~/.aws/config
Enter fullscreen mode Exit fullscreen mode

Step 4: Installing Terraform

I installed Terraform on my system and confirmed it was working by checking the version.

During this step, I noticed that my Terraform version was outdated. I am currently fixing this by downloading the latest version from the official
Terraform website.

error

Step 5. Connecting Terraform to AWS

Terraform uses the AWS credentials configured through AWS CLI.

Step 6. Verifying the Setup

To make sure everything was working properly, I ran checks to confirm:

  • Terraform is installed

  • AWS CLI is installed

  • AWS authentication is working

  • AWS configuration is correct

Everything worked as expected, which means my environment is ready.

Challenges I Faced

The main issue I encountered was that my Terraform version was outdated.

I discovered this while verifying my setup and started fixing it by installing the latest version.

What I Learned

From this setup, I learned how Terraform connects to AWS through AWS CLI.

I also understood why using an IAM user is important instead of using root credentials. It improves security and gives better control over access.

Conclusion

From everything, using the AWS CLI is more secured than flexible than clicking the AWS console.

If this resonates with you, feel free to share it with others on a similar journey.


Other Helpful Resources


Stay updated with my projects by following me on Twitter, LinkedIn, and GitHub.

Thank you for reading

Top comments (0)