0

I have a lambda function which is pretty general. It queries an Athena Database table, and writes the output to an S3 Bucket. The function is currently set up to accept environment variables for the Database name, table name, output filename and output bucket.

Currently, I call this function with 4 different sets of environment variables, so I have 4 different lambdas, whose code is the same, but whose environment variables are different. Do I need to have 4 different lambdas, or is there a way of having one lambda with 4 environment variable 'sets'?

Thanks!

1
  • 1
    Why not keep the env values in SSM Parameter store instead? This way a single function can fetch what it needs. Commented Jan 20, 2022 at 22:08

1 Answer 1

1

Here's one option: To handle 4 sets of configuration with a single lambda, send a variable (e.g. type: Foo) part of the lambda invocation1. As @Marcin suggests, the lambda uses the type value to fetch the config variables from the SSM Parameter Store at runtime with the GetParametersByPath API. Parameters support hierarchies, so you can store your config using nested variable names like: /Foo/db, /Foo/table, /Bar/table etc.


(1) For example, send type in the event detail if event-triggered, or in the SDK Invoke command payload.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks @fedonev. This works for me. I don't need to use SSM Parameter store though since the parameters aren't confidential. I can simply pass the parameters via Input: in Cloud Formation JSON. Was this the reason you recommended using it or is there another reason?
You're welcome, @GideonDeveloper. SSM Parameters decouple configuration from code. That's not always super important, though. Glad you found a solution that works for you!
I think this relates to what I am wondering about which is if I am able to pass environment variables via events, then I can go from having 4 lambdas to 1. That is, 4 events pointing to 1 lambda. Or I could have one event (since the timing of the functions is all the same) that triggers 4 lambdas where the environment variables are stored in the lambdas. Are you indicating that using SSM Parameters will allow me to have one event and one lambda since I can store the different parameters there? I thought that SSM PS was just for security, like Secrets Manager, but that doesn't seem so.
Right, @GideonDeveloper, the Param Store is for any config. Your Lambda needs to know (A) which scenario(s) to run and (B) the actual config values. Because your answer to A is "always all 4", you are also right that 1 event and 1 lambda works. What about B? My answer suggests saving config values as Params. Your Lambda calls GetParametersByPath at runtime to lookup the values from the Param Store. Or you could pass the config values in the event detail. Also works.
@GideonDeveloper agreed on all but the 1st sentence. Config for all scenarios could fit into in a single event. Your 1 event detail would be a stringified JSON array of scenario configuration objects. That aside, the rest makes sense. Why not take this opportunity to learn a new technology? Welcome to Team Parameter Store!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.