用于构建和部署 Next.js 应用到 AWS 的 Terraform 模块。支持 SSR (Lambda)、静态 (S3) 和 API (Lambda) 页面。
用于构建和部署 Next.js 应用到 AWS 的 Terraform 模块。支持 SSR (Lambda)、静态 (S3) 和 API (Lambda) 页面。
--- # Terraform Next.js module for AWS A zero-config Terraform module for self-hosting Next.js sites serverless on AWS Lambda. ## Features Some features are still under development, here is a list of features that are currently supported and what we plan to bring with the next releases: - ✅ Supports any version of [Next.js](https://nextjs.org/) - ✅ [Terraform](https://www.terraform.io/) `v0.15+` - ✅ Unlimited parallel deployments of Next.js apps (atomic deployments) - ✅ Static, SSG, Lambda and API pages (with [dynamic routes](https://nextjs.org/docs/routing/dynamic-routes)) - ✅ Automatic expiration of old static assets - ✅ [Rewrites](https://nextjs.org/docs/api-reference/next.config.js/rewrites) & [Redirects](https://nextjs.org/docs/api-reference/next.config.js/redirects) - ✅ [Image Component & Image Optimization](https://nextjs.org/docs/basic-features/image-optimization) support - [Incremental Static Regeneration](https://nextjs.org/docs/basic-features/data-fetching#incremental-static-regeneration) - ⛔️ [Middleware](https://nextjs.org/docs/advanced-features/middleware) (Not supported by Lambda@Edge / CloudFront functions) ## Architecture The Next.js Terraform module is designed as a full stack AWS app. It relies on multiple AWS services and connects them to work as a single application: ## Usage ### Prerequisites You should have the following tools installed: - [Terraform](https://www.terraform.io/downloads) - [Node.js](https://nodejs.org) - [Bash](https://www.gnu.org/software/bash/) & [curl](https://curl.se/) (Should be available by default on many Linux based images or macOS) > **Note:** Additionally we assume here that you already have a public [Route53 Hosted Zone](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/AboutHZWorkingWith.html) associated with your AWS account. > > This is a requirement in the preview phase of atomic deployments, where each deployment gets a unique subdomain assigned. > It will change once atomic deployments become generally available. ### Setup the Next.js Terraform module The Terraform module contains the system that is later used for creating new deployments and managing the aliases (domains) for your Next.js app(s). Creating the Terraform stack is only required on initial setup and creates the global resources (CloudFront distributions, DynamoDB tables, S3 storage) that is used for handling incoming requests to your website. Create a new `main.tf` file in an empty folder (or add it to your existing Terraform stack) and add the following content: ``` … ``` To create the resources in your AWS account, run the following commands: ```sh terraform init # Only needed on the first time running Terraform terraform plan # (Optional) See what resources Terraform will create terraform apply # Create the resources in your AWS account > Apply complete! > > Outputs: > > api_endpoint = "https://.execute-api.us-west-2.amazonaws.com" > api_endpoint_access_policy_arn = "arn:aws:iam::123456789012:policy/access-api" ``` The `api_endpoint` is later used by the CLI tool to create new deployments. With the `api_endpoint_access_policy_arn` AWS policy you can create new users (and assign that policy) that only can use the CLI tool `tf-next` but cannot access other resources inside of your AWS account. After the successful deployment your Next.js app is publicly available at the CloudFront subdomain from the `cloudfront_domain_name` output. ### Deploy a Next.js App For building and deploying Next.js apps to the system we created a CLI tool called [`tf-next`](https://www.npmjs.com/package/tf-next). It is a npm package that can be installed with: ```sh npm i -g tf-next@canary ``` Next, we need to build the Next.js so that it can run in a serverless environment (with AWS Lambda). This is archived by running `tf-next build` in the same directory where your Next.js app is located (Right where your `package.json` or `next.config.js` files are located): ``` tf-next build > All serverless functions created in: 20.791ms > 1752924 total bytes > Build successful! ``` Now deploy the Next.js app by running `tf-next deploy` from the same directory. The deploy command communicates through a secured (and authenticated with your AWS credentials) API with the Terraform module. To tell the command where to deploy the app, an additional `--endpoint` flag must be provided, which should use the value from the `api_endpoint` output from the `terraform apply` step: ``` tf-next deploy --endpoint https://.execute-api.us-west-2.amazonaws.com > Available at: https://3edade7a2bf7bb0343699af6b851bbfa.example.com/ ``` The preview deployment can now be accessed by the displayed url. To make the deployment available from a more readable url, you can use the `tf-next alias` subcommand: ``` tf-next alias set my-app.example.com 3edade7a2bf7bb0343699af6b851bbfa.example.com > Available at: https://my-app.example.com/ ``` For a full list of available commands that can be used with `tf-next`, check the [command reference](https://github.com/milliHQ/terraform-aws-next-js/blob/main/packages/tf-next/README.md). ## Examples - [Atomic Deployments](https://github.com/milliHQ/terraform-aws-next-js/tree/main/examples/atomic-deployments) Each deployment gets a unique url from where it can be previewed. - [Complete](https://github.com/milliHQ/terraform-aws-next-js/tree/main/examples/complete) Complete example with SSR, API and static pages. - [Static](https://github.com/milliHQ/terraform-aws-next-js/tree/main/examples/static) Example that uses static pages only (No SSR). - [Next Image](https://github.com/milliHQ/terraform-aws-next-js/tree/main/examples/next-image) Images are optimized on the fly by AWS Lambda. - [Existing CloudFront](https://github.com/milliHQ/terraform-aws-next-js/tree/main/examples/with-existing-cloudfront) Use the module together with an existing CloudFront distribution that can be fully customized. - [Custom Domain](https://github.com/milliHQ/terraform-aws-next-js/tree/main/examples/with-custom-domain) Use the module with your own domain from Route 53. ## Requirements | Name | Version | |------|---------| | terraform | >= 0.15 | | aws | >= 4.8 | ## Providers | Name | Version | |------|---------| | aws | >= 4.8 | ## Inputs | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | cloudfront\_acm\_certificate\_arn | ACM certificate arn for custom\_domain | `string` | `null` | no | | cloudfront\_aliases | Aliases for custom\_domain | `list(string)` | `[]` | no | | cloudfront\_cache\_key\_headers | Header keys that should be used to calculate the cache key in CloudFront. | `list(string)` |Note
The main branch currently contains the atomic deployments alpha preview.
For the lastest stable release, check out thev0.xbranch.Please see our blog post "The road to Atomic Deployments"
or watch the latest release review for more information:
[| no | | cloudfront\_create\_distribution | Controls whether the main CloudFront distribution should be created. | `bool` | `true` | no | | cloudfront\_external\_arn | When using an external CloudFront distribution provide its arn. | `string` | `null` | no | | cloudfront\_external\_id | When using an external CloudFront distribution provide its id. | `string` | `null` | no | | cloudfront\_minimum\_protocol\_version | The minimum version of the SSL protocol that you want CloudFront to use for HTTPS connections. One of SSLv3, TLSv1, TLSv1\_2016, TLSv1.1\_2016, TLSv1.2\_2018 or TLSv1.2\_2019. | `string` | `"TLSv1"` | no | | cloudfront\_origin\_request\_policy | Id of a custom request policy that overrides the default policy (AllViewer). Can be custom or managed. | `string` | `null` | no | | cloudfront\_price\_class | Price class for the CloudFront distributions (main & proxy config). One of PriceClass\_All, PriceClass\_200, PriceClass\_100. | `string` | `"PriceClass_100"` | no | | cloudfront\_response\_headers\_policy | Id of a response headers policy. Can be custom or managed. Default is empty. | `string` | `null` | no | | cloudfront\_webacl\_id | An optional webacl2 arn or webacl id to associate with the cloudfront distribution | `string` | `null` | no | | create\_image\_optimization | Controls whether resources for image optimization support should be created or not. | `bool` | `true` | no | | debug\_use\_local\_packages | Use locally built packages rather than download them from npm. | `bool` | `false` | no | | deployment\_name | Identifier for the deployment group (only lowercase alphanumeric characters and hyphens are allowed). | `string` | `"tf-next"` | no | | enable\_multiple\_deployments | Controls whether it should be possible to run multiple deployments in parallel (requires multiple\_deployments\_base\_domain). | `bool` | `false` | no | | image\_optimization\_lambda\_memory\_size | Amount of memory in MB the worker Lambda Function for image optimization can use. Valid value between 128 MB to 10,240 MB, in 1 MB increments. | `number` | `2048` | no | | lambda\_attach\_policy\_json | Whether to deploy additional lambda JSON policies. If false, lambda\_policy\_json will not be attached to the lambda function. (Necessary since policy strings are only known after apply when using Terraforms data.aws\_iam\_policy\_document) | `bool` | `false` | no | | lambda\_attach\_to\_vpc | Set to true if the Lambda functions should be attached to a VPC. Use this setting if VPC resources should be accessed by the Lambda functions. When setting this to true, use vpc\_security\_group\_ids and vpc\_subnet\_ids to specify the VPC networking. Note that attaching to a VPC would introduce a delay on to cold starts | `bool` | `false` | no | | lambda\_policy\_json | Additional policy document as JSON to attach to the Lambda Function role | `string` | `null` | no | | lambda\_role\_permissions\_boundary | ARN of IAM policy that scopes aws\_iam\_role access for the lambda | `string` | `null` | no | | multiple\_deployments\_base\_domain | Default wildcard domain where new deployments should be available. Should be in the form of *.example.com. | `string` | `null` | no | | tags | Tag metadata to label AWS resources that support tags. | `map(string)` | `{}` | no | | tags\_s3\_bucket | Tag metadata to label AWS S3 buckets. Overrides tags with the same name in input variable tags. | `map(string)` | `{}` | no | | vpc\_security\_group\_ids | The list of Security Group IDs to be used by the Lambda functions. lambda\_attach\_to\_vpc should be set to true for these to be applied. | `list(string)` | `[]` | no | | vpc\_subnet\_ids | The list of VPC subnet IDs to attach the Lambda functions. lambda\_attach\_to\_vpc should be set to true for these to be applied. | `list(string)` | `[]` | no | ## Outputs | Name | Description | |------|-------------| | api\_endpoint | API endpoint that is used by the CLI. | | api\_endpoint\_access\_policy\_arn | ARN of the policy that grants access to the API endpoint. | | cloudfront\_custom\_error\_response | Preconfigured custom error response the CloudFront distribution should use. | | cloudfront\_default\_cache\_behavior | Preconfigured default cache behavior the CloudFront distribution should use. | | cloudfront\_default\_root\_object | Preconfigured root object the CloudFront dist
"Authorization"
]
暂无开放 Issues,或尚未同步最近议题。