Skip to content

Commit

Permalink
Initial commit via Backstage
Browse files Browse the repository at this point in the history
  • Loading branch information
tmclaugh committed Dec 5, 2024
0 parents commit ace3e3d
Show file tree
Hide file tree
Showing 30 changed files with 2,189 additions and 0 deletions.
Empty file added .cfnlintrc
Empty file.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.aws-sam
84 changes: 84 additions & 0 deletions .github/workflows/feature-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Feature Branch
on:
workflow_dispatch:
push:
branches:
- feature/*

jobs:
build:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read

steps:
- name: Setup job workspace
uses: ServerlessOpsIO/gha-setup-workspace@v1

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install Yarn via corepack
shell: bash
run: corepack enable

- name: Install dependencies
shell: bash
run: yarn install --immutable

- name: Compile Typescript
run: yarn run compile

- name: Run unit tests
run: yarn run test:unit

- name: Assume AWS credentials
uses: ServerlessOpsIO/gha-assume-aws-credentials@v1
with:
build_aws_account_id: ${{ secrets.AWS_CICD_ACCOUNT_ID }}

- name: Install AWS SAM
uses: aws-actions/setup-sam@v2

- name: Validate template
run: sam validate --lint

- name: Build deployment artifact
run: yarn run sam:build

- name: Store artifacts
uses: ServerlessOpsIO/gha-store-artifacts@v1
with:
use_aws_sam: true

deploy:
needs:
- build

environment: production
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read

steps:
- name: Setup job workspace
uses: ServerlessOpsIO/gha-setup-workspace@v1
with:
checkout_artifact: true

- name: Assume AWS credentials
uses: ServerlessOpsIO/gha-assume-aws-credentials@v1
with:
build_aws_account_id: ${{ secrets.AWS_CICD_ACCOUNT_ID }}
deploy_aws_account_id: ${{ secrets.DEPLOYMENT_ACCOUNT_ID }}

- name: Deploy via AWS SAM
uses: ServerlessOpsIO/gha-deploy-aws-sam@v1
with:
aws_account_id: ${{ secrets.DEPLOYMENT_ACCOUNT_ID }}
env_json: ${{ toJson(env) }}
secrets_json: ${{ toJson(secrets) }}
85 changes: 85 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Main

on:
workflow_dispatch:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read

steps:
- name: Setup job workspace
uses: ServerlessOpsIO/gha-setup-workspace@v1

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install Yarn via corepack
shell: bash
run: corepack enable

- name: Install dependencies
shell: bash
run: yarn install --immutable

- name: Compile Typescript
run: yarn run compile

- name: Run unit tests
run: yarn run test:unit

- name: Assume AWS credentials
uses: ServerlessOpsIO/gha-assume-aws-credentials@v1
with:
build_aws_account_id: ${{ secrets.AWS_CICD_ACCOUNT_ID }}

- name: Install AWS SAM
uses: aws-actions/setup-sam@v2

- name: Validate template
run: sam validate --lint

- name: Build deployment artifact
run: yarn run sam:build

- name: Store artifacts
uses: ServerlessOpsIO/gha-store-artifacts@v1
with:
use_aws_sam: true

deploy:
needs:
- build

environment: production
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read

steps:
- name: Setup job workspace
uses: ServerlessOpsIO/gha-setup-workspace@v1
with:
checkout_artifact: true

- name: Assume AWS credentials
uses: ServerlessOpsIO/gha-assume-aws-credentials@v1
with:
build_aws_account_id: ${{ secrets.AWS_CICD_ACCOUNT_ID }}
deploy_aws_account_id: ${{ secrets.DEPLOYMENT_ACCOUNT_ID }}

- name: Deploy via AWS SAM
uses: ServerlessOpsIO/gha-deploy-aws-sam@v1
with:
aws_account_id: ${{ secrets.DEPLOYMENT_ACCOUNT_ID }}
env_json: ${{ toJson(env) }}
secrets_json: ${{ toJson(secrets) }}
44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.swp

# Coverage reports
coverage

# API keys and secrets
.env

# Dependency management
.yarn
!.yarn/patches
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
.pnp.*
node_modules

# Editors
.idea
*.iml

# OS metadata
.DS_Store
Thumbs.db

# Ignore Typescript build output
dist

# IDE
.vscode
!.vscode/launch.json
!.vscode/tasks.json

# AWS SAM
.aws-sam
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# backstage / test-thing-ts

Test-thing

The full API can be found in the [OpenAPI document](./openapi.yaml).


## Architecture
This is an AWS serverless CRUD API backend service. It is built on top of the following AWS services:
* API Gateway
* Lambda
* DynamoDB
* Cognito (See _Getting Started / API / Authentication and Authorization_ for more)


## New Project Getting Started
This repository was generated from a template intended to get a new API up and running quickly. This section will cover different aspects of the newly created project as well as areas that may need to be modified to meet the specific needs of a new project.


### Database
The database is a DDB table with a primary key composed of a partition key and sort key to form a composite primark key. Both usage of a composite primary key and generically named partition and sort keys were chosen to allow for a variety of different data types to be stored in the table. These keys are:

* Partition Key: `pk`
* Sort Key: `sk`

The starter code assumes the values of _pk_ and _sk_ are the same value and their value is the same as the API's `id` path parameter. When creating new resources the starter code will dynamically create a UUID as the value for `pk` and `sk`.

If you expect to have multiple types of data stored in the same table you should consider using compound key values for _pk_ and _sk_. These are keys where the values are prefixed with a string indicating the data type. For example, prefixing the value with the collection name. eg. _thing#1234_. See the _Code_ section for more information on implimenting this.


### Code
When starting a new project the first place to start with adapting the code to meet the needs of a new project is the [`src/lib/ThingItem.ts`](src/lib/ThingItem.ts) file. This file contains interfaces for data and DDB table items.

Start by modifying the `ThingItemData` interface to match the shape of your data. Optionally you can choose to replace that interface with one from another module if you're working with a pre-existing data model. The existing interface definition was chosen simply to make the project work out of the box.

Next, modify the `createKeys()` and `getKeys()` functions as necessary. These functions exist to make working with keys consistent across Lambda functions. If you want to prepend a data type to key values as mentioned in the _Database_ section you can do so here.


### API
This projects provides and OpenAPI document that defines the API. This document is located at [openapi.yaml](./openapi.yaml). This document besides defining the API is also used by the AWS SAM IaC to define the API Gateway resources.

#### Authentication and Authorization
This service is configured to use a pre-existing Cognito User Pool. Clients should obtain a JWT from the Cognito token endpoint using the client's clientId and clientSecret. Each endpoint's scope requirements are defined in the [OpenAPI document](./openapi.yaml).
32 changes: 32 additions & 0 deletions catalog-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: test-thing-ts
description: Test-thing
annotations:
github.com/project-slug: ServerlessOpsIO/test-thing-ts
spec:
type: api
lifecycle: production
owner: group:backstage
system: system:backstage
providesApis:
- resource:test-thing-ts

---
apiVersion: backstage.io/v1alpha1
kind: API
metadata:
name: test-thing-ts
description: |
Thing API
Manage things!
spec:
type: openapi
system: system:backstage
lifecycle: production
owner: group:backstage
definition:
$text: https://github.com/ServerlessOpsIO/test-thing-ts/blob/main/openapi.yaml
9 changes: 9 additions & 0 deletions cfn-parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Domain": "devtools",
"System": "backstage",
"Component": $env.GITHUB_REPOSITORY_NAME_PART_SLUG_CS,
"CodeBranch": $env.GITHUB_REF_SLUG_CS,
"Hostname": "api.test-ts.backstage.serverlessops.io",
"DnsZoneId": "/org/dns/ZoneId",
"CognitoUserPoolArn": $secrets.COGNITO_USER_POOL_ARN
}
5 changes: 5 additions & 0 deletions cfn-tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"org:domain": "devtools",
"org:system": "backstage",
"org:component": $env.GITHUB_REPOSITORY_NAME_PART_SLUG_CS
}
7 changes: 7 additions & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import js from "@eslint/js";
import tsEslint from 'typescript-eslint';

module.exports = tsEslint.config(
js.configs.recommended,
...tsEslint.configs.recommended,
)
11 changes: 11 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"testEnvironment": "node",
"transform": {
"^.+.tsx?$": ["ts-jest",{}]
},
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"modulePathIgnorePatterns": ["<rootDir>/dist/", "<rootDir>/.aws-sam"],
"collectCoverage": true
}
Loading

0 comments on commit ace3e3d

Please sign in to comment.