Customize modules with object attributes
Terraform modules let you organize and re-use Terraform configuration. They make your infrastructure deployments consistent and help your team adhere to your organization's best practices. Input variables let module users customize attributes of the module. You can define module attributes using strings, numbers, booleans, lists, maps, and objects.
Object type attributes contain a fixed set of named values of different types. Using objects in your modules lets you group related attributes together, making it easier for users to understand how to use your module. You can make attributes within objects optional, which make it easier for you to ship new module versions without changing the variables that module users need to define.
In this tutorial, you will refactor a module to use objects for some of its attributes.
Prerequisites
You can complete this tutorial using the same workflow with either Terraform Community Edition or HCP Terraform. HCP Terraform is a platform that you can use to manage and execute your Terraform projects. It includes features like remote state and execution, structured plan output, workspace resource summaries, and more.
Select the Terraform Community Edition tab to complete this tutorial using Terraform Community Edition.
This tutorial assumes that you are familiar with the Terraform and HCP Terraform workflows. If you are new to Terraform, complete the Get Started collection first. If you are new to HCP Terraform, complete the HCP Terraform Get Started tutorials first.
For this tutorial, you will need:
- Terraform v1.3+ installed locally.
- an HCP Terraform account and organization.
- HCP Terraform locally authenticated.
- the AWS CLI.
- an HCP Terraform variable set configured with your AWS credentials.
Clone the example repository
Clone the example repository for this tutorial, which contains Terraform configuration for an AWS S3 bucket configured to host a static website.
Change into the repository directory.
Review example configuration
The example configuration uses a local module to provision an AWS S3 bucket
configured to host a static website. The modules/aws-s3-static-website
directory contains the module definition, while the configuration that uses it
is in main.tf
in the repository's root directory.
Open modules/aws-s3-static-website/main.tf
to review the module configuration.
This module includes resources that manage the files your website will serve and
how it will respond to requests.
The configuration sets the index and error document for your website.
Note
S3 stores data as objects, which are roughly equivalent to files on a local system. Web servers and browsers often use the term "document". For simplicity, this tutorial uses the generic term "file".
The configuration uses the hashicorp/dir/template
public module to
render the files that Terraform will upload to the S3 bucket. Terraform will
load these files from the path specified in the www_path
variable, or from the
modules/aws-s3-static-website/www
directory if the variable is not set.
Open modules/aws-s3-static-website/variables.tf
to review the input variables
that configure this module's attributes.
This configuration lets users specify a bucket name, or a prefix that Terraform will use to generate a unique name. It also allows users to define tags for their buckets. Finally, it includes several variables that allow them to configure the files in their bucket:
- the
index_document_suffix
anderror_document_key
variables control which files the website will use for its index and error documents, respectively. - the
www_path
variable allows users to specify a path from which to load the files for the website. - the
terraform_managed_files
variable is a flag that allows users to manage files outside of Terraform.
Open main.tf
to review the initial configuration, which uses the
aws-s3-static-website
module to provision an S3 bucket and related resources.
Apply configuration
Set the TF_CLOUD_ORGANIZATION
environment variable to your HCP Terraform
organization name. This will configure your HCP Terraform integration.
Initialize your configuration. Terraform will automatically create the
learn-terraform-module-object-attributes
workspace in your HCP Terraform
organization.
Note
This tutorial assumes that you are using a tutorial-specific HCP Terraform organization with a global variable set of your AWS credentials. Review the Create a Credential Variable Set for detailed guidance. If you are using a scoped variable set, assign it to your new workspace now.
Apply the configuration. Respond to the confirmation prompt with a yes
to
create your resources.
Visit the domain in the website_bucket_endpoint
output value to confirm that
your website responds with "Nothing to see here."
Refactor module with object attribute
Open modules/aws-s3-static-website/variables.tf
and delete the four
variables relating to files: index_document_suffix
, error_document_key
,
www_path
, and terraform_managed_files
.
Replace these variables with a new variable that captures all file-related options.
The files
variable defines an object with fields corresponding to the
variables you removed. Since it does not set a default value, it is required
whenever practitioners use your module. Objects map a specific set of named keys
to values. Keeping related attributes in a single object helps your users
understand how to use your module.
The terraform_managed
field is required, while the other three are optional.
Both error_document_key
and index_document_suffix
fields configure default
values for the attributes after specifying that they are optional. Since no
default value is set for www_path
, Terraform will set it to null
, unless the
module user specifies a value for it.
Update modules/aws-s3-static-website/main.tf
to use the files
object instead
of the individual variables. First replace the index and error document
variables in the aws_s3_bucket_website_configuration.web
resource.
Next, replace the www_path
and terraform_managed_files
variables in the
module.template_files
and aws_s3_object.web
configuration blocks.
Now downstream users of your module can control how Terraform manages the contents of their bucket in a few ways. When they use your module, they can:
Manage the files outside of Terraform by setting
terraform_managed
tofalse
. This allows web developers to manage the contents of the website with tools other than Terraform:Either use the default files (in
modules/aws-s3-static-website/www
) or specify their own path withwww_path
:Use different index and error documents by configuring
index_document_suffix
anderror_document_key
:
Update the module block in main.tf
in the root repository directory to
use the new files
input variable. Because you set the www_path
attribute on
the files
object, Terraform will replace the website contents with the files
in the www
directory under the repository's root directory.
Apply your configuration. Respond to the confirmation prompt with yes
.
Terraform will replace the contents of your bucket with the files in the www
sub-directory. These files contain a simple Tetris-like game.
Visit the domain given in the website_bucket_endpoint
output value in your
browser, which now responds with a playable Tetris-like game.
Use a list of objects to configure CORS
Cross-Origin Resource Sharing (CORS) allows web developers to control where and
how users access resources in their website. CORS configuration limits access to
websites based on request headers, method, or originating domain. Add a new
variable to modules/aws-s3-static-website/variables.tf
to control your S3
bucket's CORS configuration.
The cors_rules
variable contains a list of objects. Since the default value is
an empty list ([]
), users do not need to set this input variable to deploy the
module. When they do use it, they must set allowed_methods
and
allowed_origins
for each object in the list; the other attributes are
optional. This matches the behavior of the aws_s3_bucket_cors_configuration
resource you will use to configure CORS.
Use the cors_rules
variable by adding a new resource to
modules/aws-s3-static-website/main.tf
.
This resource uses the dynamic
block to create a cors_rule
block for each
item in the var.cors_rules
list. When the list is empty, the count
meta-argument will evaluate to 0
, and Terraform will not provision this
resource. Otherwise, the dynamic
block will create a CORS rule for each object
in the list. Since optional object attributes default to null
, Terraform will
not set values for them unless the module user specifies them.
Update the module block in main.tf
in the repository root directory to use the
new variable. These example rules limit PUT
and POST
requests to an example
domain, and permit GET
requests from anywhere.
Apply this change to configure CORS for your bucket. Respond to the confirmation
prompt with yes
. Terraform will report the new CORS resource it created for
your bucket.
Clean up your infrastructure
Remove your bucket and related resources. Respond to the confirmation prompt
with a yes
.
If you used HCP Terraform for this tutorial, after destroying your resources,
delete the learn-terraform-module-object-attributes
workspace from your
HCP Terraform organization.
Next steps
In this tutorial, you refactored the aws-s3-static-website
module to group
attributes that configure a website bucket into a single object variable. You
also added the ability to configure CORS with a list of objects. Defining module
attributes as objects will make it easier for module users to understand how
your module works, and let you update the module without changing its required
input variables. Review the following resources to learn more about using and
creating modules with Terraform.
- Learn how to Create Dynamic Expressions to help make your Terraform configurations more dynamic and flexible.
- Create composable, shareable, and reusable modules with Module Creation - Recommended Pattern.
- Read the documentation for optional object type attributes.