Lädt...

🔧 Build and Manage Cloud Infrastructure with Terraform and AWS CLI


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Table of Contents

  • Introduction
  • Prerequisites
  • Step 1: Configure the AWS CLI
  • Step 2: Create a Terraform Configuration File
  • Step 3: Initialize Terraform
  • Step 4: Plan, Format, and Validate Configuration
  • Step 5: Create the Infrastructure (EC2 instance)
  • Step 6: Inspect and Verify the EC2 Instance
  • Step 7: Delete the EC2 Instance (After Use)
  • Conclusion

Introduction

It is important to have efficient cloud infrastructure management because it enhances the scalability, reliability, and also reduces the cost. Terraform and AWS CLI are great tools that allow you to automate and simplify the cloud resource provisioning process.

In this guide, we will guide you on how to set up an Amazon EC2 instance using Terraform and AWS CLI, how to define, deploy, and manage infrastructure as code. When you go through this tutorial, you will be able to set up an EC2 instance that is fully functional and be able to scale and automate the deployment process for future projects.

Prerequisites

Before setting up an EC2 instance with Terraform and AWS CLI, ensure you have the following:

aws --version
  • Terraform Downloaded and Installed (Terraform’s official site). Confirm installation by running the command below in your terminal:
terraform --version
  • A Code Editor and Terminal (VS Code, Vim, or any code editor with a terminal). We would be using VS Code in this tutorial.

After setting all these up, we can start managing and automating your AWS EC2 instance or any other infrastructure! 🚀

Step 1: Configure the AWS CLI

  • In your AWS account, click your Username at the top-right.

  • Select Security Credencials.

  • Click the Create access key button.

Access key

  • Check the box and click the Create access key button, and then your keys will be created.

Access key

Successful

  • Open your VS code terminal and enter the command below:
aws configure
  • Now enter your access key ID and secret key.

Step 2: Create a Terraform Configuration File

  • Create a working directory with the command:
mkdir terraform-ec2

Directory

  • Change directory into it with the command:
cd terraform-ec2
  • Create a Terraform file to define the infrastructure (main.tf):
touch main.tf
  • Open main.tf and paste in the configuration below, and save the file.
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region  = "us-west-2"
}

resource "aws_instance" "app_server" {
  ami           = "ami-830c94e3"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
}

Configuration

Step 3: Initialize Terraform

After the configuration has been created run the command below to initialize the directory:

terraform init

Initialization

This downloads and installs the AWS plugins in the configuration

Step 4: Plan, Format, and Validate Configuration

  • Preview the configuration plan with the command below:
terraform plan

Preview plan

  • Format to automatically update the configurations in the terraform-ec2 directory for readability and consistency with the command:
terraform fmt
  • Verify syntax and internal consistency of the configurations with the command:
terraform validate

Validate

Step 5: Create the Infrastructure (EC2 instance)

  • Apply the configuration to create the EC2 instance with the command:
terraform apply

Apply configuration

  • Enter y or yes to confirm action.

Confirm application

Step 6: Inspect and Verify the EC2 Instance

  • Inspect the current state of the Instance using the command:
terraform show

Inspect

  • To verify the instance navigate to the AWS Management Console in EC2 Dashboard. Make sure you're in the right region us-west-2.

Verification

Step 7: Delete the EC2 Instance (After Use)

For best practice you can destroy the instance and resources with the command:

terraform destroy

Conclusion

After following all of these steps, you would have deployed an AWS EC2 instance with the use of Terraform and AWS CLI successfully.
NOTE: You can also SSH into the instance as long as you have a key pair associated with the command ssh -i my-key.pem ec2-user@<instance-public-ip>.

Thank you for reading my blog.😊😊

If you need further assistance, feel free to reach out in the comment or hit me up on Twitter. You can also follow me on GitHub. My DM's are open and love discussions on cloud topics!😁

Oluwatofunmi Emmanuel Oluwaloseyi

...

