Using Terraform Modules from AWS S3 Buckets

Zeeshan Baig
Oct 28, 2018 · 2 min read
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.

DataNext Solutions

Cloud | Security | DevOps | Big Data

Zeeshan Baig

Written by

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

DataNext Solutions

DataNext Solutions is a US based system integrator, specialized in Cloud, Big Data, DevOps technologies. As a registered AWS partner, our services comprise any Cloud Migration, Cost optimization, Integration, Security and Managed Services.

Zeeshan Baig

Written by

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

DataNext Solutions

DataNext Solutions is a US based system integrator, specialized in Cloud, Big Data, DevOps technologies. As a registered AWS partner, our services comprise any Cloud Migration, Cost optimization, Integration, Security and Managed Services.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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