Creating AWS Resource by Terraform

Here's an example of creating AWS resources using Terraform.

Step 1. Install Terraform.

Download and install Terraform from the official website.

Step 2. Configure AWS Provider.

You need to add an AWS Provider to create AWS resources. Create a terraform file named “main.tf” in the root of a directory you want to use. File names completely depend on your imagination. Terraform will grab all the files in the same directory.

# Configure AWS Provider
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# AWS Provider configuration
provider "aws" {
  region = "eu-west-1"
}

To download necessary plugins, configuration, etc., you should call terraform init in CLI.

Step 3. Create AWS Resources.

I will focus on only creating an EC2 instance, s3bucket, instead of many resources with complex dependencies.

# Create EC2 Instance
resource "aws_instance" "example" {
  ami           = "ami-0c94855ba95c71c5"
  instance_type = "t2.micro"
}

# Create S3 Bucket
resource "aws_s3_bucket" "example" {
  bucket = "my-tf-bucket"
  acl    = "private"
}

Step 4. Initialize Terraform.

Run the following command to initialize Terraform.

terraform init

Step 5. Apply Terraform Configuration.

Run the following command to apply the Terraform configuration.

terraform apply

This will create the specified AWS resources.

Step 6. Verify Resources.

Verify that the resources have been created successfully by checking the AWS Management Console or using the AWS CLI.

AWS Management Console


Similar Articles