🔧 Build and Manage Cloud Infrastructure with Terraform and AWS CLI


📈 49.1 Punkte
🔧 Programmierung

🔧 STEPS TO CONFIGURE CLOUD INFRASTRUCTURE USING TERRAFORM AND AWS-CLI ON VISUAL CODE STUDIO


📈 34.97 Punkte
🔧 Programmierung

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


📈 34.97 Punkte
🔧 Programmierung

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


📈 34.97 Punkte
🔧 Programmierung

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


📈 34.97 Punkte
🔧 Programmierung

🔧 🛠️ DevOps Made Easy: Install AWS CLI, ECS CLI, Docker &amp; Terraform Using Chocolatey 🎯


📈 32.69 Punkte
🔧 Programmierung

🔧 AWS Cloud Path Week 2 - Set up CLI &amp; Understand AWS Global Infrastructure


📈 29.71 Punkte
🔧 Programmierung

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


📈 29.69 Punkte
🔧 Programmierung

🔧 How To Manage AWS Security Hub in AWS Organizations Using Terraform


📈 29.43 Punkte
🔧 Programmierung

🔧 Terraform Remote Backend: How to Manage Terraform State File for Easier Collaboration across Teams


📈 28.88 Punkte
🔧 Programmierung

🔧 Terraform for DevOps: Introduction to Terraform &amp; Infrastructure as Code (IaC) (Part 1)


📈 27.07 Punkte
🔧 Programmierung

🔧 Unveiling the Magic: How Terraform Works Under the Hood to Manage Your Infrastructure as Code


📈 26.31 Punkte
🔧 Programmierung

🔧 How to Manage On-premise Infrastructure with Terraform


📈 26.31 Punkte
🔧 Programmierung

🔧 Seamless Cloud Infrastructure: Integrating Terragrunt and Terraform with AWS


📈 26.18 Punkte
🔧 Programmierung

📰 Infrastructure as Code: Terraform 1.4 präsentiert CLI-Run-Ergebnisse im UI


📈 25.98 Punkte
📰 IT Nachrichten

🔧 Game Highlights Processor - AWS ECR, AWS ECS, AWS Elemental MediaConvert, Docker &amp; Terraform.


📈 25.52 Punkte
🔧 Programmierung

🔧 How to Import AWS RDS Instances into Terraform and Safely Manage Drift


📈 24.8 Punkte
🔧 Programmierung

🔧 Deploy and manage AWS Control Tower controls by using Terraform


📈 24.8 Punkte
🔧 Programmierung

🔧 Transforming Cloud Infrastructure with Terraform: Build, Change, Deploy


📈 24.8 Punkte
🔧 Programmierung

🔧 Transforming Cloud Infrastructure with Terraform: Build, Change, Deploy


📈 24.8 Punkte
🔧 Programmierung

🔧 Build Infrastructure with Terraform on Google Cloud


📈 24.8 Punkte
🔧 Programmierung

🔧 Infrastructure as Code (IaC) : Automatisation de l'Infrastructure avec Terraform et Ansible


📈 24.49 Punkte
🔧 Programmierung

🔧 Demystifying Infrastructure as Code: Provisioning Infrastructure with Terraform


📈 24.49 Punkte
🔧 Programmierung

🔧 Step-by-Step Guide to Setting Up Terraform, AWS CLI, and VS Code


📈 24.48 Punkte
🔧 Programmierung

🔧 Step-by-Step Guide to setting Up Terraform, AWS CLI, and VS Code


📈 24.48 Punkte
🔧 Programmierung

🔧 Step-by-Step Guide to Setting Up Terraform, AWS CLI, and VS Code


📈 24.48 Punkte
🔧 Programmierung

🔧 Step-by-Step Guide to Setting Up Terraform, AWS CLI, and VS Code


📈 24.48 Punkte
🔧 Programmierung

🔧 Step-by-Step Guide to Setting Up Terraform, AWS CLI, and VS Code


📈 24.48 Punkte
🔧 Programmierung

matomo