0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

yes, forgot to copy&paste it, but still how you reference it to output?
I've edited my response to include the answer.
I will get Requested attribute response_data['testList'] does not exist in schema for AWS::Lambda::Function. Rollback requested by user.
You only have to put 'testList' amd have to reference the CR. So '!GetAtt EC2CustomResource.testList'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.