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

feat: Add option to enable logging on state machine #150

Merged
merged 10 commits into from
Dec 19, 2022
98 changes: 96 additions & 2 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/lambdas/status/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ exports.handler = async function () {
webhookHandlerUrl: lambdaArnToUrl(process.env.WEBHOOK_HANDLER_ARN),
stepFunctionArn: process.env.STEP_FUNCTION_ARN,
stepFunctionUrl: stepFunctionArnToUrl(process.env.STEP_FUNCTION_ARN),
stepFunctionLogGroup: process.env.STEP_FUNCTION_LOG_GROUP,
recentRuns: [] as RecentRun[],
},
};
Expand Down
60 changes: 60 additions & 0 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
aws_ec2 as ec2,
aws_iam as iam,
aws_lambda as lambda,
aws_logs as logs,
aws_stepfunctions as stepfunctions,
aws_stepfunctions_tasks as stepfunctions_tasks,
RemovalPolicy,
} from 'aws-cdk-lib';
import { FunctionUrlAuthType } from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
Expand Down Expand Up @@ -82,6 +84,46 @@ export interface GitHubRunnersProps {
* @default 10 minutes
*/
readonly idleTimeout?: cdk.Duration;

/**
* Logging options for the state machine that manages the runners.
*
* @default no logs
*/
readonly logOptions?: LogOptions;
}

/**
* Defines what execution history events are logged and where they are logged.
*/
export interface LogOptions {
/**
* The log group where the execution history events will be logged.
*/
readonly logGroupName?: string;

/**
* Determines whether execution data is included in your log.
*
* @default false
*/
readonly includeExecutionData?: boolean;

/**
* Defines which category of execution history events are logged.
*
* @default ERROR
*/
readonly level?: stepfunctions.LogLevel;

/**
* The number of days log events are kept in CloudWatch Logs. When updating
* this property, unsetting it doesn't remove the log retention policy. To
* remove the retention policy, set the value to `INFINITE`.
*
* @default logs.RetentionDays.ONE_MONTH
*/
readonly logRetention?: logs.RetentionDays;
}

