Creating custom Terraform module - creating submodule for s3 bucket resource from the AWS provider.

Hello, readers. Today we will look into creating custom modules using Terraform. And in this blog, we will look into creating a submodule for the s3 bucket resource from the AWS provider. The code exists with double inverted commas, within which you need to edit your own required names for the buckets and various other things.

Let's start with the process.

Module Structure

Terraform treats any local directory referenced in the source argument of a module block as a module. A typical file structure for a new module is:

.├── LICENSE├── README.md├── main.tf├── variables.tf├── outputs.tf


Creating Module

You'll need to create the directory for your module. Inside your existing configuration directory, create a directory called modules, with a directory called aws-s3-static-website-bucket inside of it. And inside them, you can create the empty files as shown in the structure section above. Thus, now your skeleton should look like this:

modules└── aws-s3-static-website-bucket ├── LICENSE ├── README.md ├── main.tf ├── outputs.tf ├── variables.tf └── www

Add an S3 bucket resource to main.tf inside the modules/aws-s3-static-website-bucket directory:


This configuration creates a public S3 bucket hosting a website with an index page and an error page.

Now, define the following variables  invariables.tf inside the modules/aws-s3-static-website-bucket directory:


When creating a module, consider which resource arguments to expose to module end-users as input variables. For example, you might decide to expose the index and error documents to end users of this module as variables, but not define a variable to set the ACL , since to host a website your bucket will need the ACL to be set to "public-read".

You should also consider which values to add as outputs, since outputs are the only supported way for users to get information about resources configured by the module.

Add outputs to your module in the outputs.tf file inside the modules/aws-s3-static-website-bucket directory:


Now that you have created your module, return to the module folder, create another main.tf in your root module and add a reference to the new module:

AWS S3 Buckets must be globally unique. Because of this, you will need to replace <UNIQUE BUCKET NAME> with a unique, valid name for an S3 bucket.


Define Outputs

Earlier, you added several outputs to the aws-s3-static-website-bucket module, making those values available to your root module configuration.

Add these values as outputs to your root module by adding the following to outputs.tf file that you create in your root module directory (not the one in modules/aws-s3-static-website-bucket).


Initialzing/Installing Terraform

Whenever you add a new module to a configuration, Terraform must install the module before it can be used. Both the terraform get and terraform init commands will install and update modules. The terraform init command will also initialize backends and install plugins.

Now install the module by running terraform init.

Run terraform apply to provision your bucket.


Upload Files to the Bucket

You have now configured and used your own module to create a static website. You may want to visit this static website. Right now there is nothing inside your bucket, so there would be nothing to see if you visit the bucket's website. In order to see any content, you will need to upload objects to your bucket, with the following set of commands:

aws s3 cp modules/aws-s3-static-website-bucket/www/ s3://$(terraform output -raw website_bucket_name)/ --recursive

The website domain was shown when you last ran terraform apply, or whenever you run terraform output.

Visit the website domain in a web browser, and you will see the website contents.

https://<YOUR BUCKET NAME>.s3-us-west-2.amazonaws.com/index.html


Thanks for reading the post!

Comments

Popular posts from this blog

How Unilever Benefitted by Transforming into Cloud-Based Digital-Market Empire