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.

posta 2 anni fa255 visualizzazioni
2 Risposte
0
Risposta accettata

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.

con risposta 2 anni fa
profile picture
ESPERTO
verificato un mese fa
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
TECNICO DI SUPPORTO
con risposta 2 anni fa
  • 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.

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande