如何在无服务器框架中获取 AWS 账户 ID 作为自定义变量?
How to get AWS account id as custom variable in serverless framework?
在无服务器框架中,我想将部署桶设置为
<project_name>-<stage>-<account_id>
我可以使用自定义变量获取舞台,例如:
custom:
stage: ${opt:stage, self:provider.stage}
但是我怎样才能得到 aws 账户 ID?我已经尝试使用 serverless-pseudo-parameters,如下所示,但没有成功。
custom:
account_id: #{AWS::AccountId}
plugins:
- serverless-pseudo-parameters
有人可以帮我将帐户 ID 设置为自定义变量吗?
您应该可以按照下面的示例访问它们 https://serverless.com/framework/docs/providers/aws/guide/variables/
Resources:
- 'Fn::Join':
- ':'
- - 'arn:aws:logs'
- Ref: 'AWS::Region'
- Ref: 'AWS::AccountId'
- 'log-group:/aws/lambda/*:*:*'
看来你的语法有误。尝试
custom:
account_id: ${AWS::AccountId}
因为至少在您提供的示例中使用的是 #{AWS::AccountId}
注意到你的主题标签了吗?
根据documentation,要获取Account Id,可以使用外部js文件:
// myCustomFile.js
module.exports.getAccountId = async (context) => {
return context.providers.aws.getAccountId();
};
.
# serverless.yml
service: new-service
provider: aws
custom:
accountId: ${file(../myCustomFile.js):getAccountId}
对于使用 "assumed role" 使用 Serverless 的任何人,其中您的 IAM 用户是在主 AWS 账户中定义的,并且您正在尝试使用该子账户中的角色在子账户中进行部署:记录的解决方案 - 上面接受的答案中的那个 - 不起作用。
此处详细描述了此设置:https://theithollow.com/2018/04/30/manage-multiple-aws-accounts-with-role-switching/。将无服务器与配置为承担另一个帐户中定义的角色的 --aws-profile
一起使用时,sts.getCallerIdentity()
returns 来自默认配置文件的主帐户的帐户信息,而不是假定的帐户角色。
为了获取代入角色的帐户 ID(这是我们要部署到的位置),我执行了以下操作:
const { STS } = require('aws-sdk');
module.exports.getAccountId = async (context) => {
// This loads the AWS credentials Serverless is currently using
// They contain the role ARN of the assumed role
const credentials = context.providers.aws.getCredentials();
// init STS using the same credentials
const sts = new STS(credentials);
const identity = await sts.getCallerIdentity().promise();
return identity.Account;
};
编辑:
找到了一种更好的方法,它比 Serverless 文档中介绍的方法更简单,并且也可以很好地处理假设角色:
module.exports.getAccountId = async (context) => {
return context.providers.aws.getAccountId();
};
在无服务器框架中,我想将部署桶设置为
<project_name>-<stage>-<account_id>
我可以使用自定义变量获取舞台,例如:
custom:
stage: ${opt:stage, self:provider.stage}
但是我怎样才能得到 aws 账户 ID?我已经尝试使用 serverless-pseudo-parameters,如下所示,但没有成功。
custom:
account_id: #{AWS::AccountId}
plugins:
- serverless-pseudo-parameters
有人可以帮我将帐户 ID 设置为自定义变量吗?
您应该可以按照下面的示例访问它们 https://serverless.com/framework/docs/providers/aws/guide/variables/
Resources:
- 'Fn::Join':
- ':'
- - 'arn:aws:logs'
- Ref: 'AWS::Region'
- Ref: 'AWS::AccountId'
- 'log-group:/aws/lambda/*:*:*'
看来你的语法有误。尝试
custom:
account_id: ${AWS::AccountId}
因为至少在您提供的示例中使用的是 #{AWS::AccountId}
注意到你的主题标签了吗?
根据documentation,要获取Account Id,可以使用外部js文件:
// myCustomFile.js
module.exports.getAccountId = async (context) => {
return context.providers.aws.getAccountId();
};
.
# serverless.yml
service: new-service
provider: aws
custom:
accountId: ${file(../myCustomFile.js):getAccountId}
对于使用 "assumed role" 使用 Serverless 的任何人,其中您的 IAM 用户是在主 AWS 账户中定义的,并且您正在尝试使用该子账户中的角色在子账户中进行部署:记录的解决方案 - 上面接受的答案中的那个 - 不起作用。
此处详细描述了此设置:https://theithollow.com/2018/04/30/manage-multiple-aws-accounts-with-role-switching/。将无服务器与配置为承担另一个帐户中定义的角色的 --aws-profile
一起使用时,sts.getCallerIdentity()
returns 来自默认配置文件的主帐户的帐户信息,而不是假定的帐户角色。
为了获取代入角色的帐户 ID(这是我们要部署到的位置),我执行了以下操作:
const { STS } = require('aws-sdk');
module.exports.getAccountId = async (context) => {
// This loads the AWS credentials Serverless is currently using
// They contain the role ARN of the assumed role
const credentials = context.providers.aws.getCredentials();
// init STS using the same credentials
const sts = new STS(credentials);
const identity = await sts.getCallerIdentity().promise();
return identity.Account;
};
编辑:
找到了一种更好的方法,它比 Serverless 文档中介绍的方法更简单,并且也可以很好地处理假设角色:
module.exports.getAccountId = async (context) => {
return context.providers.aws.getAccountId();
};