Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you reuse S3 buckets when deploying Lambdas with Serverless?

Recently we have adopted Serverless to handle the deployment of Lambda functions in our AWS environment, but for every unique function that is deployed, a new S3 bucket is created. This is inefficient and having a single bucket for every stack that Serverless creates would be ideal. Is there any way to do this from within the serverless.yml file? I have attempted the following yml file configurations for resources without any success.

1 - Listing the bucket as a resource to use in the yml

resources:
  Resources:
    ServerlessBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: serverless-test-bucket

Output:

Serverless: Packaging service...
Serverless: Removing old service versions...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading service .zip file to S3...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
............Serverless: Deployment failed!

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

     An error occurred while provisioning your stack: ServerlessBucket
     - serverless-test-bucket already exists.

2 - Attempting to reference the bucket in the yml

resources:
  Resources:
    ServerlessBucket:
      Type: AWS::S3::Bucket
      Properties:
        Ref: serverless-test-bucket

Output:

Serverless: Packaging service...
Serverless: Removing old service versions...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading service .zip file to S3...
Serverless: Updating Stack...

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

     Template format error: Unresolved resource dependencies
     [serverless-test-bucket] in the
     Resources block of the template
like image 587
Raiju Avatar asked Oct 11 '16 13:10

Raiju


People also ask

Are S3 buckets serverless?

In the Serverless Framework, S3 is most often used as a source for events in Serverless functions. For example, a Serverless function can be triggered when an object in an S3 bucket is created or modified, with rules available to filter for the specific requests you want to call the functions.

How do I transfer files from S3 bucket to Lambda?

Add below Bucket Access policy to the IAM Role created in Destination account. Lambda function will assume the Role of Destination IAM Role and copy the S3 object from Source bucket to Destination. In the Lambda console, choose Create a Lambda function. Directly move to configure function.


1 Answers

This has been added in a recent release of Serverless, however that release breaks the deploy functionality on certain operating systems, so the release to go with is 1.1.0.

This is done inside the serverless.yml file by adding deploymentBucket as a field under provider. Example:

provider:
  name: aws
  runtime: python2.7
  stage: dev
  region: us-east-1
  deploymentBucket: bucketName
  iamRoleStatements:
    - Effect: "Allow"
      Action:
      -  "*"
      Resource: "*"
like image 73
Raiju Avatar answered Oct 23 '22 23:10

Raiju