首先,在CloudFormation中,您需要提供Lambda函数的执行角色,并将其赋予事件桥接目标中的Lambda函数,就像这样:
Resources:
MyFunction:
Type: 'AWS::Lambda::Function'
Properties:
Code:
S3Bucket: mybucket
S3Key: myfunction.zip
Handler: index.handler
Role: !GetAtt MyExecutionRole.Arn
Runtime: nodejs12.x
MyExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
- 'arn:aws:iam::aws:policy/AmazonEventBridgeInvocationAccess'
现在,我们需要使用Lambda函数的执行角色来创建事件桥接目标。这就需要使用RoleArn参数,如下所示:
MyRule:
Type: 'AWS::Events::Rule'
Properties:
Description: 'My scheduled rule'
ScheduleExpression: 'cron(0 4 * * ? *)'
State: ENABLED
Targets:
- Arn: !GetAtt MyFunction.Arn
Id: 'MyTarget'
RoleArn: !GetAtt MyExecutionRole.Arn
这将确保在Lambda函数执行事件时,它将拥有所需的权限。