Thursday, July 18, 2024

  Steps to install terraform on Ubuntu / Ubuntu cloud server :


 1.       Install unzip

sudo apt-get install unzip

2.  Download latest version of the terraform (substituting newer version number if needed)

wget https://releases.hashicorp.com/terraform/1.0.7/terraform_1.0.7_linux_amd64.zip

3.  Extract the downloaded file archive

unzip terraform_1.0.7_linux_amd64.zip

4. Move the executable into a directory searched for executables

sudo mv terraform /usr/local/bin/

5. Run it

terraform --version


CREATE EC2 WITH TERRAFORM

To create an EC2 instance with Terraform using the provided AWS credentials, follow these steps. Be sure to replace the placeholder values with your actual credentials and ensure your credentials are kept secure.

Step 1: Setup Terraform Project Directory

  1. Create a Project Directory:

mkdir my-ec2-instance

cd my-ec2-instance

 

2.      Create main.tf File: Open your favorite text editor and create a file named main.tf.

Step 2: Define the AWS Provider and EC2 Instance

In the main.tf file, define the AWS provider and the EC2 instance resource:

# main.tf

# Define the AWS provider
provider "aws" {
  region     = "us-east-1"
  access_key = "AKIAU6GDXLT5RR3WZ755"
  secret_key = "4pllcY6LbEkPJHCpqwZUUq7C6+dqui6HDQY1srcF"
}

# Define the EC2 instance resource
resource "aws_instance" "example" {
  ami           = "ami-04b70fa74e45c3917"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleInstance"
  }
}


2.       Initialize Terraform: Initialize your Terraform configuration. This step downloads the necessary providers.

terraform init

3.       Plan the Configuration: Create an execution plan, which lets you preview the changes that Terraform will make to your infrastructure.

terraform plan

4.       Apply the Configuration: Apply the configuration to create the EC2 instance.

terraform apply

5.       Confirm Apply: When prompted, type yes to confirm the apply step.

 

After testing, if you want to delete the created resources, you can use:

terraform destroy

This will remove all the resources defined in your Terraform configuration.

This basic setup gets you started with Terraform and AWS EC2 instances. You can expand and customize it as needed for your specific requirements.


Multiple instances

# Define the EC2 instance resource

resource "aws_instance" "example" {

  count         = 5

  ami           = "ami-0862be96e41dcbf74"

  instance_type = "t2.micro"

 

  tags = {

    Name = "ExampleInstance-${count.index}"

  }

}


No comments:

Post a Comment

  PROMETHEUS AND GRAFANA A robust performance monitoring and alerting stack is crucial to service reliability. Cloud Native Computing Foun...