0
votes

Is it possible to separate my code and infrastructure in serverless framework?

I'm using serverless framework (https://serverless.com) to deploy my AWS resources. I have a lambda functions defined in a serverless.yml file like this:

functions:
   hello:
   handler: handler.hello
   ...

Now I want to separate my infrastructure (serverless.yml) from my code. There would be two separated git projects: one for the infrastructure and one for the lambda (the code part).

This would be the development flow: Changes made in lambda project (git merge) would trigger a CI/CD pipeline to pull the code do the necessary checks (hint, test etc) and deploy the lambda to an s3 bucket.

After this I could include the s3 as a source of my lambda function and update the stack with the function that includes the changes. Something like this:

functions:
   hello:
   handler: function-s3-location
   ...

My questions:

1) Is it a good CI/CD approach?

2) Is it possible with serverless framework or it is possible only using AWS::Lambda::Functions code attribute to specify the s3 bucket?

Thank you!

1

1 Answers

0
votes

Regardless your decision, in the end, it will all be an architectural decision, which should be made considering processes, team experience, culture, architectural knowledge, etc.

To give you a bit of insight, you can structure your project in a way you have your functions in a folder and any other module in a diff folder. What you need to be careful is that you define the "package.include" and/or "package.exclude" in your config file.

Moreover if by structure you mean all the CloudFormation resources, you have a section for it in your serverless.yml config files. If it starts getting too big, you could as well use an extra config file and import it inside your serverless.yml

Ex:

├── config //configs based on your stage
 ├── dev.json
 ├── prod.json
├── functions
 ├── functionsA
  ├── index.js
 ├── functionsB
  ├── index.js
├── tests
 ├── unit.test.js
├── package.json
├── serverless.yml
├── serverless.resources.yml

The benefits of having everything in one repo:

  • You keep all resources under the same project, which means that if you "sls remove" the command will take care of deleting all of your resources. Same for lambdas.
  • It is easy for new team members to understand your architecture.
  • You have all code you need to start a local instance of your API and execute integrated tests. Useful for CI.
  • You will have only one repo to manage during your pipeline.
  • You will not have to manage the 2 repos in order to assess they are "walking" together.
  • Serverless allow you to push/deploy just the functions if you don't want to update your cloud architecture.
  • The provider will manage the bind between all your resources. Ex: You will not have to manage that the apiGateway on stage DEV, should invoke the lambdas pointing to the DV DB or containing the environmental variable with the value DEV.
  • The least you keep from your own manual management, the better.

I hope those reasons are enough for you to make your decision.