我想动态构建我的文件中的ARN,但我需要获取当前的AccountId。我该如何将其作为变量访问?
例如:
example: arn:aws:states:${region}:${accountId}:stateMachine:${self:service}-${self:custom.stage}-example
如何正确引用当前的region
和accountId
?
我想动态构建我的文件中的ARN,但我需要获取当前的AccountId。我该如何将其作为变量访问?
例如:
example: arn:aws:states:${region}:${accountId}:stateMachine:${self:service}-${self:custom.stage}-example
如何正确引用当前的region
和accountId
?
现在,在Serverless Framework中已经原生支持此功能。
functions:
hello:
handler: my-function.handler
environment:
var: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/*:*:*'
iam:
role:
statements:
- Effect: Allow
Action:
- dynamodb:*
Resource: !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${AWS::StackName}-*
请参阅伪参数参考获取官方文档。
!Sub
。 - C.Lee从2.3.0版本开始,这现在可以使用原生支持。
只需通过${aws:accountId}
引用它。您还可以通过${aws:region}
引用区域。文档在这里:https://www.serverless.com/framework/docs/providers/aws/guide/variables#referencing-aws-specific-variables
service: new-service
provider: aws
functions:
func1:
name: function-1
handler: handler.func1
environment:
ACCOUNT_ID: ${aws:accountId}
REGION: ${aws:region}
AWS::AccountId
),并借助于!Sub
函数。我的答案本地支持${aws:accountId}
,但仅在serverless版本>=2.3.0上支持。https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html - John Paulo Rodriguez!Sub
函数是不相关的。然而,使用更新后的aws:accountId
确实更好。 - V Maharajh有一个方便的无服务器插件 https://www.npmjs.com/package/serverless-pseudo-parameters,它可以添加引用AWS参数(例如区域和帐户ID)的功能。我刚刚开始使用,效果非常好。
Resource: !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${AWS::StackName}-*
。您可以在此处查看完整的serverless.yml文件:
https://github.com/vivmaha/blog-server/blob/main/serverless.yml - V Maharajhnpm run sls -- package --stage dev
,它对您无法成功?SamJ的建议也很有前途,我认为他们是想告诉您而不是告诉我。 - V Maharajh由于这些变量在CloudFormation中定义但未在serverless中公开,因此Serverless本身无法引用这些变量。
如果您需要在资源部分中使用它们,则可以通过“Ref”调用直接访问它们。
如果您需要将这些变量作为函数环境变量,请使用CloudFormation代码覆盖由Serverless生成的函数代码。
因此,要实现这一点,您必须按照以下模式修改serverless.yml。
functions:
hello:
handler: handler.hello
resources:
Resources:
HelloLambdaFunction:
Type: AWS::Lambda::Function
Properties:
Environment:
Variables:
accountId:
Ref: AWS::AccountId
region:
Ref: AWS::Region
arn:
Fn::Join:
- ""
- - "arn:aws:states:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- ":stateMachine:"
- ${self:service}
- "-"
- ${self:custom.stage}
- "-example"
AWS::AccountId
和 AWS::Region
,但你不能像使用 ${AWS::AccountId}
一样在 serverless.yml 文件中使用它们。这些变量不被支持。
@jens 的答案 是正确的。你必须使用 CloudFormation 语法。在下面的示例中,我提供了另一种使用 CloudFormation 的方式。service: testing-aws-account-id
provider:
name: aws
runtime: nodejs4.3
region: us-east-1
iamRoleStatements:
- Effect: "Allow"
Action:
- "iot:Publish"
Resource: 'Fn::Join: ["", [ "aws:iot:", { "Ref": "AWS::Region" }, ":", { Ref: "AWS::AccountId" }, ":topic/foo" ]]'
functions:
publishIot:
handler: handler.publishIot
这一行:
Resource: 'Fn::Join: ["", [ "aws:iot:", { "Ref": "AWS::Region" }, ":", { Ref: "AWS::AccountId" }, ":topic/foo" ]]'
Resource: "arn:aws:iot:us-east-1:1234567890:topic/foo"
Resource: '!Sub arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topic/foo'
? - BamaPookie