/**
Expand Down Expand Up @@ -140,6 +182,7 @@ export class GitHubRunners extends Construct {
private readonly setupUrl: string;
private readonly extraLambdaEnv: {[p: string]: string} = {};
private readonly extraLambdaProps: lambda.FunctionOptions;
private stateMachineLogGroup?: logs.LogGroup;

constructor(scope: Construct, id: string, readonly props?: GitHubRunnersProps) {
super(scope, id);
Expand Down Expand Up @@ -278,11 +321,27 @@ export class GitHubRunners extends Construct {
.when(stepfunctions.Condition.isNotPresent('$.labels.self-hosted'), new stepfunctions.Succeed(this, 'No'))
.otherwise(work);

let logOptions: cdk.aws_stepfunctions.LogOptions | undefined;
if (this.props?.logOptions) {
this.stateMachineLogGroup = new logs.LogGroup(this, 'Logs', {
logGroupName: props?.logOptions?.logGroupName,
retention: props?.logOptions?.logRetention ?? logs.RetentionDays.ONE_MONTH,
removalPolicy: RemovalPolicy.DESTROY,
});

logOptions = {
destination: this.stateMachineLogGroup,
includeExecutionData: props?.logOptions?.includeExecutionData ?? true,
level: props?.logOptions?.level ?? stepfunctions.LogLevel.ALL,
};
}

const stateMachine = new stepfunctions.StateMachine(
this,
'Runner Orchestrator',
{
definition: check,
logs: logOptions,
},
);

Expand Down Expand Up @@ -351,6 +410,7 @@ export class GitHubRunners extends Construct {
WEBHOOK_URL: this.webhook.url,
WEBHOOK_HANDLER_ARN: this.webhook.handler.latestVersion.functionArn,
STEP_FUNCTION_ARN: this.orchestrator.stateMachineArn,
STEP_FUNCTION_LOG_GROUP: this.stateMachineLogGroup?.logGroupName ?? '',
SETUP_FUNCTION_URL: this.setupUrl,
...this.extraLambdaEnv,
},
Expand Down
10 changes: 5 additions & 5 deletions test/default.integ.snapshot/github-runners-test.assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,28 +209,28 @@
}
}
},
"1d1f025a6abc03af735a1d9f680134d3b9c44baf6899b8734486d8527d4f1a5b": {
"165e1c4dfb9c46dd1e0d52c192374a9a1c16c01851d1aee4b48b5dc58bf31df0": {
"source": {
"path": "asset.1d1f025a6abc03af735a1d9f680134d3b9c44baf6899b8734486d8527d4f1a5b",
"path": "asset.165e1c4dfb9c46dd1e0d52c192374a9a1c16c01851d1aee4b48b5dc58bf31df0",
"packaging": "zip"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "1d1f025a6abc03af735a1d9f680134d3b9c44baf6899b8734486d8527d4f1a5b.zip",
"objectKey": "165e1c4dfb9c46dd1e0d52c192374a9a1c16c01851d1aee4b48b5dc58bf31df0.zip",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
},
"5bdb97545990fafb65ad252cd2694d12749d3d887cfd0327feb65c50a3ec1d0d": {
"c2822ff75b10806b106028ef401bbed464a897b608e8597bc0b2e409e00756ca": {
"source": {
"path": "github-runners-test.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "5bdb97545990fafb65ad252cd2694d12749d3d887cfd0327feb65c50a3ec1d0d.json",
"objectKey": "c2822ff75b10806b106028ef401bbed464a897b608e8597bc0b2e409e00756ca.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12063,7 +12063,7 @@
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "1d1f025a6abc03af735a1d9f680134d3b9c44baf6899b8734486d8527d4f1a5b.zip"
"S3Key": "165e1c4dfb9c46dd1e0d52c192374a9a1c16c01851d1aee4b48b5dc58bf31df0.zip"
},
"Role": {
"Fn::GetAtt": [
Expand Down Expand Up @@ -12109,6 +12109,7 @@
"STEP_FUNCTION_ARN": {
"Ref": "runnersRunnerOrchestratorF9B66EBA"
},
"STEP_FUNCTION_LOG_GROUP": "",
"SETUP_FUNCTION_URL": {
"Fn::GetAtt": [
"runnerssetupFunctionUrlB8BC43E8",
Expand Down
2 changes: 1 addition & 1 deletion test/default.integ.snapshot/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/5bdb97545990fafb65ad252cd2694d12749d3d887cfd0327feb65c50a3ec1d0d.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/c2822ff75b10806b106028ef401bbed464a897b608e8597bc0b2e409e00756ca.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down
3 changes: 2 additions & 1 deletion test/default.integ.snapshot/tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -16247,7 +16247,7 @@
"s3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"s3Key": "1d1f025a6abc03af735a1d9f680134d3b9c44baf6899b8734486d8527d4f1a5b.zip"
"s3Key": "165e1c4dfb9c46dd1e0d52c192374a9a1c16c01851d1aee4b48b5dc58bf31df0.zip"
},
"role": {
"Fn::GetAtt": [
Expand Down Expand Up @@ -16293,6 +16293,7 @@
"STEP_FUNCTION_ARN": {
"Ref": "runnersRunnerOrchestratorF9B66EBA"
},
"STEP_FUNCTION_LOG_GROUP": "",
"SETUP_FUNCTION_URL": {
"Fn::GetAtt": [
"runnerssetupFunctionUrlB8BC43E8",
Expand Down
33 changes: 33 additions & 0 deletions test/runner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as cdk from 'aws-cdk-lib';
import { Match, Template } from 'aws-cdk-lib/assertions';
import { GitHubRunners } from '../src';

let app: cdk.App;
let stack: cdk.Stack;

describe('GitHubRunners', () => {
beforeEach(() => {
app = new cdk.App();
stack = new cdk.Stack(app, 'test');
});

test('Create GithubRunners with state machine logging enabled', () => {
new GitHubRunners(stack, 'runners', {
providers: [],
logOptions: {
logRetention: 1,
logGroupName: 'test',
},
});

const template = Template.fromStack(stack);

template.hasResourceProperties(
'AWS::Logs::LogGroup',
Match.objectLike({
LogGroupName: 'test',
RetentionInDays: 1,
}),
);
});
});