Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/core/src/Components/ServerlessCode.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { IAgent as Agent } from '@sre/types/Agent.types';
import { Component } from './Component.class';
import Joi from 'joi';
import { ConnectorService } from '@sre/Core/ConnectorsService';
import { AWSCredentials, AWSRegionConfig } from '@sre/types/AWS.types';
import { calculateExecutionCost, getLambdaCredentials, reportUsage } from '@sre/helpers/AWSLambdaCode.helper';

export class ServerlessCode extends Component {

Expand Down Expand Up @@ -56,9 +58,18 @@ export class ServerlessCode extends Component {
}

logger.debug(`\nInput Variables: \n${JSON.stringify(codeInputs, null, 2)}\n`);

let codeConnector = ConnectorService.getCodeConnector();
let codeCredentials: AWSCredentials & AWSRegionConfig & { isUserProvidedKeys: boolean } =
await getLambdaCredentials(agent, config);

if (codeCredentials.isUserProvidedKeys) {
codeConnector = codeConnector.instance({
region: codeCredentials.region,
accessKeyId: codeCredentials.accessKeyId,
secretAccessKey: codeCredentials.secretAccessKey,
})
}
// Deploy lambda function if it doesn't exist or the code hash is different
await codeConnector.agent(agent.id)
.deploy(config.id, {
Expand All @@ -78,6 +89,13 @@ export class ServerlessCode extends Component {
);
logger.debug(`Execution time: ${executionTime}ms\n`);

const cost = calculateExecutionCost(executionTime);
if (!codeCredentials.isUserProvidedKeys) {
const accountConnector = ConnectorService.getAccountConnector();
const agentTeam = await accountConnector.getCandidateTeam(agent.id);
reportUsage({ cost, agentId: agent.id, teamId: agentTeam });
}

if (executionResponse.success) {
Output = executionResponse.output;
} else {
Expand Down
43 changes: 42 additions & 1 deletion packages/core/src/helpers/AWSLambdaCode.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import zl from 'zip-lib';
import { InvokeCommand, Runtime, LambdaClient, UpdateFunctionCodeCommand, CreateFunctionCommand, GetFunctionCommand, GetFunctionCommandOutput, InvokeCommandOutput } from '@aws-sdk/client-lambda';
import { GetRoleCommand, CreateRoleCommand, IAMClient, GetRoleCommandOutput, CreateRoleCommandOutput } from '@aws-sdk/client-iam';
import fs from 'fs';
import { AWSCredentials, AWSRegionConfig } from '@sre/types/AWS.types';
import { AWSConfig, AWSCredentials, AWSRegionConfig } from '@sre/types/AWS.types';
import { VaultHelper } from '@sre/Security/Vault.service/Vault.helper';
import { IAgent } from '@sre/types/Agent.types';
import { SystemEvents } from '@sre/Core/SystemEvents';
export const cachePrefix = 'serverless_code';
export const cacheTTL = 60 * 60 * 24 * 16; // 16 days
const PER_SECOND_COST = 0.0001;

export function getLambdaFunctionName(agentId: string, componentId: string) {
return `${agentId}-${componentId}`;
Expand Down Expand Up @@ -340,8 +344,45 @@ export async function getDeployedFunction(functionName: string, awsConfigs: AWSC
}
}

export async function getLambdaCredentials(agent: IAgent, config: any): Promise<AWSConfig & { isUserProvidedKeys: boolean }> {
let awsAccessKeyId = null;
let awsSecretAccessKey = null;
let awsRegion = null;
let userProvidedKeys = false;
if (config.data.accessKeyId && config.data.secretAccessKey && config.data.region && config.data.use_own_keys) {
userProvidedKeys = true;
[awsAccessKeyId, awsSecretAccessKey] = await Promise.all([
VaultHelper.getTeamKey(extractKeyFromTemplateVar(config.data.accessKeyId), agent?.teamId),
VaultHelper.getTeamKey(extractKeyFromTemplateVar(config.data.secretAccessKey), agent?.teamId),
]);
awsRegion = config.data.region;
}
const awsCredentials = {
accessKeyId: awsAccessKeyId,
secretAccessKey: awsSecretAccessKey,
region: awsRegion,
isUserProvidedKeys: userProvidedKeys,
};
return awsCredentials;
}

export function calculateExecutionCost(executionTime: number) {
// executionTime in milliseconds
const cost = (executionTime / 1000) * Number(PER_SECOND_COST);
return cost;
}

export function extractKeyFromTemplateVar(input: string) {
const regex = /\{\{KEY\((.*?)\)\}\}/;
const match = input.match(regex);
return match ? match[1] : input;
}

export function reportUsage({ cost, agentId, teamId }: { cost: number; agentId: string; teamId: string }) {
SystemEvents.emit('USAGE:API', {
sourceId: 'api:serverless_code.smyth',
cost,
agentId,
teamId,
});
}