How to Install AWS CLI Version 2 and Setup AWS credentials

AWS CLI that is AWS Command Line Interface that enables you to interact with AWS services in various AWS accounts using commands in your command-line shell from your local environment or remotely. The AWS CLI provides direct access to the public APIs of AWS services.

You can control multiple AWS services from the command line and automate them through scripts. You can run AWS CLI commands from Linux shell such as bash , zsh , tcsh and from windows machine you can usecommand prompt or PowerShell to execute AWS CLI commands.

The AWS CLI is available in two versions but lets learn how to install AWS CLI version 2.

Table of Contents

  1. Installing AWS CLI Version 2 on windows machine
  2. Creating an IAM user in AWS account with programmatic access
  3. Configure AWS credentials using aws configure
  4. Verify aws configure from AWS CLI by running a simple commands
  5. Configuring AWS credentials using Named profile.
  6. Verify Named profile from AWS CLI by running a simple commands.
  7. Configuring AWS credentials using environment variable
  8. Conclusion

Installing AWS CLI Version 2 on windows machine

  • Download the installed for AWS CLI on windows machine from here
  • Select I accept the terms and then click next button
  • Do custom setup like location of installation and then click next button
  • Now you are ready to install the AWS CLI 2
  • Click finish and now verify the AWS cli
  • Verify the AWS version by going to command prompt and type
aws --version

Now AWS cli version 2 is successfully installed on windows machine.

Creating an IAM user in AWS account with programmatic access

There are two ways to connect to an AWS account, the first is providing a username and password on the AWS login page using browser and the other way is to configure Access key ID and secret keys of IAM user on your machine and then use command-line tools such as AWS CLI to connect programmatically.

For applications to connect from AWS CLI to AWS Service, you should already have Access key ID and secret keys with you that you will configure on your local machine to connect to AWS account.

Lets learn how to create a IAM user and Access key ID and secret keys !!

  1. Open your favorite web browser and navigate to the AWS Management Console and log in.
  2. While in the Console, click on the search bar at the top, search for ‘IAM’, and click on the IAM menu item.
  1. To Create a user click on Users→ Add user and provide the name of the user myuser and make sure to tick the Programmatic access checkbox in Access type which enables an access key ID and secret access key and then hit the Permissions button.
  1. Now select the “Attach existing policies directly” option in the set permissions and look for the “Administrator” policy using filter policies in the search box. This policy will allow myuser to have full access to AWS services.
  1. Finally click on Create user.
  2. Now, the user is created successfully and you will see an option to download a .csv file. Download this file which contains IAM users i.e. myuser Access key ID and Secret access key which you will use later in the tutorial to connect to AWS service from your local machine.

Configure AWS credentials using aws configure

Now you IAM user with Access key ID and secret keys ,but AWS CLI cannot perform anything unless you configure AWS credentials . Once you configure the credentials then AWS CLI allows you to connect to AWS account and execute commands.

  • Configure AWS Credentials by running the aws configure command on command prompt
aws configure
  • Enter the details such as AWS Access key ID , Secret Access Key , region . You can skip the output format as default or text or json .
  • Once AWS is configured successfully , verify by navigating to C:\Users\YOUR_USER\.aws  and see if two file credentials and config are present.
  • Now open both the files and verify.
  • Now, you’re AWS credentials are configured successfully using aws configure.

Verify aws configure from AWS CLI by running a simple commands

Now, you can test if AWS Access key ID , Secret Access Key , region you configured in AWS CLI is working fine by going to command prompt and running the following commands.

aws ec2 describe-instances

Configuring AWS credentials using Named profile.

A named profile is a collection of settings and credentials that you can apply to a AWS CLI command. When you specify a profile to run a command, the settings and credentials are used to run that command

Earlier you created one IAM user and configure AWS credentials using aws configure, lets learn how to store named profiles.

  1. Open credentials files which got created earlier using aws configure or create a file at  C:\Users\your_profile\.aws\credentials on your windows machine.
  2. Now , you can provide multiple  Access key ID and Secret access key  into the credentials file in the below format and save the file.

credentials files help you to set your profile. By this way, it helps you to create multiple profiles and avoid confusion while connecting to specific AWS accounts.

  1. Similarly, create another file C:\Users\your_profile\.aws\config in the same directory
  2. Next, add the “region” into the config file and make sure to add the name of the profile which you provided in the credentials file, and save the file. This file allows you to work with a specific region.

~/.aws/credentials (Linux & Mac) or %USERPROFILE%\.aws\credentials (Windows)

~/.aws/config (Linux & Mac) or %USERPROFILE%\.aws\config (Windows)

Verify Named profile from AWS CLI by running a simple commands

Lets open command prompt and run the below command to verify sandbox profile which you created earlier under two files ( %USERPROFILE%\.aws\credentials and USERPROFILE%\.aws\config)

aws ec2 describe-instances --profile sandbox

If you get a response shows you were able to configure Named profile succesfully.

Configuring AWS credentials using environment variable

Lets open command prompt and set the AWS secret key and access key using environmental variable. Using set to set an environment variable changes the value used until the end of the current command prompt session, or until you set the variable to a different value

Conclusion

In this tutorial, you learned how to install AWS CLI and configured it using AWS Access key ID , Secret Access Key, region. Also you learned how to generate AWS Access key ID , Secret Access Key by creating an IAM user.

How to Launch multiple EC2 instances on AWS using Terraform count and for_each

Creating lots of instances in any cloud provider is always required for any organization or is a project need. If you are asked to create 10 EC2 machines in a particular AWS account using console UI , I am sure it will take tons of hours to create it and lots of efforts. There are lots of automated ways which can create multiple instance in quick time , Yes that’s quite possible and with terraform its very simple and easy.

