Custom resource Outputs not found

0

I've written a custom resource in Go using cloudformation-cli-go-plugin, it's failing when I try and use it in a stack with

Unable to retrieve Guid attribute for MyCo::CloudFormation::Workloads, with error message NotFound guid not found.

The stack:

AWSTemplateFormatVersion: 2010-09-09
Description: Sample MyCo Workloads Template
Resources:
  Resource1:
    Type: 'MyCo::CloudFormation::Workloads'
    Properties:
      APIKey: ""
      AccountID: ""
      Workload: >-
        workload: {entityGuids: "", name: "CloudFormationTest-Create"}
Outputs:
  CustomResourceAttribute1:
    Value: !GetAtt  Resource1.Guid

If I remove the Outputs stanza the stack runs successfully and I can see the created resource.

Running with SAM locally I've verified that Guid is in fact always returned. FWIW the resource passes all of the contract tests, Guid is the primaryIdentifier, and is a readOnlyProperties.

I've tried several tests playing with the !GetAtt definition, all of which fail with schema errors so it appears the CF is aware of the format of the resource's properties.

Suggestions and/or pointers would be appreciated.

질문됨 2년 전255회 조회
2개 답변
0
수락된 답변

The issue here was Read failing due to CloudFormation behaving differently than the Contract Tests. The Contract Tests do not follow the CloudFormation model rules, they are more permissive.

답변함 2년 전
profile picture
전문가
검토됨 한 달 전
0

You can pass the output values from the custom resource in the response data being returned as a parameter to the custom resource response objects

Replicating the scenario at my end, I tried to create a sample lambda backed custom resource responsible for creation of a new EC2 instance and then returned the value of the ‘InstanceId' as a return value.

Resources:
  LambdaFunction:
    Type: 'AWS::Lambda::Function'
    Properties:
      FunctionName: executeFn
      Timeout: 900
      Code:
        ZipFile: |
          import boto3, json
          import cfnresponse
          def handler(event, context):
              ec2 = boto3.client('ec2')
              print ("Createing new instance:")
              instance = ec2.run_instances(
                ImageId='ami-061e2417489356de3',
                InstanceType='t2.micro',
                MaxCount=1,
                MinCount=1
              )
              instance_id = instance['Instances'][0]['InstanceId']
              responseValue = instance_id
              responseData = {}
              responseData['Data'] = responseValue
              cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
      Handler: index.handler
      Role: 'arn:aws:iam::xxxxxxxxxxxx:role/testLambda'
      Runtime: python3.7
  CustomResource:
    Type: 'AWS::CloudFormation::CustomResource'
    DependsOn: LambdaFunction
    Version: '1.0'
    Properties:
      ServiceToken: !GetAtt LambdaFunction.Arn
      Description: Custom resource to invoke lambda
Outputs:
  InstanceId:
    Value: !GetAtt CustomResource.Data

Now, here in this example, you can refer that lambda function associated with the custom resource returns the instance id as a value in the Data which is then displayed as an output to the stack created using the GetAtt intrinsic function.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-ref-responses.html

profile pictureAWS
지원 엔지니어
답변함 2년 전
  • Thanks for the response.

    The question isn't how to return Outputs from a custom resource, it's why aren't the Outputs showing up in CF even though SAM and test-type indicate that they are.

    Is there a way to dump the response object CF is receiving from the custom resource?

    Thanks again for the response.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