DEV Community

Cover image for Configure Your Cloud Infrastructure using Terraform and AWS CLI: A Step-by-Step Tutorial
Ikechukwu Kelechi Alexander
Ikechukwu Kelechi Alexander

Posted on

Configure Your Cloud Infrastructure using Terraform and AWS CLI: A Step-by-Step Tutorial

In this tutorial, you will set up an EC2 instance on Amazon Web Services (AWS). EC2 instances are virtual machines that run on AWS and are commonly used in infrastructure projects.

The requirements
To complete this tutorial, you'll need:

  • The Terraform CLI (1.2.0+) is installed.
  • The AWS CLI is installed.
  • You will need an AWS account and the necessary credentials to build resources.
  • Setting the AWS_ACCESS_KEY_ID environment variable will allow you to authenticate the Terraform AWS provider using your IAM credentials.

Log into your AWS portal

  1. Click on the top right-corner (Username)
  2. Select Security credentials
  3. Click on Create access key Aws dashboard 1
  • Check the "I Understand Box and Create." Accept & Create

Copy the Access Key and Secret key to your VScode Terminal

Press the enter button on every command you input.
Access Key

  • Input aws configure, then follow the instructions and paste your keys, respectively

VScode Terminal

  • Create your own working directory and cd into the directory Mkdir

Open main.tf in your text editor, paste in the configuration below, and save the file.
Copy and paste

For more information on Terraform Block, Terraform Registry
To learn more, reference the provider source documentation.

Initialise the directory.
When you create a new configuration or check out an old configuration from version control, you must initialise the directory using terraform init.

Terraform Initialized

When a configuration directory is initialised, the providers defined in the configuration, in this example the aws provider, are downloaded and installed.

Input terraform: fmt, Validate, & apply respectively
Terraform fmt, validate & apply

Terraform contd

  • Input yes to complete Input yes to complete

Inspect the current state using terraform show
Inspect

  • AWS Active Access Key Confirmation AWS Access Key Active

I hope this article is able to guide you in configuring your infrastructure.

You can follow me up on LinkedIn

Top comments (0)