阅读背景:

如何在Java AWS无服务器框架设置下确定Lambda是在本地运行还是在AWS下运行

来源:互联网 

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.

我正在使用AWS SAM Java无服务器应用程序。我正在使用eclipse AWS无服务器插件来创建基于Dynamo DB的简单CRUD应用程序。 Application接受http请求,并根据HTTP方法在DynamoDB上尝试相应的CRUD操作。

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:

所以一切都很好,除了我无法弄清楚如何将环境变量或属性文件传递给我的Lambda java代码以确定lambda是在本地运行还是在AWS环境中运行。取决于我想要使用本地Dynamo DB客户端或AWS DB客户端。以下是代码段:

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

    if("local".equalsIgnoreCase(environment)) {
        dynamoDBclient = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
                new AwsClientBuilder.EndpointConfiguration("https://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.

试图找出如何通过这个环境变量“配置文件”。在SAM本地运行/调试配置中,我没有看到任何选项来执行此操作。

1 个解决方案

#1


2  

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

根据此提交,您应该能够依赖AWS_SAM_LOCAL = true。


分享到: