在 AWS SAM 模板中创建 RDS 完全访问角色

Create RDS full access role inside AWS SAM template


我正在尝试通过 lambda 函数访问 RDS mysql 数据库。我正在部署为 SAM 模板。我有一个附加到执行角色的 lambda 函数,如下所示:

  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          Effect: Allow
          Principal:
            Service: lambda.amazonaws.com
          Action: sts:AssumeRole
      Path: "/"
      Policies:
        - PolicyName: root
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: arn:aws:logs:*:*:*
              - Effect: Allow
                Action:
                  - rds:*
                Resource: "*"

  CreateTaskFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./components/lambdaFunctions/createTask
      Handler: createTask.handler
      Runtime: nodejs12.x
      Role: !GetAtt LambdaExecutionRole.Arn
      Timeout: 500
      Events:
        ProxyApiRoot:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /
            Method: ANY
        ProxyApiGreedy:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /{proxy+}
            Method: ANY
      Layers:
        - !Ref NodeModulesLayer

部署堆栈后,lambda 无法连接到 RDS,我在 lambda 的权限部分发现只有 cloudwatch 日志角色:

如您所见,未列出 RDS 权限。有什么建议吗?

我的天啊。我想通了,这是一个VPC问题。 Lambda必须附加到VPC,以及数据库安全组允许的安全组。