Using Terraform Modules from AWS S3 Buckets

Image for post
Image for post

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

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

Solution

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

Originally published at datanextsolutions.com on October 28, 2018.

Written by

Cloud Security Expert & CEO of DataNext Solutions, helping people every day with the latest tech. Connect @LinkedIn http://bit.ly/zb-linkedin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store