Using Terraform Modules from AWS S3 Buckets
Modules are used in Terraform to modularize and encapsulate groups of resources in your infrastructure. Using Modules you can write a generic code and reuse it as you need if you are from database background it is similar to using stored procedures.

Terraform provides a various way to use Modules, you can write module and use it from many sources, similar to using files from Shared Drives or services like DropBox and Google Drive.

The module installer supports installation from a number of different source types, as listed below.

Local paths

Terraform Registry

GitHub

Bitbucket

Generic Git, Mercurial repositories

HTTP URLs

S3 buckets

To learn more about Modules check this Terraform documentation link

In this post, we will see how to use Module from S3 buckets

Prerequisites

Create S3 bucket in your AWS account, the bucket could be public or private

Make sure your Terraform user (the account you are using to run terraform CLI) has access to the bucket

Solution

Note: Download the working example from our GitHub repository

Following is the source of the basic module, the module will create an S3 bucket by taking a bucket name as a variable

variable "s3-bucket-name" {

description = "Name of the S3 bucket"

} resource "aws_s3_bucket" "s3-module" {

bucket = "${var.s3-bucket-name}"

acl = "private"

}

Write your module and ZIP all files as one file for example s3-module.zip

Make sure you select all files of your module then zip it, Terraform would not recognize the module if you zip the directory, you might get the following error

Error downloading modules: Error loading modules: module s3-module: No Terraform configuration files found in directory: .terraform/modules/b06e8e34227215983c85107e22cc01

Upload the ZIP file to your S3 bucket

Copy the URL of the Module ZIP file and use it as a source of your Modules as follows

provider "aws" {

region = "us-east-1"

} module "bucket-1" { #Replace the URL with the link of your module source = "s3::https://s3.amazonaws.com/my-terraform-modules/s3-module.zip" s3-bucket-name = "my-bucket-no-1"

}

Execute the following commands to use the modules

$ terraform get $ terraform init $ terraform plan $ terraform apply $ terraform destroy

Hope you find this post useful, please leave a comment for any questions or suggestions.

@IamZeeshanBaig

About DataNext

DataNext Solutions is a US based system integrator, specialized in Cloud, Big Data, DevOps technologies. As a registered AWS partner, our services comprise of any Cloud Migration, Cost optimization, Integration, Security and Managed Services. Click here and Book Free assessment call with our experts today or visit our website www.datanextsolutions.com for more info.
['Zeeshan Baig']
['AWS', 'DevOps', 'Infrastructure', 'Terraform', 'Cloud Computing']