Serverless plugin to reference outputs from Terraform
serverless-terraform-outputs
Serverless framework plugin to reference outputs from Terraform.
At a Glance
- It allows nested attributes to be picked from objects.
- The picked attribute must be a non-null value.
- It doesn't support picking items from arrays.
- It's an ESM-only module.
Referencing Terraform outputs in Serverless Framework has been addressed multiple times using various approaches and package names. This is my attempt to solve the problem with what I believe is the fastest implementation. Additionally, I aimed to maintain it as an ESM-only module in the JavaScript Registry.
Pre-requisites
- Serverless framework
v3or higher - Node.js version
v20or higher - Ensure the
terraformcommand is available in your PATH
Installation
This plugin is an ESM-only module available on the JavaScript Registry for the Node.js runtime.
npx jsr add --save-dev @halfstack/serverless-terraform-outputs
Add the plugin to your serverless.yml configuration:
plugins: - '@halfstack/serverless-terraform-outputs'
Configuration
The plugin can be configured using the ServerlessTerraformOutputs option.
path
Optionally, specify the path to the Terraform files. Otherwise, the plugin assumes the current working directory.
Here is the configuration if the files are located in a directory named infra:
├── project ├── infra ├── src └── serverless.yml
# serverless.yml custom: ServerlessTerraformOutputs: path: './infra'
Usage
The reference in the serverless.yml file should begin with the ${TF:} prefix, followed by the path to the
variable in the Terraform outputs. Here's an example:
# /infra/main.tf output "user-uploads-bucket-name" { value = aws_s3_bucket.user-uploads-bucket.bucket } output "main" { value = { stage = "dev" region = "eu-west-1" identityPool = { name = "hello-world" url = "https://example.com" } tags = ["tag-1", "tag-2"] } }
# serverless.yml service: some-service provider: name: aws stage: ${TF:main.stage} region: ${TF:main.region} custom: tags: ${TF:main.tags} functions: hello: handler: handler.hello environment: userUploadsBucket: ${TF:user-uploads-bucket-name} identityPoolUrl: ${TF:main.identityPool.url}
License
MIT
Credits
I read these projects, which solved the exact problem this plugin addresses, to understand their approach. However, this plugin has taken a different implementation approach.
Add Package
pnpm i jsr:@halfstack/serverless-terraform-outputs
pnpm dlx jsr add @halfstack/serverless-terraform-outputs
Import symbol
import * as serverless_terraform_outputs from "@halfstack/serverless-terraform-outputs";
Add Package
yarn add jsr:@halfstack/serverless-terraform-outputs
yarn dlx jsr add @halfstack/serverless-terraform-outputs
Import symbol
import * as serverless_terraform_outputs from "@halfstack/serverless-terraform-outputs";
Add Package
vlt install jsr:@halfstack/serverless-terraform-outputs
Import symbol
import * as serverless_terraform_outputs from "@halfstack/serverless-terraform-outputs";
Add Package
npx jsr add @halfstack/serverless-terraform-outputs
Import symbol
import * as serverless_terraform_outputs from "@halfstack/serverless-terraform-outputs";