AWS Java-承担从lambda调用API网关的角色

问题描述 投票:2回答:1

我如何承担来自lambda的角色,以使用Java SDK调用API网关

aws-lambda aws-api-gateway assume-role
1个回答
0
投票

我假设您是指API Gateway生成的Java SDK。客户端构建器具有方法public void setIamCredentials(AWSCredentialsProvider iamCredentials)。您可以将可以如下生成的AWSStaticCredentialsProvider传递给该方法:

AWSSecurityTokenService client = AWSSecurityTokenServiceClientBuilder.standard().build();
AssumeRoleRequest request = new AssumeRoleRequest().withRoleArn(iamRole)
            .withRoleSessionName(yourRoleName);

AssumeRoleResult response = client.assumeRole(request);

return new AWSStaticCredentialsProvider(
            new BasicSessionCredentials(
                    response.getCredentials().getAccessKeyId(),
                    response.getCredentials().getSecretAccessKey(),
                    response.getCredentials().getSessionToken()));

请记住,您将需要在角色上设置IAM策略,以使其能够访问您的API网关:https://docs.aws.amazon.com/apigateway/latest/developerguide/permissions.html

© www.soinside.com 2019 - 2024. All rights reserved.