简体   繁体   English

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

[英]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. 我正在使用AWS SAM Java无服务器应用程序。 I am using the eclipse AWS serverless plugin to create simple Dynamo DB based CRUD application. 我正在使用eclipse AWS无服务器插件来创建基于Dynamo DB的简单CRUD应用程序。 Application takes an http request and depending on the HTTP method tries the corresponding CRUD operation on DynamoDB. 应用程序接收一个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. 因此,除了我无法弄清楚如何将环境变量或属性文件传递给我的Lambda Java代码以确定lambda是在本地还是在AWS环境中运行之外,所有其他程序都工作正常。 Depending on that I want to use local Dynamo DB client or AWS DB client. 取决于我要使用本地Dynamo DB客户端还是AWS DB客户端。 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". 试图弄清楚如何通过这个环境变量“ profile”。 In SAM local run/debug config, I don't see any option to do that. 在SAM本地运行/调试配置中,我看不到任何选项可以执行此操作。

您应该能够在每次提交时依赖AWS_SAM_LOCAL=true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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