Home > Software design >  aws cli command output as value in cloudformation template
aws cli command output as value in cloudformation template

Time:01-09

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

CodePudding user response:

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

  • Related