如何使用Elastic Beanstalk设置aws-sdk凭据?

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

我在Elastic Beanstalk中运行一个快速应用程序,在一个路由中我使用aws-sdk向sns发布通知。

这在本地运行时有效,但在Elastic Beanstalk环境中,如何设置凭证'myprofile'?

router.post('/publish', async (req, res) => {
  var AWS = require('aws-sdk')
  AWS.config.update({region: 'us-east-2'})
  // myprofile exists locally, but how do I deal with this in the elastic beanstalk environment?
  var credentials = new AWS.SharedIniFileCredentials({profile: 'myprofile'})
  AWS.config.credentials = credentials
  //...more stuff
})
amazon-web-services aws-sdk amazon-elastic-beanstalk
1个回答
0
投票

您可以使用IAM实例配置文件为ec2实例提供权限,因此当您的应用程序加载SDK时,将自动加载传递的凭据。

检查此链接https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/concepts-roles.html - 您需要的是实例配置文件。要创建实例配置文件,请转到IAM控制台>角色,然后选择服务EC2作为可承担此角色的服务。然后附上应用程序需要调用的策略(SNS内容)。

在beanstalk设置上,在安全性下,您将能够设置刚刚创建的IAM实例配置文件 - 因此此环境中的实例应具有与之关联的角色。

您的代码应如下所示:

router.post('/publish', async (req, res) => {
  var AWS = require('aws-sdk')
  AWS.config.update({region: 'us-east-2'})

  //...more stuff
}) 

还要检查是否可以要求并设置控制器外的区域;)

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