How to Upload Local Files to Amazon EC2 Instance Using Terraform

 

 

Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to automate cloud infrastructure provisioning. When working with Amazon EC2 instances, you may need to upload local files to the instance. In this guide, we will walk through how to upload local files to an Amazon EC2 instance using Terraform. Whether you are using AWS directly or working on a Windows VPS UK, this tutorial will help you automate the process efficiently.

Step 1: Install Terraform

Before you start, ensure that Terraform is installed on your local machine. You can download Terraform from the official website or use your system’s package manager:

sudo apt-get install terraform

Verify that Terraform is installed correctly:

terraform --version

Step 2: Set Up AWS Credentials

For Terraform to interact with your AWS account, it needs access to your AWS credentials. You can configure the AWS CLI to store your credentials, or manually create an access key and secret key from the AWS Management Console.

aws configure

Enter your AWS Access Key ID and Secret Access Key when prompted.

Step 3: Define Your EC2 Instance in Terraform

Create a new directory for your Terraform project and navigate to it:

mkdir terraform-ec2
cd terraform-ec2

Inside this directory, create a new main.tf file to define your EC2 instance:

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

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "TerraformEC2"
  }

  connection {
    type        = "ssh"
    user        = "ec2-user"
    private_key = file("~/.ssh/id_rsa")
    host        = self.public_ip
  }
}

This file defines a simple EC2 instance in the "us-west-2" region. Make sure to replace the AMI ID with an AMI available in your preferred region.

Step 4: Upload Local Files Using Provisioners

To upload files from your local machine to the EC2 instance, you can use the file provisioner in Terraform. Modify your main.tf file to include the following code:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "TerraformEC2"
  }

  connection {
    type        = "ssh"
    user        = "ec2-user"
    private_key = file("~/.ssh/id_rsa")
    host        = self.public_ip
  }

  provisioner "file" {
    source      = "path/to/local/file.txt"
    destination = "/home/ec2-user/file.txt"
  }

  provisioner "remote-exec" {
    inline = [
      "sudo chmod 600 /home/ec2-user/file.txt"
    ]
  }
}

In this example, the file provisioner uploads a local file (replace path/to/local/file.txt with your file path) to the EC2 instance at /home/ec2-user/file.txt. The remote-exec provisioner runs a command on the instance to change the file's permissions.

Step 5: Apply the Terraform Configuration

Initialize Terraform in your project directory to download the necessary providers:

terraform init

Next, apply the configuration to provision the EC2 instance and upload your local files:

terraform apply

Confirm the action by typing yes when prompted. Terraform will create the EC2 instance and upload the specified files to the instance.

Step 6: Verify File Upload

After the EC2 instance is created, you can SSH into the instance to verify that the file was successfully uploaded:

ssh -i ~/.ssh/id_rsa ec2-user@

Once logged in, check the file at the specified destination:

  • 0 användare blev hjälpta av detta svar
Hjälpte svaret dig?

Relaterade artiklar

Boost Your Ubuntu System's Performance with a Swap File: A Step-by-Step Guide

What is a Swap File? A swap file in Ubuntu serves as dedicated virtual memory on your hard...

How to Migrate ISPConfig 2, ISPConfig 3.x, Confixx, CPanel or Plesk to ISPConfig 3.2 (single server)

Introduction Migration from other control panels like ISPConfig 2, ISPConfig 3.x, Confixx,...

How to Install and Configure Zabbix Server and Client on Rocky Linux 9

Introduction Zabbix is an open-source monitoring solution that provides real-time...

How to Install CockroachDB Cluster on Debian 12

Introduction CockroachDB is a distributed SQL database built to handle large-scale,...

How to Install Joomla with Apache and Let's Encrypt SSL on AlmaLinux 9

Introduction Joomla is a popular open-source content management system (CMS) used to build...