Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether Lambda is running locally or under AWS under Java AWS serverless framework setup

I am playing with AWS SAM Java serverless application. I am using the eclipse AWS serverless plugin to create simple Dynamo DB based CRUD application. Application takes an http request and depending on the HTTP method tries the corresponding CRUD operation on DynamoDB.

So all is working good except that I am not able to figure out how to pass an environment variable or a property file to my Lambda java code to determine whether lambda is running locally or in AWS environment. Depending on that I want to use local Dynamo DB client or AWS DB client. Here is the code snippet for that:

    String environment = System.getenv("profile");
    AmazonDynamoDB dynamoDBclient = null;

    if("local".equalsIgnoreCase(environment)) {
        dynamoDBclient = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
                new AwsClientBuilder.EndpointConfiguration("http://172.16.123.1:8000", "local"))
                .build(); 
    } else {
        dynamoDBclient = AmazonDynamoDBClientBuilder.standard().build();
    }

    dynamoDBMapper = new DynamoDBMapper(dynamoDBclient);

Trying to figure out how to pas this environment variable "profile". In SAM local run/debug config, I don't see any option to do that.

like image 441
Juned Ahsan Avatar asked Sep 17 '25 16:09

Juned Ahsan


1 Answers

You should be able to rely on AWS_SAM_LOCAL=true per this commit.

like image 54
jarmod Avatar answered Sep 20 '25 05:09

jarmod