在SAM模板中,是否有方法引用使用Lambda函数自动创建的角色的ARN?
我需要在模板中的其他地方使用ARN。
# this is the role
Role:
Type: AWS::IAM::Role
Properties:
RoleName: client-role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
- Effect: Allow
Principal:
AWS: "arn:aws:iam::xxxx:role/xxxxxxx-ApiHandlerRole-12UWXALxxxxx"
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3FullAccess
# this is the lambda
ApiHandler:
Type: AWS::Serverless::Function
Properties:
FunctionName: api-handler
......
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref Table
- S3ReadPolicy:
BucketName: !Ref Bucket
- S3WritePolicy:
BucketName: !Ref Bucket
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action: sts:AssumeRole
Resource: !GetAtt Role.Arn
我认为您所做的是分离角色的资源构建。
然后你可以使用!在任何需要的地方引用角色,包括lambda角色中的角色。
解决方案:
所以,我今天遇到了这种情况。
假设您的资源Lambda名为MyLambda,则使用
!Ref MyLambdaRole.Arn
经过尝试和测试,效果就像一种魅力。
工作原理:如果展开已处理的模板,IAM角色资源将使用以下命名法命名
<lambda-logical-id>Role
别忘了投赞成票!:D
您可以自己构造角色的ARN。它有固定的格式。来自文档:
如果未指定角色,则会为您创建一个逻辑ID为函数逻辑idRole的角色。
例如使用Sub
:
!Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/function-logical-idRole"
其中function-logical-id
是lambda函数的逻辑id。
类似。。。
# this is the lambda
ApiHandler:
Type: AWS::Serverless::Function
Properties:
FunctionName: api-handler
Policies:
......
# this is the role
Role:
Type: AWS::IAM::Role
Properties:
RoleName: client-role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
- Effect: Allow
Principal:
AWS: !GetAtt ApiHandlerRole.Arn
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3FullAccess
假设您没有为函数指定">假设角色策略文档"。(上面基于pastebin的代码(,SAM cli将为您生成一个名为">ApiHandlerRole"的角色。
来自文档:
如果未指定此属性,AWS SAM将添加默认的假定角色用于此功能。
从外观上看,您可能只想进一步探索该属性。我是AWS SAM的新手,但必须有办法在上面插入"arn:AWS:iam::AWS:policy/AAmazonS3FullAccess"。:(
希望这能帮助所有
docs链接:https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-功能假设政策文件
对我有用的是参考自动生成角色的ARN属性,如
SomeLambdaFunction:
Type: AWS::Serverless::Function
Properties:
...other properties...
Policies:
... Some sam policies...
.... different resource reference Lambda autogenerate role...
- Action: //some actions here
Effect: Allow
Principal:
AWS: !GetAtt SomeLambdaFunctionRole.Arn
Resource: "*"
.... More template code...