使用serverless.yml

时间:2018-09-19 12:12:34

标签: amazon-web-services amazon-s3 amazon-cloudformation serverless-framework

我正在使用Severless框架配置AWS Cognito身份池,并且正在yml配置中编辑文件以添加未经身份验证的角色,以供用户将图像上传到s3存储桶。

该代码以前是在未指定未经身份验证的角色的情况下部署的,因此部署进行得很好且稳定。在寻找一种控制有关访问S3存储桶的权限的方法之后,我发现,授予S3存储桶写入(而非读取)权限的唯一方法是在用户策略中指定它,因此我必须添加身份池的角色。但是,当我部署代码时,出现错误提示:

Serverless Error ---------------------------------------

  An error occurred: CognitoIdentityPoolRoles - Resource cannot be updated.

我设法在开发环境中解决了这个问题,但是它要求完全删除堆栈并从头开始重建它。

我也不希望在AWS控制台中手动调整资源,因为应该在cloudformation或控制台中管理资源,但是两种方式都会导致混乱。

因此,目前,我看到的选项是删除整个堆栈,并使用新角色进行重建,或者找到一种通过cloudformation更新的方法。

有人能避免第一种选择,并允许我在不附加控制台角色的情况下更新堆栈吗?

serverless.yml的相关部分在下面...

Resources:
  # The federated identity for our user pool to auth with
  CognitoIdentityPool:
    Type: AWS::Cognito::IdentityPool
    Properties:
      # Generate a name based on the stage
      IdentityPoolName: ${self:custom.stage}MyIdentityPool
      # Allow unathenticated users
      AllowUnauthenticatedIdentities: true
      # Link to our User Pool
      CognitoIdentityProviders:
      - ClientId:
          Ref: CognitoUserPoolClient
        ProviderName:
          Fn::GetAtt: [ "CognitoUserPool", "ProviderName" ]

  # IAM roles
  CognitoIdentityPoolRoles:
    Type: AWS::Cognito::IdentityPoolRoleAttachment
    Properties:
      IdentityPoolId:
        Ref: CognitoIdentityPool
      Roles:
        authenticated:
          Fn::GetAtt: [CognitoAuthRole, Arn]
        # Next two lines are the 2 lines of code which break everything
        unauthenticated:
          Fn::GetAtt: [CognitoUnAuthRole, Arn]

  # IAM role for UN-authenticated users
  CognitoUnAuthRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: 'Allow'
          Principal:
            Federated: 'cognito-identity.amazonaws.com'
          Action:
          - 'sts:AssumeRoleWithWebIdentity'
          Condition:
            StringEquals:
              'cognito-identity.amazonaws.com:aud':
                Ref: CognitoIdentityPool
            'ForAnyValue:StringLike':
              'cognito-identity.amazonaws.com:amr': unauthenticated
      Policies:
      - PolicyName: 'CognitoUnAuthorizedPolicy'
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Effect: 'Allow'
            Action:
            - 'mobileanalytics:PutEvents'
            - 'cognito-sync:*'
            - 'cognito-identity:*'
            Resource: '*'
          # Allow users to upload attachments to their
          # folder inside our S3 bucket
          - Effect: 'Allow'
            Action:
            - 's3:PutObject'
            Resource:
            - Fn::Join:
              - ''
              -
                - Fn::GetAtt: [MediafilesBucket, Arn]
                - '/submissions/'

1 个答案:

答案 0 :(得分:0)

固定。

我注释掉了serverless.yml与已部署(销毁)的身份池相关的部分,然后取消了对该部分的注释,重新部署并从备份中还原。

这似乎有点破解,但它确实有效。

我还觉得应该应该一种通过cloudformation编辑身份池角色的方法...