Skip to content

Commit 886283e

Browse files
authored
chore(scheduler-alpha): unit test schedule with Lambda alias as target (#31873)
### Issue # (if applicable) None ### Reason for this change Missing this test case. ### Description of changes Adding a unit test to verify `Schedule` works with Lambda Alias and correct permissions are added. ### Description of how you validated changes Unit test. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6136d9e commit 886283e

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

packages/@aws-cdk/aws-scheduler-targets-alpha/test/lambda-invoke.test.ts

+62
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,68 @@ describe('schedule target', () => {
286286
});
287287
});
288288

289+
test('creates IAM role and IAM policy for lambda alias', () => {
290+
const lambdaVersion = new lambda.Version(stack, 'MyLambdaVersion', {
291+
lambda: func,
292+
});
293+
const lambdaAlias = new lambda.Alias(stack, 'MyLambdaAlias', {
294+
version: lambdaVersion,
295+
aliasName: 'SomeAliasName',
296+
});
297+
298+
const lambdaTarget = new LambdaInvoke(lambdaAlias, {});
299+
300+
new Schedule(stack, 'MyScheduleDummy', {
301+
schedule: expr,
302+
target: lambdaTarget,
303+
});
304+
305+
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Permission', 0);
306+
307+
Template.fromStack(stack).hasResource('AWS::Scheduler::Schedule', {
308+
Properties: {
309+
Target: {
310+
Arn: {
311+
Ref: 'MyLambdaAliasD26C43B4',
312+
},
313+
RoleArn: { 'Fn::GetAtt': ['SchedulerRoleForTarget1441a743A31888', 'Arn'] },
314+
RetryPolicy: {},
315+
},
316+
},
317+
});
318+
319+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
320+
PolicyDocument: {
321+
Statement: [
322+
{
323+
Action: 'lambda:InvokeFunction',
324+
Effect: 'Allow',
325+
Resource: {
326+
Ref: 'MyLambdaAliasD26C43B4',
327+
},
328+
},
329+
],
330+
},
331+
Roles: [{ Ref: 'SchedulerRoleForTarget1441a743A31888' }],
332+
});
333+
334+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', {
335+
AssumeRolePolicyDocument: {
336+
Version: '2012-10-17',
337+
Statement: [
338+
{
339+
Effect: 'Allow',
340+
Condition: { StringEquals: { 'aws:SourceAccount': '123456789012' } },
341+
Principal: {
342+
Service: 'scheduler.amazonaws.com',
343+
},
344+
Action: 'sts:AssumeRole',
345+
},
346+
],
347+
},
348+
});
349+
});
350+
289351
test('creates IAM policy for imported role for lambda function in the same account', () => {
290352
const importedRole = Role.fromRoleArn(stack, 'ImportedRole', 'arn:aws:iam::123456789012:role/someRole');
291353

0 commit comments

Comments
 (0)