AWS Lambda自定义触发器 [英] AWS Lambda custom triggers

查看:429
本文介绍了AWS Lambda自定义触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何在AWS Lambda函数中访问AWS凭证吗?

Can someone tell me how to get access to AWS credentials in an AWS Lambda function?

我已经在互联网上进行了彻底的搜索,但是仍然找不到任何可以帮助我的东西.

I've searched the internet thoroughly but still haven't found anything that has helped me.

我正在用Java编写函数.我认为我应该可以使用HandleRequest方法中的上下文对象访问凭据.

Im writing the function in Java. I think I should have access to the credentials with the context object in the HandleRequest method.

如果有帮助,我想调用DynamoDB客户端并将记录上载到数据库.

If it helps, I want to invoke a DynamoDB client and upload a record to the database.

推荐答案

我最近也遇到了同样的问题. 我认为,这当然是AWS的Java Lambda文档中的一个盲点.

I came into the same problem myself recently. This certainly is a blind spot in AWS's Lambda documentation for Java, in my opinion.

假设您使用的是适用于Java文档的AWS开发工具包,此Java代码段将为您工作:

This snippet in Java should work for you, assuming you're using the AWS SDK for Java Document API :

DynamoDB dynamodb = new DynamoDB(
    new AmazonDynamoDBClient(new EnvironmentVariableCredentialsProvider()));

主要收获是使用EnvironmentVariableCredentialsProvider访问所需的凭证,以访问AWS Lambda容器内的其他AWS资源. Lambda容器附带凭据作为环境变量,这足以检索它们.

The main takeaway is to use the EnvironmentVariableCredentialsProvider to access the required credentials to access your other AWS resources within the AWS Lambda container. The Lambda containers are shipped with credentials as environment variables, and this is sufficient in retrieving them.

注意:这将创建一个DynamoDB实例,该实例仅看到默认区域中的资源.要为特定区域创建一个,请使用以下方法(假设您要访问ap-northeast-1区域中的DynamoDB的数据库):

Note: This creates a DynamoDB instance that only sees resources in the default region. To create one for a specific region, use this (assuming you want to access DynamoDB's in the ap-northeast-1 region):

DynamoDB dynamodb = new DynamoDB(
    Regions.getRegion(Regions.AP_NORTHEAST_1).createClient(
        AmazonDynamoDBClient.class,
        new EnvironmentVariableCredentialsProvider(),
        new ClientConfiguration()));

这篇关于AWS Lambda自定义触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