Home > other >  How to reference Lambda response data to Output in CloudFormation
How to reference Lambda response data to Output in CloudFormation

Time:10-21

Is it possible to reference the Lambda response_data or return or any other variable value into the CloudFormation Output? so I can export the output for using as Cross Stack Referencing.

Resources:
  EC2CustomResource:
    Type: Custom::EC2CustomResource
    Properties:
      ServiceToken: !GetAtt AWSLambdaFunction.Arn
        
  AWSLambdaFunction:
     Type: AWS::Lambda::Function
     Properties:
       Description: "bucket!"
       FunctionName: !Sub '${AWS::StackName}-${AWS::Region}-lambda'
       Handler: index.handler
       Timeout: 360
       Runtime: python3.9
       Code:
         ZipFile: |
           import boto3
           import cfnresponse   
           
           def handler(event, context):
           
               response_data = {}
               s3 = boto3.client('s3')
               
               # dummy test
               testList = [1, 2, 3]
               
               try:
                   print("Execution succesfull!")
                   response_data['testList'] = testList
                   cfnresponse.send(event, context, cfnresponse.SUCCESS, response_data)
               except Exception as e:
                   print("Execution failed...")
                   response_data['Data'] = str(e)
                   cfnresponse.send(event, context, cfnresponse.FAILED, response_data)

# This is not working
Outputs:
  LambdaFunctionOutput: 
    Value: !GetAtt AWSLambdaFunction.response_data['testList']
    Description: Return Value of Lambda Function

CodePudding user response:

The template you've specified only creates a Lambda function. It will not execute this lambda function. If you want to execute a lambda function and reference some return value, you'll have to use a custom resource backed by a lambda function.

Edit #1

The response data you've defined and passed along using the cfnresponse library, can be retrieved using the cfn !GetAtt function as stated in the documentation.

Data: Optional. The custom resource provider-defined name-value pairs to send with the response. You can access the values provided here by name in the template with Fn::GetAtt.

  • Related