Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for creating custom resources #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions custom-resources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store

# sst build output
.build

npm-debug.log*
yarn-debug.log*
yarn-error.log*
43 changes: 43 additions & 0 deletions custom-resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# How to create a REST API

An example serverless app created with SST.

## Getting Started

[**Read the tutorial**](https://serverless-stack.com/examples/how-to-create-a-rest-api-with-serverless.html)

Install the dependencies.

```bash
$ npm install
```

## Commands

### `npm run start`

Starts the Live Lambda Development environment.

### `npm run build`

Build your app and synthesize your stacks.

### `npm run deploy [stack]`

Deploy all your stacks to AWS. Or optionally deploy, a specific stack.

### `npm run remove [stack]`

Remove all your stacks and all of their resources from AWS. Or optionally removes, a specific stack.

### `npm run test`

Runs your tests using Jest. Takes all the [Jest CLI options](https://jestjs.io/docs/en/cli).

## Documentation

Learn more about the SST.

- [Docs](https://docs.serverless-stack.com/)
- [@serverless-stack/cli](https://docs.serverless-stack.com/packages/cli)
- [@serverless-stack/resources](https://docs.serverless-stack.com/packages/resources)
17 changes: 17 additions & 0 deletions custom-resources/lib/MyStack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Token } from "@aws-cdk/core";
import * as sst from "@serverless-stack/resources";

import Sum from "./Sum";

export default class MyStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);

// Create the custom resource
const sum = new Sum(this, "MySum", { lhs: 40, rhs: 2 });

this.addOutputs({
Result: Token.asString(sum.result),
});
}
}
32 changes: 32 additions & 0 deletions custom-resources/lib/Sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Construct, CustomResource, Token } from "@aws-cdk/core";
import * as cr from "@aws-cdk/custom-resources";
import * as sst from "@serverless-stack/resources";

export default class Sum extends Construct {
result;

constructor(scope, id, props) {
super(scope, id);

// Create Lambda function
const eventHandler = new sst.Function(this, "Handler", {
handler: "src/lambda.main",
});

// Create custom resource
const provider = new cr.Provider(this, "ResourceProvider", {
onEventHandler: eventHandler,
});

const resource = new CustomResource(this, "Resource", {
resourceType: "Custom::Sum",
serviceToken: provider.serviceToken,
properties: {
lhs: props.lhs,
rhs: props.rhs,
},
});

this.result = Token.asNumber(resource.getAtt("Result"));
}
}
7 changes: 7 additions & 0 deletions custom-resources/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import MyStack from "./MyStack";

export default function main(app) {
new MyStack(app, "my-stack");

// Add more stacks
}