Upload file(s) to S3 Bucket on Deploy

I have added the following to my serverless.yml file to create an S3 bucket when my Alexa lambda function is deployed:

resources:
  Resources:
    SkillBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: alexa-starter-skill      
        AccessControl: PublicRead

I want to keep translations (json), audio and images separate from my lambda function as they can be independently updated. Now that I have a bucket created, what do I need to do so that when I execute serverless deploy specified files of various mime types are uploaded to specified bucket paths?

Is there already a serverless plugin that does this? Is this something that is available via CloudFormation?

Thanks,

Mark

From what I have seen from example code is that many choose to use the aws cli in their build options along with sls deploy in order to upload files to s3 with MIME types when they deploy. This is what I use in my work flow also, I have not come across a way to do this via cloud formation or directly through sls deploy.

Example uploading file to s3 bucket with css MIME via aws cli:
aws s3 cp /yourpath/style.css s3://yourbucket --content-type ‘text/css’

2 Likes

When you say “build options” are you talking about something built into sls or are you referring to npm scripts or tools like Grunt, Gulp or Webpack?

How would you tie this into sls deploy?
Do you have a sample on GitHub or elsewhere?

Thanks,

Mark

I refer to production build software like grunt, gulp and webpack. Here is one example of an implementation using webpack https://github.com/serverless/serverless-graphql/blob/master/package.json. In the package.json they use aws s3 sync to upload the frontend to s3 bucket and sls deploy in their webpack build options. I doing something similar but with bash scripting and Leiningen for clojurescript builds @ github.

In my serverless handler.js file I need to know the name of the S3 bucket and key that was created and synched in my npm script using the aws cli. I want this config info for the S3 bucket and key in only one location (my-config.js, my-config.json, package.json or other).

What ways would you recommend so that the config is defined once and can be used in my build/deploy scripts as well as the Lambda function code?

Unfortunately, I do not have enough experience with your requested use case to make a proper recommendation, hopefully someone else can chime in.

You have been awesome with the help so far. Thank you.

Hi there @rmtuckerphx
Not sure if you’re still searching for a solution to this, but you might want to check out the README for this example:

I think you are looking for something like serverless-finch.

Github: https://github.com/fernando-mc/serverless-finch
Blog Post: https://www.fernandomc.com/posts/deploy-your-website-serverless-finch-serverless-framework/

I can’t vouch for it completely yet, still implementing it myself.