Prerequisites:
What is AWS CLI ?
AWS CLI is a tool that integrates all of the AWS services into a single central platform, allowing you to easily operate numerous AWS services with a single command. The acronym stands for Amazon Web Services Command Line Interface, and as the name implies, users access it via the command line. It allows you to control services directly or automate them using powerful scripts. AWS is a secure cloud services platform that provides computing power, content delivery, database storage, and other infrastructure services to developers.
Here are some of its benefits:
Efficiency: AWS CLI allows users to automate tasks and workflows by scripting commands, which can significantly improve operational efficiency.
Flexibility: Users can perform a wide range of tasks across different AWS services using AWS CLI, including managing instances, storage, databases, networking, and more. .
Scalability: AWS CLI is designed to scale alongside AWS services, making it suitable for managing environments of any size.
Portability: Since AWS CLI is a command-line tool, it can be used across different operating systems, including Windows, macOS, and Linux. This portability allows users to manage AWS resources consistently regardless of the platform they are using.
Integration: AWS CLI integrates seamlessly with other automation and scripting tools, such as shell scripts, Python scripts, and third-party automation frameworks like Ansible and Terraform.
Version Control: By using AWS CLI commands in scripts or code repositories, users can maintain version control over their infrastructure configurations and deployments.
AWS CLI installed
Here's an overview of how it works:
Installation: Users need to install the AWS CLI tool on their local machine or server. The installation process varies depending on the operating system being used, but AWS provides instructions for installation on Windows, macOS, and Linux.
sudo apt-get update sudo apt install awscli -y aws --version
AWS IAM:
Identity and Access Management (IAM) regulates (AWS) users and their access to AWS accounts and services. It defines the level of access a user can have over an AWS account, sets users, grants permissions, and allows a user to use different features of an AWS account.
The two keywords here are "who" and "permissions". "Who" refers to a distinct identification, such as a user, group, or role. "Permissions" refers to the policies that are associated with an identity. These permissions grant or deny access to a resource.
For example, we can use IAM to enable an EC2 instance to access S3 buckets by requesting fine-grained permissions.
Configuration: Once installed, users need to configure the AWS CLI with their AWS credentials. This typically involves providing an Access Key ID and Secret Access Key, which are obtained from the AWS Management Console by creating an IAM (Identity and Access Management) user with the necessary permissions. Users can also configure other settings, such as the default region and output format.
export AWS_ACCESS_KEY_ID=<access key> export AWS_SECRET_ACCESS_KEY=<secret access key>
aws configure
Authentication: When a user executes a command using the AWS CLI, the tool authenticates with AWS using the provided credentials. AWS uses these credentials to verify the identity and permissions of the user making the request.
Command Execution: Users can then use the AWS CLI to execute commands to perform various tasks, such as creating and managing instances, configuring storage, managing databases, setting up networking, and more. Each AWS service has its set of commands and options that can be used to interact with it.
Install AWS Providers:
# terraform.tf terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 4.0" } } }
Add the region where you want your instances to be.
# provider.tf provider "aws" { region = "us-east-1" }
Task1 :
Create a terraform file, provision an AWS EC2 instance using terraform AWS provider.
Step 1: Create a
terraform.tf
and pass the aws provider.terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 4.0" } } }
Step 2: Create a
providers.tf
and put the selected AWS Region that you want to create an EC2 instance.provider "aws" { region = "us-east-1" }
Step 3: Create the
aws.tf
file to provide all the details like AMI ID, instance type and instance name and the number of EC2 count that has to be created.resource "aws_instance" "aws_ec2_test" { count = 1 ami = "ami-0ddda618e961f2270" instance_type = "t2.micro" tags = { Name = "TerraformDemoInstance" } }
Step 4: Now the first step is to initialize the working directory with the necessary plugins and modules by executing
terraform init
terraform init
Step 5: Once you initialize all the plugins required for AWS, now execute the
terraform plan
which will create an execution plan by analyzing the changes required to achieve the desired state of your infrastructure.terraform plan
Step 6: Finally, use the command
terraform apply
it will apply the changes to create or update resources as neededterraform apply
Step 7: You can check, a new EC2 instance is created using Terraform as we provided a count as 1.
Step 8: Once you are done with the newly created instance we can use
terraform destroy
command which will delete the complete infrastructure.Step 9: Now from EC2 Instance, we can verify that the newly created EC2 instance is in the terminated state.
Conclusion
🔎Terraform is a powerful infrastructure as code (IaC) tool that lets you provide and manage cloud resources declaratively. When integrated with AWS (Amazon Web Services), Terraform becomes even more powerful, letting you to create, modify, and delete AWS resources using code.
🔎Terraform supports a wide range of AWS services, including compute instances (EC2), storage (S3, EBS), databases (RDS, DynamoDB), networking (VPC, subnets, security groups), load balancers, IAM roles, and more.
🔎Advantage of using Terraform for AWS infrastructure is its ability to manage the entire lifecycle of your resources. It can create, modify, and delete resources as needed, ensuring that your infrastructure remains in the desired state.
🔎Terraform also supports the concept of infrastructure as code, allowing you to version control your infrastructure configurations and collaborate with other team members.
Thank you for 📖reading my blog, 👍Like it and share it 🔄 with your friends .Hope you find it helpful🤞
Happy learning😊😊