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
专家
已审核 1 个月前
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.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则