简体   繁体   中英

Configuring values into AWS Lambda written in Java

My requirement is that I need to write a AWS Lambda function that needs to do some processing and then invoke a URL. These URLs would very from stack to stack.

So in stack 1 Lambda the URL can be http://do-something.com and in stack 2 the URL can be http://do-nothing.com

I would have my Lambda jar built out of Jenkins, so I cannot put those details within the Lambda Jar as well

My question is what are the ways of configuring such things in Lambda. One approach that I could think of is that put that URL in a file in a standard bucket and Lambda would read that everytime it is invoked. Seems to be non-efficient because it has to read that everytime.

Any other suggestions or recommended good practices.

With the advent of 'Environment variables' for AWS Lambda, this can be easily achieved using the same.

So using 'Environment variables' is the way to go for such scenarios

The way I've solved this is by using the name of the lambda function as a key into a DynamoDB table. Deployment information is encoded into the lambda function name when you deploy it to AWS.

For example, first the lambda would be deployed with a DEV tag attached to the name:

$ aws lambda update-function-code --function-name myLambda_DEV 
    --s3-bucket lambda_s3_release_bucket --s3-key myLambda-1.0.0.jar --publish

Then in the lambda, the function name is read from the context and used to read from the config table:

public Response handleRequest(Request request, Context context) {
    String functionName = context.getFunctionName();
    AmazonDynamoDBClient dbclient = new AmazonDynamoDBClient();
    DynamoDB configdb = new DynamoDB(dbclient);
    Table config = dynamoDB.getTable("config_" + functionName);
    String url = config.getItem("url");
    ...
}

And the DynamoDB has a table called config_myLambda_DEV that looks like this:

 name   | value
 -------+-------------------------
 url    |  http://do-something.com
 ...    |

While the call to DynamoDB (or S3, etc.) may be relatively slow compared to the rest of your lambda, in practice lambda functions get reused. You can load and cache the information on the first invocation, and use the values from memory on subsequent calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM