在SAM(Serverless Application Model)模板中为隐式API网关分配IAM角色,可以通过以下步骤实现:
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DefinitionBody:
swagger: "2.0"
info:
title: My API
paths:
/hello:
get:
responses:
'200':
description: OK
Auth
,用于指定隐式API网关的IAM角色。例如:Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Auth:
DefaultAuthorizer: MyAuthorizer
Authorizers:
MyAuthorizer:
Type: AWS_IAM
Resources:
MyApiRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: apigateway.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: MyApiPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource: "*"
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Auth:
DefaultAuthorizer: MyAuthorizer
Authorizers:
MyAuthorizer:
Type: AWS_IAM
AuthorizerUri: arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:MyAuthorizer/invocations
AuthorizerCredentials:
Fn::GetAtt: [MyApiRole, Arn]
在上述示例中,我们创建了一个名为MyApiRole
的IAM角色资源,并为其指定了允许API网关服务扮演该角色的权限。然后,在API网关资源的Auth
属性中,我们指定了MyApiRole
作为隐式API网关的IAM角色。
请注意,以上示例中的资源和属性名称仅供参考,实际使用时需要根据具体情况进行调整。
关于SAM模板和隐式API网关的更多信息,您可以参考腾讯云的相关文档和产品介绍:
领取专属 10元无门槛券
手把手带您无忧上云