Open Next Terraform
This module deploys a next.js website using Open Next to AWS utilising lambda, S3 and CloudFront.
This module will build the corresponding resources to host the single-zone or multi-zone website; several options exist to deploy the backend. The options are:
- Lambda function URLs (with no auth)
- HTTP API Gateway (with proxy integrations to lambda functions)
- Lambda@edge (server function only)
NOTE: If lambda@edge is used, then the warmer function is not deployed
The script to invalidate the CloudFront distribution uses bash, AWS CLI and jq. The invalidation script and Terraform apply will fail if the script fails to run.
To use ISR you need to use at least 2.x of Open Next. If you are using 1.x, please add the following to your Terraform/ Terragrunt configuration
...
isr = {
create = false
}
...
The module is available in the Terraform registry
Examples
The examples have been moved to a separate repository to reduce the amount of code that Terraform downloads. You can find them at terraform-aws-open-next-examples repo
Module documentation
Below is the documentation for the Terraform module, outlining the providers, modules and resources required to deploy the website. The documentation includes the inputs that can be supplied (including any defaults) and what is outputted from the module.
NOTE: The module will zip all the necessary open-next artefacts as part of a Terraform deployment. To facilitate this, the .open-next folders need to be stored locally.
You must configure the AWS providers four times because some organisations use different accounts or roles for IAM, DNS, etc. The module has been designed to cater for these requirements. The server function is a separate provider to allow your backend resources to be deployed to a region, i.e. eu-west-1, and deploy the server function to another region, i.e. us-east-1, for lambda@edge.
Below is an example setup.
provider "aws" {
}
provider "aws" {
alias = "server_function"
}
provider "aws" {
alias = "iam"
}
provider "aws" {
alias = "dns"
}
Requirements
Name | Version |
---|---|
terraform | >= 1.4.0 |
archive | >= 2.3.0 |
aws | >= 4.67.0 |
Providers
Name | Version |
---|---|
archive | >= 2.3.0 |
aws | >= 4.67.0 |
aws.dns | >= 4.67.0 |
terraform | n/a |
Modules
Name | Source | Version |
---|---|---|
image_optimisation_function | ./modules/tf-aws-lambda | n/a |
revalidation_function | ./modules/tf-aws-lambda | n/a |
server_function | ./modules/tf-aws-lambda | n/a |
warmer_function | ./modules/tf-aws-scheduled-lambda | n/a |
Resources
Inputs
Name | Description | Type | Default | Required |
---|---|---|---|---|
cache_control_immutable_assets_regex | Regex to set public,max-age=31536000,immutable on immutable resources | string |
`"^.*(\.js | \.css |
cloudfront | Configuration for the CloudFront distribution | object({ |
{} |
no |
cloudwatch_log | Override the Cloudwatch logs configuration | object({ |
{ |
no |
content_types | The MIME type mapping and default for artefacts generated by Open Next | object({ |
{} |
no |
domain | Configuration to for attaching a custom domain to the CloudFront distribution | object({ |
{} |
no |
iam | Override the default IAM configuration | object({ |
{} |
no |
image_optimisation_function | Configuration for the image optimisation function | object({ |
{} |
no |
isr | Configuration for ISR, including creation and function config. To use ISR you need to use at least 2.x of Open Next, for 1.x please set create to false | object({ |
{ |
no |
open_next | The next.js website config for single and multi-zone deployments | object({ |
n/a | yes |
preferred_architecture | Preferred instruction set architecture for the lambda function. If lambda@edge is used for the server function, the architecture will be set to x86_64 for that function | string |
"arm64" |
no |
prefix | A prefix which will be attached to the resource name to ensure resources are random | string |
null |
no |
server_function | Configuration for the server function | object({ |
{} |
no |
suffix | A suffix which will be attached to the resource name to ensure resources are random | string |
null |
no |
vpc | The default VPC configuration for the lambda resources. This can be overridden for each function | object({ |
null |
no |
warmer_function | Configuration for the warmer function | object({ |
{ |
no |
Outputs
Name | Description |
---|---|
cloudfront_url | The URL for the cloudfront distribution |
domain_names | The custom domain names attached to the cloudfront distribution |