aws cli命令输出作为CloudFormation模板中的值

huangapple go评论73阅读模式
英文:

aws cli command output as value in cloudformation template

问题

我有一个独立的Lambda层,ARN是使用以下CLI命令检索的。

aws lambda list-layer-versions --layer-name my-custom-lambda-layer --region us-east-1 --query 'LayerVersions[0].LayerVersionArn'

我该如何将这个输出引用到我的云堆栈模板中,如下所示,

Resources:
  Parameters:
    MYLAYERARN: $(aws lambda list-layer-versions --layer-name my-custom-lambda-layer --region us-east-1 --query 'LayerVersions[0].LayerVersionArn')

或者直接在我的任何Lambda函数中使用,如下所示,

Resources:
  MyLambdaFuntion:
    handler: Hello.lambda_handler
    timeout: 60
    memorySize: 256
    layers:
      - $(aws lambda list-layer-versions --layer-name my-custom-lambda-layer --region us-east-1 --query 'LayerVersions[0].LayerVersionArn')

目前它不执行AWS CLI命令,而是将CLI命令作为值。

英文:

I have an independent lambda layer, the arn is retrieved using the below CLI command.

aws lambda list-layer-versions --layer-name my-custom-lambda-layer --region us-east-1 --query 'LayerVersions[0].LayerVersionArn'

How can I refer this output to my cloud formation template, like below,

Resources:
  Parameters:
    MYLAYERARN: $(aws lambda list-layer-versions --layer-name my-custom-lambda-layer --region us-east-1 --query 'LayerVersions[0].LayerVersionArn')

Or use it directly in any of my lambda function as below,

Resources:
  MyLambdaFuntion:
    handler: Hello.lambda_handler
    timeout: 60
    memorySize: 256
    layers:
      - $(aws lambda list-layer-versions --layer-name my-custom-lambda-layer --region us-east-1 --query 'LayerVersions[0].LayerVersionArn')

Currenlty it is not executing the AWS CLI command, but taking the CLI command as the value

答案1

得分: 4

这是不可能的,因为你无法在CloudFormation模板中评估这种表达式。

最简单的解决方案是将已经评估过的表达式作为参数传递。

或者,如果你必须使用CloudFormation解决方案,那么你可以利用CloudFormation宏来调用一个Lambda函数,该函数执行自定义代码(在这种情况下,代码将具有等效于AWS CLI命令的SDK代码)。

更多关于CloudFormation宏的信息:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html

宏示例:https://stackoverflow.com/a/70475459/3390419

英文:

That is not possible, as you can't evaluate such expressions in a CloudFormation template.

The easiest solution would be to pass in the already-evaluated expression as a parameter.

Alternatively, if you must use a CloudFormation solution, then you could leverage a CloudFormation macro to invoke a lambda function which executes custom code (in this case, the code would have the SDK equivalent of the AWS CLI command).


More on CloudFormation macros:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html

Macro example: https://stackoverflow.com/a/70475459/3390419

huangapple
  • 本文由 发表于 2023年1月9日 00:28:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定