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
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,41 @@

API Gateway for processing Benchling Events

The `cdk.json` file tells the CDK Toolkit how to execute your app.
## Configuration

Create a `.env` file with the following content:

```bash
export CDK_DEFAULT_ACCOUNT=XXXXXXXXXXXX
export CDK_DEFAULT_REGION=us-west-2
export BUCKET_NAME=bucket-in-that-region
export PREFIX=test/benchling-webhook
export QUEUE_NAME=STACK_NAME-PackagerQueue-XXXXXXX
export QUEUE_URL=https://sqs.$CDK_DEFAULT_REGION.amazonaws.com/$CDK_DEFAULT_ACCOUNT/$QUEUE_NAME
```

## Deployment

```bash
curl -X POST https://gtju7dq18a.execute-api.us-west-1.amazonaws.com/prod/benchling-webhook \
source .env
npx cdk bootstrap aws://$CDK_DEFAULT_ACCOUNT/$CDK_DEFAULT_REGION
npx cdk deploy
```

## Usage

```bash
export ENDPOINT_ID=4sdc7ph31f
export ENDPOINT_URL=https://$ENDPOINT_ID.execute-api.$CDK_DEFAULT_REGION.amazonaws.com/$STAGE/benchling-webhook

curl -X POST $ENDPOINT_URL \
-H "Content-Type: application/json" \
-d '{
"message": "Hello from Benchling webhook!",
"timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'"
}'
aws s3 cp s3://quilt-ernest-staging/test/benchling-webhook/api_payload.json -
open https://nightly.quilttest.com/b/quilt-ernest-staging/tree/test/benchling-webhook/api_payload.json
aws s3 cp s3://$BUCKET_NAME/$PREFIX/api_payload.json -
open https://$QUILT_CATALOG/b/$BUCKET_NAME/tree/$PREFIX/
```

## Useful commands
Expand Down
6 changes: 4 additions & 2 deletions bin/benchling-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { BenchlingWebhookStack } from '../lib/benchling-webhook-stack';

const app = new cdk.App();
new BenchlingWebhookStack(app, 'BenchlingWebhookStack', {
bucketName: process.env.BUCKET_NAME || 'quilt-ernest-staging',
environment: process.env.ENVIRONMENT || 'prod'
bucketName: process.env.BUCKET_NAME || 'my-bucket-name',
queueName: process.env.QUEUE_NAME || 'my-queue-name',
environment: process.env.STAGE || 'prod',
prefix: process.env.PREFIX || 'test/benchling-webhook'
});
42 changes: 35 additions & 7 deletions lib/benchling-webhook-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ import * as logs from 'aws-cdk-lib/aws-logs';
interface BenchlingWebhookStackProps extends cdk.StackProps {
readonly bucketName: string;
readonly environment: string;
readonly prefix: string;
readonly queueName: string;
}

export class BenchlingWebhookStack extends cdk.Stack {
private readonly bucket: s3.IBucket;
private readonly stateMachine: stepfunctions.StateMachine;
private readonly api: apigateway.RestApi;
private readonly prefix: string;
private readonly queueName: string;

constructor(scope: Construct, id: string, props: BenchlingWebhookStackProps) {
super(scope, id, props);
this.prefix = props.prefix;
this.queueName = props.queueName;

this.bucket = this.createS3Bucket(props.bucketName);
this.stateMachine = this.createStateMachine();
this.api = this.createApiGateway();

this.createOutputs();
}

Expand All @@ -33,12 +38,15 @@ export class BenchlingWebhookStack extends cdk.Stack {

private createStateMachine(): stepfunctions.StateMachine {
const writeToS3Task = this.createS3WriteTask();
const sendToSQSTask = this.createSQSSendTask();

const definition = writeToS3Task.addCatch(new stepfunctions.Fail(this, 'FailState', {
cause: 'S3 Write Failed',
error: 'WriteError'
writeToS3Task.addCatch(new stepfunctions.Fail(this, 'FailState', {
cause: 'Task Failed',
error: 'TaskError'
}));

const definition = writeToS3Task.next(sendToSQSTask);

return new stepfunctions.StateMachine(this, 'BenchlingWebhookStateMachine', {
definitionBody: stepfunctions.DefinitionBody.fromChainable(definition),
stateMachineType: stepfunctions.StateMachineType.STANDARD,
Expand All @@ -55,14 +63,36 @@ export class BenchlingWebhookStack extends cdk.Stack {
action: 'putObject',
parameters: {
Bucket: this.bucket.bucketName,
Key: 'test/benchling-webhook/api_payload.json',
Key: `${this.prefix}/api_payload.json`,
'Body.$': '$'
},
iamResources: [this.bucket.arnForObjects('*')],
resultPath: '$.putResult'
});
}

private createSQSSendTask(): tasks.CallAwsService {
const queueArn = `arn:aws:sqs:${this.region}:${this.account}:${this.queueName}`;
const queueUrl = `https://sqs.${this.region}.amazonaws.com/${this.account}/${this.queueName}`;
const timestamp = new Date().toISOString();

return new tasks.CallAwsService(this, 'SendToSQS', {
service: 'sqs',
action: 'sendMessage',
parameters: {
QueueUrl: queueUrl,
MessageBody: {
'source_prefix': `s3://${this.bucket.bucketName}/${this.prefix}/`,
'registry': this.bucket.bucketName,
'package_name': `${this.prefix}`,
'commit_message': `Benchling webhook payload - ${timestamp}`
}
},
iamResources: [queueArn],
resultPath: '$.sqsResult'
});
}

private createCloudWatchRole(): iam.Role {
const cloudWatchRole = new iam.Role(this, 'ApiGatewayCloudWatchRole', {
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'),
Expand All @@ -71,7 +101,6 @@ export class BenchlingWebhookStack extends cdk.Stack {
]
});

// Create the account-level settings for API Gateway
new apigateway.CfnAccount(this, 'ApiGatewayAccount', {
cloudWatchRoleArn: cloudWatchRole.roleArn
});
Expand Down Expand Up @@ -107,7 +136,6 @@ export class BenchlingWebhookStack extends cdk.Stack {
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com')
});

// More specific permissions instead of using managed policy
role.addToPolicy(new iam.PolicyStatement({
actions: ['states:StartExecution'],
resources: [this.stateMachine.stateMachineArn]
Expand Down
4 changes: 3 additions & 1 deletion test/benchling-webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ describe('BenchlingWebhookStack', () => {
app = new cdk.App();
stack = new BenchlingWebhook.BenchlingWebhookStack(app, 'TestStack', {
bucketName: 'test-bucket',
environment: 'test'
environment: 'test',
prefix: 'test/benchling-webhook',
queueName: 'test-queue'
});
template = Template.fromStack(stack);
});
Expand Down
7 changes: 7 additions & 0 deletions test/sqs-message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"source_prefix": "s3://quilt-bake/test/benchling-webhook/",
"registry": "quilt-bake",
"package_name": "test/benchling-webhook",
"commit_message": "Benchling webhook payload - 2025-02-28T21:18:34.583Z",
"metadata": {"timestamp": "2025-02-28T21:18:34.583Z"}
}