npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@awboost/cfn-template-builder

v0.4.3

Published

Zero-dependency package for building AWS CloudFormation Templates

Downloads

329

Readme

@awboost/cfn-template-builder

Zero-dependency package for building AWS CloudFormation Templates.

Raw Template Types

TypeScript types are available for the plain AWS CloudFormation Template by importing from @awboost/cfn-template-builder/template. For performance reasons (there are thousands of resource types), the resource properties are not strongly typed. Strong types for resources are available from @awboost/cfn-resource-types.

import { Template } from "@awboost/cfn-template-builder/template";

const template: Template = {
  Resources: {
    MyFunction: {
      // resource types are not strongly typed here
      Type: "AWS::Lambda::Function",
      Properties: {
        Code: {
          S3Bucket: "my-bucket",
          S3Key: "code",
        },
        Role: "arn:etc:...:...",
      },
    },
  },
};

Building a Stack

A template can be constructed by combining one or more template items using the Stack class:

import { Stack } from "@awboost/cfn-template-builder/stack";
import { Parameter } from "@awboost/cfn-template-builder/template/Parameter";
import { LambdaFunction } from "@awboost/cfn-resource-types/AWS-Lambda-Function";

const stack = new Stack();

const bucketParam = stack.use(new Parameter("CodeBucket", "String"));
const objectParam = stack.use(new Parameter("CodeKey", "String"));

const lambda = stack.use(
  new LambdaFunction("MyFunction", {
    // properties have accurate types now
    Code: {
      S3Bucket: bucketParam.ref,
      S3Key: objectParam.ref,
    },
    Role: "arn:etc:...:...",
  }),
);

// now write the JSON template to the given path
const emitter = new FileSystemAssetEmitter({
  outputDirectory: "/path/to/output",
});

await stack.build(emitter, { templateFileName: "template.json" });

Using Assets

Additional files can be used by the stack by using the Asset class. These will automatically be output alongside the template when the stack is emitted.

import { Stack } from "@awboost/cfn-template-builder/stack";
import { Asset } from "@awboost/cfn-template-builder/template/Asset";
import { LambdaFunction } from "@awboost/cfn-resource-types/AWS-Lambda-Function";

const stack = new Stack();

const codeAsset = Asset.fromFile("LambdaCode", "my-code.zip");

const lambda = stack.use(
  new LambdaFunction("MyFunction", {
    // properties have accurate types now
    Code: codeAsset.ref,
    Role: "arn:etc:...:...",
  }),
);

Intrinsic Functions and Pseudo Parameters

Intrinsic Functions and Pseudo Parameters are available via Fn and AwsParam classes respectively.

Additionally the Fn class has a join$ method which can construct a value via a tagged template string. This uses the Fn::Join function under the hood to construct a string from the input.

import { Fn } from "@awboost/cfn-template-builder/intrinsics";
import { AwsParam } from "@awboost/cfn-template-builder/pseudo";
import { Template } from "@awboost/cfn-template-builder/template";

const template: Template = {
  Parameters: {
    CodeBucket: { Type: "String" },
    CodeObject: { Type: "String" },
  },
  Resources: {
    MyFunction: {
      Type: "AWS::Lambda::Function",
      Properties: {
        Code: {
          S3Bucket: Fn.ref("CodeBucket"),
          S3Key: Fn.ref("CodeObject"),
        },
        Role: "arn:etc:...:...",
        FunctionName: Fn.join$`${AwsParam.StackName}-MyFunction`,
      },
    },
  },
};

Here the Fn.join$ call is equivalent to:

Fn.join("", [AwsParam.StackName, "-MyFunction"]);

This ultimately outputs the following JSON in the template:

{
  "Fn::Join": ["", [{ "Ref": "AWS::StackName" }, "-MyFunction"]]
}