So I have a class and some constants defined that 10 or more lambda functions will need. Currently, I have packaged the common code into each lambda function. Unfortunately, if I change the common code I have to repackage all 10 lambda functions and upload the changes.
Ideas that I had considered:
Lambda return a class with defs and constants – not feasible, lambda returns JSON
Try to magically load the common code from S3 – (not sure how and do not
really like that there are multiple steps to update a lambda
function)
Packaged the common code into each lambda function – (current design)
What is the best method for referencing common python code for lambda?
The first thing I did was an automatic deployment script in python for my lambdas + API gateway (+ intern usage of S3 etc). You can deploy your lambda without changing your API gateway endpoint, and be doing so, all your lambdas can be updated in one click without modification of the interaction with outside of your AWS box.
Inside, you can manage the bounds with S3 or dynamoDB or anything else automatically. It's an investment at the beginning, but it's definitely worth it, even more in your case with many lambdas.
Your solution of a constant provider could be a short term good strategy, but you'll need to be sure that your old lambdas will work with your news constants, so either you are limited in your provider's evolutions, either you'll have to manage many versions of your provider. Lambdas are meant to be easily deployed and replaced.
Related
I have been checking some resources on internet and all the examples of lambda in AWS are very basic but I am not sure how we will modularize an application with multiples dependencies, for example in java we usually have some structure like this
packages
repository
controllers
..
..
And we place the code related to each logic inside the package, but now in AWS seems that is more like scripting that will glue the pieces than OOP that I am used to, so my question is how we handle (if apply) this relationships, because I have seen code that all the logic is in one lambda and that not seems the best way to go, for example if we had some functionality that fist authenticate, authorize, transform, call an external api, get the response and then do a call to a final rest endpoint, how we can split this, for example will be the same lambda with packages(directories) inside and we call to each other? or we have multiples lambdas each one with one purpose? and this will generate cold start for each lambda?
I was thinking in using layers, but seems very new and not sure if this is production ready feature and seems that is more related to reuse code that is common across all the environment that the way to modularize our code
Generally when you're developing Lambda functions, the function should have a single purpose (which will keep the function relatively small).
If you have multiple actions, by having each Lambda as its own function it will improve the development and deployment experience. Having a single developer working on the function reduces the risk of breaking unrelated functionality, whilst also allowing them to deploy only the function that they've worked on.
To orchestrate between Lambdas for APIs people tend to use API Gateway (be that for your clients communicating to the Lambdas, or between the Lambdas themselves).
Regarding any shared dependencies/libraries Lambda Layers as you mentioned is the correct way to go. It will allow you to centralise the dependencies that your applications share rather than the need to package the Lambda with a version of the dependencies each time.
There's an article on Best Practices for Developing on AWS Lambda that should offer additional guidance.
After doing some brief research, I'm receiving conflicting answers regarding best practices for the AWS lambda service. I'm writing a few microservices for my company that will automate the steps for adding clients to our various services: creating api keys, uploading documents to a repo, sending an email, etc.
I have copied and pasted my code for 3 lambdas now (only changing around a few variable values), but, before I start doing this for all of them, I wanted to request if anyone had an easier method. I do know about the ProxyIntegration, where I could use the same lambda for similar requests and differentiate them by their resource paths; however, is there an easier way I could "map" the lambdas to shared code?
I was thinking about using an S3 Object to hold the code, then change the variables by environment variables (which could very well work), but does anyone have any other recommendations or obvious solutions I'm not realizing?
Thanks!
There is a very recent feature called Lambda Layers that specifically allows you to share code between AWS Lambda functions.
You would build the common code as a library and deploy it as a Layer. Then each individual Lambda function would include that Layer.
Context:
I have a usecase where my backend service should compute 1 or more features, where each feature is a simple peace of computation (can be as simple as adding two numbers) and each feature takes input and return an output value, which can be boolean or a number. Client can actually request features (1 or 10 etc), also each feature can have multiple versions.
Design:
Lambda function seems like a good choice, since it supports versioning and takes care of scaling. In my design, one Lambda will receive the request and then call further lambda functions in parallel (Say user asked for 12 features, Lambda function L1 will invoke 12 Lambda functions in parallel) synchronously, and return all computed feature values as one response (HTTP). This way, all features can be versioned in their own Lambda functions.
Questions:
Is it ok to call a lambda function directly from another Lambda function? Is it a good usecase for using Lambda functions?
Thanks
I think Lambda would work just fine for your use case. For versioning, you could use the API versioning provided by API Gateway but I think that is a bit much for your case. Just create different functions.
Check out serverless.com. It is a solid framework and easy to get started with. It will take a lot of the work out of setting it up, plus you'll have your infrastructure as code.
Yes, it is okay to call lambdas from other Lambdas. There is not a 'clean' way to do that though. On the other hand, 'Step functions' may be what you need. Lambda support's chaining functions in a workflow. The previous lambda is not 'calling' the next function as much as proceeding to the next step in the workflow. The Serverless framework also supports using the method and can be configured in the serverless.yml config file
I have by mistake given wrong name to AWS Lambda function. Now, I wanted to change its name. I found from the given stackoverflow question that best way to do that is just create a new function and copy the exact same code into it.
Is it possible to rename an AWS Lambda function?
I am thinking to do that but I am just worried about data loss. Since my lambda is currently had 2 SNS triggers from where it is constantly receiving data. So, if I stop this lambda and create new one, I would lose some data during that time. Also, if I start the new lambda before deleting previous one, I would some entries in my datastore twice. So, is there any way I could use to get this done?
As #John Rotenstein said, it is not possible to rename an AWS Lambda. If you look at the documentation for Lambda (http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html) you will see that updating FunctionName requires replacement of the entity.
If you specify a name, you cannot perform updates that require replacement of this resource. You can perform updates that require no or some interruption. If you must replace the resource, specify a new name.
If you are working with more complex systems, as it seems due to your note of SNS triggers, I would highly encourage you to take a look at CloudFormation (https://aws.amazon.com/cloudformation/), which uses code to manage deployed services. This not only has the benefit of allowing easier updates, but also enables other fun things which are inherent with code, such as integration with a VCS.
As a data loss prevention strategy while you perform this migration, you can create a new Lambda and point it to a staging database, delete the old Lambda, repoint your new Lambda to your production database, and push updates from your staging database into your production database. Check out the import/export docs (http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBPipeline.html) to see one method in which you might perform data migration.
There is no rename function for an AWS Lambda function.
You could instead try creating an alias to a Lambda function that would allow both names to function simultaneously. (This is normally used when different versions exist.)
I'm trying to deploy an API suite by using Api Gateway and implementing code in Java using lambda. Is it ok to have many ( related, of course ) lambdas in a single jar ( what I'm supposing to do ) or it is better to create a single jar for each lambda I want to deploy? ( this will became a mess very easily)
This is really a matter of taste but there are a few things you have to consider.
First of all there are limitations to how big a single Lambda upload can be (50MB at time of writing).
Second, there is also a limit to the total size of all all code that you upload (currently 1.5GB).
These limitations may not be a problem for your use case but are good to be aware of.
The next thing you have to consider is where you want your overhead.
Let's say you deploy a CRUD interface to a single Lambda and you pass an "action" parameter from API Gateway so that you know which operation you want to perform when you execute the Lambda function.
This adds a slight overhead to your execution as you have to route the action to the appropriate operation. This is likely a very fast routing but nevertheless, it adds CPU cycles to your function execution.
On the other hand, deploying the same jar over several Lambda function will quickly get you closer to the limits I mentioned earlier and it also adds administrative overhead in managing your Lambda functions as that number grows. They can of course be managed via CloudFormation or cli scripts but it will still add an administrative overhead.
I wouldn't say there is a right and a wrong way to do this. Look at what you are trying to do, think about what you would need to manage the deployment and take it from there. If you get it wrong you can always start over with another approach.
Personally I like the very small service Lambdas that do internal routing and handles more than just a single operation but they are still very small and focused on a specific type of task be it a CRUD for a database table or managing a selected few very closely related operations.
There's some nice advice on serverless.com
As polythene say's, the answer is "it depends". But they've listed the pros and cons for 4 ways of going about it:
Microservices Pattern
Services Pattern
Monolithic Pattern
Graph Pattern
https://serverless.com/blog/serverless-architecture-code-patterns/