0
votes

I'm trying to use S3 service in my project.

I save custom variable & use in my provider property:

service: snapnext

custom:
  imagesBucketName: snapnext-images

provider:
  name: aws
  runtime: nodejs6.10
  iamRoleStatements: # IAM permissions granted to all functions
    - Effect: Allow
      Action:
        - S3:GetObject
      Resource:
        - Fn::Join:
            - ''
            - - 'arn:aws:s3:::'
              - '${self:custom.imagesBucketName}/*'
  environment:
    IMAGES_BUCKET_NAME: ${self:custom.imagesBucketName}

functions:
  downloadImage: # Define a new Function
    handler: functions/downloadImage.handler
    events:
      - http:
          path: images
          method: post

resources:
  Resources:
    ImagesBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:custom.imagesBucketName}
        AccessControl: PublicRead

plugins:
  - serverless-offline

When I'm trying to deploy, I'm always getting an error message like this enter image description here

Any idea why this happens? And how to better way to debug in a serverless framework, especially for .yml file?

2
Did you create the snapnext-images bucket before you tried to deploy your serverless application? For example through the web console or in a similar manner? - Milan Cermak

2 Answers

1
votes

You've configured serverless to create a bucket with the name snapnext-images and when it tries to do it it can't because... it already exists.

Provided the bucket name is available (bucket names are shared with everyone), either:

  • Ensure the bucket doesn't exist before first deployment so serverless can do its job; or
  • Don't define a bucket for creation and use your existing bucket; or
  • Pick a different name if you want to create a new bucket and keep the existing bucket.

This has nothing to do with custom variables.

0
votes

You are trying to create the s3 resource when you deploy your stack. You will get this error only of the bucket name already exists. The bucket names should be unique across regions and accounts. You need to be careful/mindful with creating s3 bucket resources in cloud formation.

  • Make sure it doesn't exist before deployment Many times deleting stack doesn't delete the S3 bucket created with the stack if the bucket has objects/files in it. You need to write a custom lambda trigged on delete bucket event to delete all the contents before deleting the bucket.