Unresolved resource dependencies [bucket name] in the resource block of the template

My serverless configuration fails to deploy and I keep getting a

The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [aips-uploads-staging] in the Resources block of the template

My serverless configuration,

service: filetype

provider:
  name: aws
  runtime: go1.x
  iamRoleStatements:
    - Effect: Allow
      Action:
        - "s3:getObject"
      Resource:
        Fn::Join:
          - ""
          - - "arn:aws:s3:::"
            - "Ref" : "${file(./config.yml):${opt:stage}.AIPS_UPLOADS_BUCKET}"
            - "/*"
    - Effect: Allow
      Action:
        - "s3:ListBucket"
        - "s3:PutObject"
        - "s3:GetBucketLocation"
      Resource:
        Fn::Join:
          - ""
          - - "arn:aws:s3:::"
            - "Ref": "ServerlessDeploymentBucket"
            - "/*"
  stage: ${opt:stage}
  region: ${file(./config.yml):${opt:stage}.REGION}
  environment:
    AIPS_UPLOADS_BUCKET: ${file(./config.yml):${opt:stage}.AIPS_UPLOADS_BUCKET}

package:
 exclude:
   - ./**
 include:
   - ./filetype

functions:
  aips:
    handler: filetype
    memorySize: 128
    events:
      - s3:
          bucket: ${file(./config.yml):${opt:stage}.AIPS_UPLOADS_BUCKET}
          event: s3:ObjectCreated:*
    timeout: 40
    environment:
      HOST: ${file(./config.yml):${opt:stage}.HOST}
      USER: ${file(./config.yml):${opt:stage}.USER}
      PASS: ${file(./config.yml):${opt:stage}.PASS}

resources:
  Resources:
    AipsUploadsBucket:
      Type: AWS::S3::S3Bucket
      Properties:
        Bucket: ${file(./config.yml):${opt:stage}.AIPS_UPLOADS_BUCKET}
    AipsBucketPolicy:
      Type: AWS::S3::BucketPolicy
      DependsOn:
        - "AipsUploadsBucket"
      Properties:
        Bucket: "arn:aws:s3:::${file(./config.yml):${opt:stage}.AIPS_UPLOADS_BUCKET}"
        PolicyDocument:
          Statement:
            -
              Action:
                - "s3:putObject"
              Effect: "Deny"
              Resource: "arn:aws:s3:::${file(./config.yml):${opt:stage}.AIPS_UPLOADS_BUCKET}/*"
              Principal: "*"
              Condition:
                NotResource:
                  - "arn:aws:s3:::${file(./config.yml):${opt:stage}.AIPS_UPLOADS_BUCKET}/*.pdf"
                  - "arn:aws:s3:::${file(./config.yml):${opt:stage}.AIPS_UPLOADS_BUCKET}/*.jpg"

Stack Trace:

Error: The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [aips-uploads-staging] in the Resources block of the template
    at provider.request.catch (/usr/lib/node_modules/serverless/lib/plugins/aws/deploy/lib/validateTemplate.js:25:13)
From previous event:
    at AwsDeploy.validateTemplate (/usr/lib/node_modules/serverless/lib/plugins/aws/deploy/lib/validateTemplate.js:20:12)
From previous event:
    at AwsDeploy.BbPromise.bind.then (/usr/lib/node_modules/serverless/lib/plugins/aws/deploy/index.js:121:39)
From previous event:
    at Object.aws:deploy:deploy:validateTemplate [as hook] (/usr/lib/node_modules/serverless/lib/plugins/aws/deploy/index.js:117:10)
    at BbPromise.reduce (/usr/lib/node_modules/serverless/lib/classes/PluginManager.js:390:55)
From previous event:
    at PluginManager.invoke (/usr/lib/node_modules/serverless/lib/classes/PluginManager.js:390:22)
    at PluginManager.spawn (/usr/lib/node_modules/serverless/lib/classes/PluginManager.js:408:17)
    at AwsDeploy.BbPromise.bind.then (/usr/lib/node_modules/serverless/lib/plugins/aws/deploy/index.js:95:48)
From previous event:
    at Object.deploy:deploy [as hook] (/usr/lib/node_modules/serverless/lib/plugins/aws/deploy/index.js:91:10)
    at BbPromise.reduce (/usr/lib/node_modules/serverless/lib/classes/PluginManager.js:390:55)
From previous event:
    at PluginManager.invoke (/usr/lib/node_modules/serverless/lib/classes/PluginManager.js:390:22)
    at PluginManager.run (/usr/lib/node_modules/serverless/lib/classes/PluginManager.js:421:17)
    at variables.populateService.then.then (/usr/lib/node_modules/serverless/lib/Serverless.js:157:33)
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)
    at processImmediate [as _immediateCallback] (timers.js:745:5)
From previous event:
    at Serverless.run (/usr/lib/node_modules/serverless/lib/Serverless.js:144:8)
    at serverless.init.then (/usr/lib/node_modules/serverless/bin/serverless:43:50)
    at <anonymous>

I couldn’t find any documentation on specifying S3 buckets in resources and bucket policies so, I am stuck. Please help.

1 Like

Make sure that your variables are being resolved type:

serverless print

This will show you the serverless config with all the variables interpret and really help in your debugging effort.

This is telling you that you have a resource depending on another called aips-uploads-staging.

The docs for S3 Bucket Syntax can be found here:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html#aws-resource-s3-bucket-syntax

It looks like there is not a property called ‘Bucket’. If you’re trying to give it a ‘BucketName’ for a bucket that already exists, I can tell you now that you are going to run into errors saying something like ‘bucket already exists’. If that’s the case, checkout the plugin serverless-plugin-existing-s3:
https://www.npmjs.com/package/serverless-plugin-existing-s3

I’ve used it with success.

As for bucket policy docs, they can be found here:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-policy.html

A great way to find the docs you’re looking for is to google the resource type, i.e. AWS::S3::S3Bucket