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(stepfunctions-tasks): add guardrailConfiguration and trace property to the BedrockInvokeModel #30426

Merged
merged 17 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,18 @@ export class BedrockInvokeModel extends sfn.TaskStateBase {
}

if (this.props.guardrail) {
const isArn = this.props.guardrail.guardrailIdentifier.startsWith('arn:');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The policy document was incorrect for the case when guardrailIdentifier is an ARN, so I added a condition to address this.

policyStatements.push(
new iam.PolicyStatement({
actions: ['bedrock:ApplyGuardrail'],
resources: [
Stack.of(this).formatArn({
service: 'bedrock',
resource: 'guardrail',
resourceName: this.props.guardrail.guardrailIdentifier,
}),
isArn
? this.props.guardrail.guardrailIdentifier
: Stack.of(this).formatArn({
service: 'bedrock',
resource: 'guardrail',
resourceName: this.props.guardrail.guardrailIdentifier,
}),
],
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ describe('Invoke Model', () => {
}).toThrow(/Output S3 object version is not supported./);
});

test('guardrail', () => {
test('guardrail when gurdarilIdentifier is set to arn', () => {
// GIVEN
const stack = new cdk.Stack();
const model = bedrock.ProvisionedModel.fromProvisionedModelArn(stack, 'Imported', 'arn:aws:bedrock:us-turbo-2:123456789012:provisioned-model/abc-123');
Expand All @@ -381,6 +381,10 @@ describe('Invoke Model', () => {
},
});

new sfn.StateMachine(stack, 'StateMachine', {
definitionBody: sfn.DefinitionBody.fromChainable(task),
});

// THEN
expect(stack.resolve(task.toStateJson())).toEqual({
Type: 'Task',
Expand All @@ -407,6 +411,111 @@ describe('Invoke Model', () => {
GuardrailVersion: 'DRAFT',
},
});

Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: 'bedrock:InvokeModel',
Effect: 'Allow',
Resource: 'arn:aws:bedrock:us-turbo-2:123456789012:provisioned-model/abc-123',
},
{
Action: 'bedrock:ApplyGuardrail',
Effect: 'Allow',
Resource: 'arn:aws:bedrock:us-turbo-2:123456789012:guardrail/testid',
},
],
},
});
});

test('guardrail when gurdarilIdentifier is set to id', () => {
// GIVEN
const stack = new cdk.Stack();
const model = bedrock.ProvisionedModel.fromProvisionedModelArn(stack, 'Imported', 'arn:aws:bedrock:us-turbo-2:123456789012:provisioned-model/abc-123');

// WHEN
const task = new BedrockInvokeModel(stack, 'Invoke', {
model,
contentType: 'application/json',
body: sfn.TaskInput.fromObject(
{
prompt: 'Hello world',
},
),
guardrail: {
guardrailIdentifier: 'testid',
guardrailVersion: 'DRAFT',
},
});

new sfn.StateMachine(stack, 'StateMachine', {
definitionBody: sfn.DefinitionBody.fromChainable(task),
});

// THEN
expect(stack.resolve(task.toStateJson())).toEqual({
Type: 'Task',
Resource: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':states:::bedrock:invokeModel',
],
],
},
End: true,
Parameters: {
ModelId: 'arn:aws:bedrock:us-turbo-2:123456789012:provisioned-model/abc-123',
Body: {
prompt: 'Hello world',
},
ContentType: 'application/json',
GuardrailIdentifier: 'testid',
GuardrailVersion: 'DRAFT',
},
});

Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: 'bedrock:InvokeModel',
Effect: 'Allow',
Resource: 'arn:aws:bedrock:us-turbo-2:123456789012:provisioned-model/abc-123',
},
{
Action: 'bedrock:ApplyGuardrail',
Effect: 'Allow',
Resource: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':bedrock:',
{
Ref: 'AWS::Region',
},
':',
{
Ref: 'AWS::AccountId',
},
':guardrail/testid',
],
],
},
},
],
},
});
});

test('guardrail fails when invalid guardrailIdentifier is set', () => {
Expand Down