In this tutorial, we will learn to create multiple ec2 instance in AWS account using terraform code.

Table of content

  1. What is terraform?
  2. Prerequisites
  3. How to Install Terraform on Ubuntu 18.04 LTS
  4. Launch multiple EC2 instances of same type using count on AWS using Terraform
  5. Launch multiple EC2 instances of different type using for_each on AWS using Terraform
  6. Conclusion

What is Terraform?

Terraform is a tool for building , versioning and changing the infrastructure. Terraform is Written in GO Language and the syntax language of configuration files is hcl which stands for HashiCorp configuration language which is much easier than yaml or json.

Terraform has been in use for quite a while now . I would say its an amazing tool to build , change the infrastructure in very effective and simpler way. It’s used with variety of cloud provider such as Amazon AWS, Oracle, Microsoft Azure , Google cloud and many more. I hope you would love to learn it and utilize it.

Prerequisites

  • Ubuntu machine preferably 18.04 version + , if you don’t have any machine you can create a ec2 instance on AWS account
  • Recommended to have 4GB RAM
  • At least 5GB of drive space
  • Ubuntu machine should have IAM role attached with AWS EC2 instance creation which we will use later in tutorial using terraform

You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.

How to Install Terraform on Ubuntu 18.04 LTS

  • Update your already existing system packages.
sudo apt update
  • Download the latest version of terraform in opt directory
wget https://releases.hashicorp.com/terraform/0.14.8/terraform_0.14.8_linux_amd64.zip
  • Install zip package which will be required to unzip
sudo apt-get install zip -y
  • unzip the Terraform download zip file
unzip terraform*.zip
  • Move the executable to executable directory
sudo mv terraform /usr/local/bin
  • Verify the terraform by checking terraform command and version of terraform
terraform               # To check if terraform is installed 

terraform -version      # To check the terraform version  
  • This confirms that terraform has been successfully installed on ubuntu 18.04 machine.

Terraform Configuration Files and Structure

Let us first understand terraform configuration files before running Terraform commands.

  • main.tf : This file contains code that create or import other AWS resources.
  • vars.tf : This file defines variable types and optionally set the values.
  • output.tf: This file helps in generating of the output of AWS resources .The output is generated after the terraform apply command is executed.
  • terraform.tfvars: This file contains the actual values of variables which we created in vars.tf
  • provider.tf: This file is very important . You need to provide the details of providers such as AWS , Oracle or Google etc. so that terraform can make the communication with the same provider and then work with resources.

Launch multiple EC2 instances of same type using count on AWS using Terraform

Now, In this demonstration we will create multiple ec2 instance using count and for_each parameters in terraform. So Lets create all the configuration files which are required for creation of EC2 instance on AWS account using terraform.

  • Create a folder in opt directory and name it as terraform-demo 
mkdir /opt/terraform-demo
cd /opt/terraform-demo
  • Create main.tf file under terraform-demo folder and paste the content below.
resource "aws_instance" "my-machine" {
  count = 4     # Here we are creating identical 4 machines.
  
  ami = var.ami
  instance_type = var.instance_type
  tags = {
    Name = "my-machine-${count.index}"
         }
}
  • Create vars.tf file under terraform-demo folder and paste the content below
                                            # Creating a Variable for ami
variable "ami" {       
  type = string
}
                                           # Creating a Variable for instance_type
variable "instance_type" {    
  type = string
}
  • Create terraform.tfvars file under terraform-demo folder and paste the content below.
 ami = "ami-0742a572c2ce45ebf"
 instance_type = "t2.micro"
  • Create output.tffile under terraform-demo folder and paste the content below.

Note: value depends on resource name and type ( same as that of main.tf)

output "ec2_machines" {
  value = aws_instance.my-machine.*.arn  # Here * indicates that there are more than one arn as we used count as 4   
}
 

provider.tf:

provider "aws" {      # Defining the Provider Amazon  as we need to run this on AWS   
  region = "us-east-1"
}
  • Now your files and code are ready for execution . Initialize the terraform
terraform init
  • Terraform initialized successfully ,now its time to run the terraform plan command.
  • Terraform plan is a sort of a blueprint before deployment to confirm if correct resources are being provisioned or deleted.
terraform plan
  • After verification , now its time to actually deploy the code using apply.
terraform apply

Great Job, terraform commands execution was done successfully. Now we should have four EC2 instance launched in AWS.

Launch multiple EC2 instances of different type using for_each on AWS using Terraform

  • In previous example we created more than one resource but all with same attributes such as instance_type
  • Note: We use for_each in the terraform when we need to create more than one resources but with different attributes such as instance_type for keys etc.

main.tf

resource "aws_instance" "my-machine" {
  ami = var.ami
  for_each  = {                     # for_each iterates over each key and values
      key1 = "t2.micro"             # Instance 1 will have key1 with t2.micro instance type
      key2 = "t2.medium"            # Instance 2 will have key2 with t2.medium instance type
        }
        instance_type  = each.value
	key_name       = each.key
    tags =  {
	   Name  = each.value
	}
}

vars.tf

variable "tag_ec2" {
  type = list(string)
  default = ["ec21a","ec21b"]
}
                                           
variable "ami" {       # Creating a Variable for ami
  type = string
}

terraform.tfvars

ami = "ami-0742a572c2ce45ebf"
instance_type = "t2.micro"
  • Now code is ready for execution , initialize the terraform , run the plan and then use apply to deploy the code as described above.
terraform init 
terraform plan
terraform apply

Conclusion

Terraform is a great open source tool which provides easiest code and configuration files to work with. Its a best Infra as a code tool to start with. You should now have an idea to Launch multiple EC2 instances on AWS using Terraform count and for_each on Amazon web service.

Hope this tutorial will help you in understanding the terraform and running multiple instances on cloud. Please share with your friends.