DEV Community

Ian Gitonga
Ian Gitonga

Posted on

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

Setting Up Your AWS Account
If you don’t already have an AWS account, head over to https://aws.amazon.com and sign up. When you first register for AWS, you initially sign in as the root user. This user account has access permissions to do absolutely anything in the account, so
from a security perspective, it’s not a good idea to use the root user on a day-to-day
basis.

You'll need to use the Identity and Access Management (IAM) service. To create a new IAM user;

  1. Go to the IAM Console.
  2. Click Users and then click the Add Users button.
  3. Enter a name for the user and make sure “Access key - Programmatic access” is selected.
  4. Click the Next button.

AWS will ask you to add permissions to the user. By default, new IAM users have no permissions whatsoever and cannot do anything in an AWS account. To give your IAM user the ability to do something, you need to associate one or more IAM Policies with that user’s account.

  1. Click Next a couple more times and then the “Create user” button.

AWS will show you the security credentials for that user, which consist of an Access Key ID and a Secret Access Key. You must save these immediately because they will never be shown again.

Installing Terraform
The easiest way to install Terraform is to use your operating system’s package manager. For my case since its macOS i use Homebrew.

$ brew tap hashicorp/tap
$ brew install hashicorp/tap/terraform
Enter fullscreen mode Exit fullscreen mode

To check if terraform is properly installed run this command
terraform --version

Install and Configure the AWS CLI

  1. Download the package installer using curl in your terminal
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
Enter fullscreen mode Exit fullscreen mode
  1. Run the installer
sudo installer -pkg AWSCLIV2.pkg -target /
Enter fullscreen mode Exit fullscreen mode
  1. Verify the installation
aws --version
Enter fullscreen mode Exit fullscreen mode
  1. Now to configure aws cli to your AWS account. This will also prompt you for 4 things. You'll get these credentials from the IAM user we created earlier.
aws configure
Enter fullscreen mode Exit fullscreen mode
AWS Access Key ID [None]: AKIA...
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxx
Default region name [None]: us-east-1
Default output format [None]: json
Enter fullscreen mode Exit fullscreen mode
  1. Verify this works. The command below will return your account info.
aws sts get-caller-identity
Enter fullscreen mode Exit fullscreen mode

Connect Terraform to AWS
I'll be using VS Code as my code editor of choice. I have also installed to extensions Hashicorp Terraform & AWS Toolkit.

  • Create a new Terraform file, e.g, main.tf and specify the AWS provider and configure the region as shown below:
provider "aws" {
  region = "eu-north-1"
}

resource "aws_instance" "instance1" {
  ami = "ami-0aaa636894689fa47"
  instance_type = "t2.micro"

  tags = {
    name = "temp-trial-instance"
  }
}
Enter fullscreen mode Exit fullscreen mode
  • In the terminal initialize your Terraform project by running the following command:
terraform init
Enter fullscreen mode Exit fullscreen mode
  • Apply the Terraform configuration to create the specified AWS resources:
terraform apply
Enter fullscreen mode Exit fullscreen mode

Congratualtion! If you check through AWS console you'll see your instance has been created.

  • To bring down the instance run the command below:
terraform destroy
Enter fullscreen mode Exit fullscreen mode

Conclusion
Through this guide you gained an overview on how to create and destroy AWS instances with Terraform.

Top comments (0)