Skip to content

Commit

Permalink
tags on tables and endpoint lambdas. tagbased resource group per proj…
Browse files Browse the repository at this point in the history
…ect.
  • Loading branch information
andreujuanc committed Jun 11, 2024
1 parent f65fcb0 commit f735725
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lambada/core",
"version": "1.15.0",
"version": "1.15.1",
"description": "A VERY opinionated infrastructure as code framework.",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/api/createEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ export const createEndpoint = <E, R>(
resources,
undefined,
mergeOptions(options, lambadaContext.api?.lambdaOptions),
`${lambadaContext.projectName} ${method} ${path}`
`${lambadaContext.projectName} ${method} ${path}`,
lambadaContext.globalTags
)

let auth: (CognitoAuthorizer | LambdaAuthorizer)[] = []
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/api/openApiDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const createOpenApiDocumentEndpoint = (args: {
...config,
method: toLowerCase(x.method),
path: x.path,
responses: config.responses //not sure why ts is not getting that config.responses is a RouteConfig and {responses: config.responses
} satisfies RouteConfig

if (x.webhook) {
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function createTable(
attributes?: TableAttribute[],
secondaryIndexes?: TableIndexDefinition[],
ttl?: { attributeName: string, enabled: boolean },
options?: TableOptions
options?: TableOptions,
tags?: pulumi.Input<{ [key: string]: pulumi.Input<string> }>
) {
const tableName = `${name}-${environment}`

Expand All @@ -44,9 +45,7 @@ function createTable(
hashKey: primaryKeyName,
rangeKey: rangeKeyName,
//readCapacity: 20,
tags: {
Environment: environment,
},
tags: tags,
ttl: ttl ? {
attributeName: ttl.attributeName,
enabled: ttl.enabled
Expand Down Expand Up @@ -89,7 +88,7 @@ export type TableDefinition = {

export type EmbroideryTables = { [id: string]: TableDefinition }

export const createDynamoDbTables = (environment: string, tables?: EmbroideryTables, prefix?: string, kmsKeys?: SecurityResult, tableRefs?: EmbroideryTables | DatabaseResult): DatabaseResult => {
export const createDynamoDbTables = (environment: string, tables?: EmbroideryTables, prefix?: string, kmsKeys?: SecurityResult, tableRefs?: EmbroideryTables | DatabaseResult, tags?: pulumi.Input<{ [key: string]: pulumi.Input<string> }>): DatabaseResult => {

const result: DatabaseResult = {}
for (const key in tables) {
Expand All @@ -100,7 +99,8 @@ export const createDynamoDbTables = (environment: string, tables?: EmbroideryTab
tableName, environment, table.primaryKey, table.rangeKey,
kmsKeys?.dynamodb?.awsKmsKey,
table.attributes, table.indexes, table.ttl,
table.options
table.options,
tags
)

result[key] = {
Expand All @@ -124,7 +124,7 @@ export const createDynamoDbTables = (environment: string, tables?: EmbroideryTab
const table = tableRefs[key];

function isRef(obj: DatabaseResultItem | TableDefinition): obj is DatabaseResultItem {
return !!(( obj as DatabaseResultItem ).awsTable && ( obj as DatabaseResultItem ).ref)
return !!((obj as DatabaseResultItem).awsTable && (obj as DatabaseResultItem).ref)
}

if (isRef(table)) {
Expand Down
36 changes: 31 additions & 5 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as pulumi from "@pulumi/pulumi";
import * as awsx from "@pulumi/awsx/classic";
import * as aws from "@pulumi/aws";

import createApi, { LambadaCreator } from './api/createApi'
import { createCloudFront } from './cdn/index'
Expand Down Expand Up @@ -92,6 +93,16 @@ type LambadaRunArguments = {
}
}
},
resourceGroups?: {
/**
* Does not create a resource group
* */
skipCreate?: boolean
/**
* Overrides the default name (projectName-environment)
*/
name?: string
},
options?: {
dependsOn: pulumi.Input<pulumi.Resource> | pulumi.Input<pulumi.Input<pulumi.Resource>[]> | undefined;
}
Expand All @@ -102,14 +113,14 @@ export type EmbroideryEnvironmentVariables = pulumi.Input<{
}> | undefined

export const run = (projectName: string, environment: string, args: LambadaRunArguments) => {
const tags = {
const globalTags = {
"Lambada:Project": projectName,
"Lambada:Environment": environment
}

const encryptionKeys = args.keys ? CreateKMSKeys(projectName, environment, args.keys) : {}
const secrets = args.secrets ? createSecrets(projectName, environment, args.secrets) : {}
const databases = createDynamoDbTables(environment, args.tables, args.tablePrefix, encryptionKeys, args.tablesRef)
const databases = createDynamoDbTables(environment, args.tables, args.tablePrefix, encryptionKeys, args.tablesRef, globalTags)

const pool: UserPool | undefined = args.auth && args.auth.createCognito ?
createUserPool(projectName, environment, encryptionKeys, {
Expand All @@ -125,7 +136,7 @@ export const run = (projectName: string, environment: string, args: LambadaRunAr
const cognitoPoolId = isPool(pool) ? pool.id : undefined


const messaging = createMessaging(environment, args.messages, args.messagesRef, tags)
const messaging = createMessaging(environment, args.messages, args.messagesRef, globalTags)
const queues = createQueues(environment, args.queues, args.queuesRef)
const notifications = createNotifications(projectName, environment, args?.notifications)

Expand All @@ -141,7 +152,7 @@ export const run = (projectName: string, environment: string, args: LambadaRunAr
const authorizers: CognitoAuthorizer[] = authorizerProviderARNs.length > 0 ? [authorizer] : [];



// TODO: option to add projectName as prefix to all functions
const lambadaContext: LambadaResources = {
projectName: projectName,
Expand All @@ -163,7 +174,7 @@ export const run = (projectName: string, environment: string, args: LambadaRunAr
kmsKeys: encryptionKeys,
environmentVariables: args.environmentVariables || {},
secrets: secrets,
globalTags: tags
globalTags: globalTags
}

if (args.messageHandlerDefinitions) {
Expand Down Expand Up @@ -243,6 +254,21 @@ export const run = (projectName: string, environment: string, args: LambadaRunAr
args.cdn.customDomain,
) : undefined


if (!args.resourceGroups?.skipCreate) {
const groupName = args.resourceGroups?.name ?? `${projectName}-${environment}`

new aws.resourcegroups.Group(groupName, {
name: groupName,
resourceQuery: {
query: JSON.stringify({
ResourceTypeFilters: ["AWS::AllSupported"],
TagFilters: globalTags
})
}
})
}

return {
api: api,
cdn: cdn,
Expand Down

0 comments on commit f735725

Please sign in to comment.