在CDK中,如果我们想要将“sts:SetSourceIdentity”添加到AssumeRolePolicyDocument,则可以使用以下代码示例:
import * as cdk from 'aws-cdk-lib';
import * as iam from 'aws-cdk-lib/aws-iam';
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
const role = new iam.Role(stack, 'MyRole', {
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
});
const policy = new iam.PolicyStatement({
actions: ['sts:AssumeRole'],
effect: iam.Effect.ALLOW,
principals: [new iam.ServicePrincipal('lambda.amazonaws.com')],
conditions: {
'ForAnyValue:SourceIp': {
'aws:SourceIp': ['10.0.0.0/16', '192.168.0.0/24'],
},
'StringEquals': {
'sts:SetSourceIdentity': 'my-app'
},
},
});
role.addToPolicy(policy);
上述代码中的“sts:SetSourceIdentity”被包含在条件中,只有当条件StringEquals满足时,才会允许AssumeRole。您可以根据需要调整此代码,以满足您的具体条件和策略需求。