代码之家  ›  专栏  ›  技术社区  ›  Ele

Springboot、千分尺和Aws Lambda函数

  •  0
  • Ele  · 技术社区  · 3 年前

    我正试图使用千分尺将指标发送到AWS cloudwatch,然而,我面临着AWS证书的问题。

    ERROR i.m.c.CloudWatchMeterRegistry - error sending metric data. 
    com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain: 
    [com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper@b23c49d: Failed to connect to service endpoint: , com.amazonaws.auth.profile.ProfileCredentialsProvider@7edf67de: profile file cannot be null]
        at com.amazonaws.auth.AWSCredentialsProviderChain.getCredentials(AWSCredentialsProviderChain.java:136)r 
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.getCredentialsFromContext(AmazonHttpClient.java:1257)r   
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.runBeforeRequestHandlers(AmazonHttpClient.java:833)r 
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:783)r    
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:770)r 
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:744)r  
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:704)r   
        at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:686)r  at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:550)r  
        at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:530)r  at com.amazonaws.services.cloudwatch.AmazonCloudWatchClient.doInvoke(AmazonCloudWatchClient.java:2587)r 
        at com.amazonaws.services.cloudwatch.AmazonCloudWatchClient.invoke(AmazonCloudWatchClient.java:2554)r   
        at com.amazonaws.services.cloudwatch.AmazonCloudWatchClient.invoke(AmazonCloudWatchClient.java:2543)r   
        at com.amazonaws.services.cloudwatch.AmazonCloudWatchClient.executePutMetricData(AmazonCloudWatchClient.java:2297)r 
        at com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient$27.call(AmazonCloudWatchAsyncClient.java:1215)r    
        at com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient$27.call(AmazonCloudWatchAsyncClient.java:1209)r    
        at java.base/java.util.concurrent.FutureTask.run(Unknown Source)r   
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)r at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)r    
        at java.base/java.lang.Thread.run(Unknown Source)r
    

    AmazonHttpClient正在尝试使用 EC2ContainerCredentialsProviderWrapper ProfileCredentialsProvider ,但在lambda环境中,凭据可以通过执行角色获得,而且我们还有特定的环境变量,称为 AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY .

    那么,有没有什么方法可以告诉千分尺使用不同的AwsCredentials提供程序,例如 EnvironmentVariableCredentialsProvider ?

    1 回复  |  直到 3 年前
        1
  •  0
  •   Alberto    3 年前

    你是否尝试过使用 Configuration 要更改的文件 Bean 为此?

    @Configuration
    public class ManualAWSCredentialProviderConfiguration {
      @Value("${AWS_ACCESS_KEY_ID}")
      protected String accessKey;
    
      @Value("${AWS_SECRET_ACCESS_KEY}")
      protected String secretKey;
    
      @Bean
      @Primary
      public AWSCredentialsProvider buildAWSCredentialsProviderManually() {
        return new AWSStaticCredentialsProvider(
          new BasicAWSCredentials(accessKey, secretKey)
        );
      }
    }
    
        2
  •  0
  •   Ele    3 年前

    经过一些研究,我能够通过创建一个自定义 CloudWatchMeterRegistry bean如下:

    @Bean
    @Primary
    public CloudWatchMeterRegistry customCloudWatchMeterRegistry(
            CloudWatchConfig config, Clock clock, AwsRegionProperties awsRegionProperties) {
    
        AmazonCloudWatchAsync amazonCloudWatchAsync = AmazonCloudWatchAsyncClient
                .asyncBuilder()
                .withCredentials(new EnvironmentVariableCredentialsProvider())
                .withRegion(awsRegionProperties.getStatic())
                .build();
    
        return new CloudWatchMeterRegistry(config, clock, amazonCloudWatchAsync);
    }
    

    正如您所看到的,现在我可以配置一个自定义凭据提供程序,在我的情况下 EnvironmentVariableCredentialsProvider .

    重要提示: bean的名称不应该是 cloudWatchMeterRegistry 因为这个类 org.springframework.cloud.aws.autoconfigure.metrics.CloudWatchExportAutoConfiguration 已经有一个具有该名称的已声明bean。

        3
  •  0
  •   Joker    3 年前

    添加时 aws-java-sdk-sts 到您的依赖项(以及将其放在类路径上)将导致扩展的提供者链。然后应该使用执行角色。

    对于maven:

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-sts</artifactId>
        <version>1.12.52</version>
    </dependency>
    

    对于渐变:

    implementation group: 'com.amazonaws', name: 'aws-java-sdk-sts', version: '1.12.52'
    
    推荐文章