Skip to content

Commit 131827f

Browse files
authored
Merge pull request #601 from richardzimring/master
feat: add IAM Role support for bedrock invokeModel with foundation models
2 parents f2f379d + 8282395 commit 131827f

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

lib/deploy/stepFunctions/compileIamRole.js

+20
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,23 @@ function getSageMakerPermissions(state) {
524524
];
525525
}
526526

527+
function getBedrockPermissions(state) {
528+
const modelId = state.Parameters.ModelId;
529+
const modelArn = modelId.startsWith('arn:') ? modelId : {
530+
'Fn::Sub': [
531+
`arn:\${AWS::Partition}:bedrock:$\{AWS::Region}::foundation-model/${modelId}`,
532+
{},
533+
],
534+
};
535+
536+
return [
537+
{
538+
action: 'bedrock:InvokeModel',
539+
resource: modelArn,
540+
},
541+
];
542+
}
543+
527544
function getEventBridgePermissions(state) {
528545
const eventBuses = new Set();
529546

@@ -683,6 +700,9 @@ function getIamPermissions(taskStates) {
683700
case 'arn:aws:states:::sagemaker:createTransformJob.sync':
684701
return getSageMakerPermissions(state);
685702

703+
case 'arn:aws:states:::bedrock:invokeModel':
704+
return getBedrockPermissions(state);
705+
686706
case 'arn:aws:states:::events:putEvents':
687707
case 'arn:aws:states:::events:putEvents.waitForTaskToken':
688708
return getEventBridgePermissions(state);

lib/deploy/stepFunctions/compileIamRole.test.js

+63
Original file line numberDiff line numberDiff line change
@@ -3559,6 +3559,69 @@ describe('#compileIamRole', () => {
35593559
}]);
35603560
});
35613561

3562+
it('should give bedrock invoke permissions for foundation models', () => {
3563+
serverless.service.stepFunctions = {
3564+
stateMachines: {
3565+
myStateMachine1: {
3566+
id: 'StateMachine1',
3567+
definition: {
3568+
StartAt: 'A',
3569+
States: {
3570+
A: {
3571+
Type: 'Task',
3572+
Resource: 'arn:aws:states:::bedrock:invokeModel',
3573+
Parameters: {
3574+
ModelId: 'anthropic.claude-v2:1',
3575+
Body: {
3576+
prompt: 'your-prompt',
3577+
max_tokens_to_sample: 500,
3578+
temperature: 0.1,
3579+
},
3580+
ContentType: 'application/json',
3581+
Accept: 'application/json',
3582+
},
3583+
Next: 'B',
3584+
},
3585+
B: {
3586+
Type: 'Task',
3587+
Resource: 'arn:aws:states:::bedrock:invokeModel',
3588+
Parameters: {
3589+
// modelId can be specified as an arn
3590+
ModelId: 'arn:aws:bedrock:us-east-1::foundation-model/meta.llama2-70b-chat-v1',
3591+
Body: {
3592+
prompt: 'your-prompt',
3593+
max_tokens_to_sample: 500,
3594+
temperature: 0.1,
3595+
},
3596+
ContentType: 'application/json',
3597+
Accept: 'application/json',
3598+
},
3599+
End: true,
3600+
},
3601+
},
3602+
},
3603+
},
3604+
},
3605+
};
3606+
3607+
serverlessStepFunctions.compileIamRole();
3608+
const statements = serverlessStepFunctions.serverless.service
3609+
.provider.compiledCloudFormationTemplate.Resources.StateMachine1Role
3610+
.Properties.Policies[0].PolicyDocument.Statement;
3611+
const bedrockPermissions = statements.filter(s => _.isEqual(s.Action, ['bedrock:InvokeModel']));
3612+
expect(bedrockPermissions).to.have.lengthOf(1);
3613+
expect(bedrockPermissions[0].Resource).to.have.lengthOf(2);
3614+
expect(bedrockPermissions[0].Resource).to.deep.eq([
3615+
{
3616+
'Fn::Sub': [
3617+
'arn:${AWS::Partition}:bedrock:${AWS::Region}::foundation-model/anthropic.claude-v2:1',
3618+
{},
3619+
],
3620+
},
3621+
'arn:aws:bedrock:us-east-1::foundation-model/meta.llama2-70b-chat-v1',
3622+
]);
3623+
});
3624+
35623625
it('should give event bridge putEvents permissions', () => {
35633626
const genStateMachine = id => ({
35643627
id,

0 commit comments

Comments
 (0)