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;
- Go to the IAM Console.
- Click Users and then click the Add Users button.
- Enter a name for the user and make sure “Access key - Programmatic access” is selected.
- 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.
- 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
To check if terraform is properly installed run this command
terraform --version
Install and Configure the AWS CLI
- Download the package installer using curl in your terminal
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
- Run the installer
sudo installer -pkg AWSCLIV2.pkg -target /
- Verify the installation
aws --version
- 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
AWS Access Key ID [None]: AKIA...
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxx
Default region name [None]: us-east-1
Default output format [None]: json
- Verify this works. The command below will return your account info.
aws sts get-caller-identity
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"
}
}
- In the terminal initialize your Terraform project by running the following command:
terraform init
- Apply the Terraform configuration to create the specified AWS resources:
terraform apply
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
Conclusion
Through this guide you gained an overview on how to create and destroy AWS instances with Terraform.
Top comments (0)