Cat_Enthusiast
Cat_Enthusiast

Reputation: 15688

Serverless: Creating S3Bucket and BucketPolicy [Unresolved resource dependencies]

I'm trying to create an S3 Bucket and a corresponding Resource Policy in the same serverless.yml so that both are established on the new stack formation.

However, I am running into an error on build:

Unresolved resource dependencies [CUSTOM-BUCKETNAME] in the Resources block of the template

Is there to a way to synchronously create the policy so that it waits for the bucket to be created first? I'm setting this up in the resources section of my yml

resources:
  Resources:
    Bucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: CUSTOM-BUCKETNAME
    BucketPolicy:
      Type: AWS::S3::BucketPolicy
      Properties:
        Bucket: 
          Ref: CUSTOM-BUCKETNAME
        PolicyDocument:
          Statement:
            - Principal: 
                Service: "ses.amazonaws.com"
              Action:
                - s3:PutObject
              Effect: Allow
              Sid: "AllowSESPuts"
          Resource: 
            Fn::Join: ['', ['arn:aws:s3:::', Ref: "CUSTOM-BUCKETNAME", '/*'] ]   

Above is a small snippet of my yml configuration.

After using DependsOn, I'm still getting the same error. Worth note, the resource dependency refers to the dynamic name (CUSTOM-BUCKETNAME) of the bucket.

resources:
  Resources:
    Bucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: CUSTOM-BUCKETNAME
    BucketPolicy:
      Type: AWS::S3::BucketPolicy
      DependsOn: Bucket
      Properties:
        Bucket: 
          Ref: CUSTOM-BUCKETNAME
        PolicyDocument:
          Statement:
            - Principal: 
                Service: "ses.amazonaws.com"
              Action:
                - s3:PutObject
              Effect: Allow
              Sid: "AllowSESPuts"
          Resource: 
            Fn::Join: ['', ['arn:aws:s3:::', Ref: "CUSTOM-BUCKETNAME", '/*'] ]

CUSTOM-BUCKETNAME is never explicity hardcoded in the yml itself, its a dynamically generated name using template literals.

Upvotes: 0

Views: 304

Answers (2)

pkarfs
pkarfs

Reputation: 1039

Issue is occurring on your policy as your bucket is: BucketName: CUSTOM-BUCKETNAME

Not a referenced parameter. Which means your not referencing the actual resource in the policy statement when your using Bucket: Ref: CUSTOM-BUCKETNAME.

Instead, either change the bucket name to reference the same parameter BucketName: Ref: CUSTOM-BUCKETNAME or change the policy to reference the resource: Bucket: Ref: Bucket

Upvotes: 2

Binh Nguyen
Binh Nguyen

Reputation: 2157

CloudFormation DependsOn attribute should solve your problem.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html

Upvotes: 2

Related Questions