Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(CDK Migrate CLI): CDK Migrate command does not filter out type Custom::* resource with flag --filter resource-type-prefix #29249

Open
jiem-ying opened this issue Feb 25, 2024 · 3 comments
Assignees
Labels
bug This issue is a bug. cli Issues related to the CDK CLI effort/medium Medium work item – several days of effort p1 package/tools Related to AWS CDK Tools or CLI toolkit/migrate Related to cdk migrate

Comments

@jiem-ying
Copy link

Describe the bug

I have an existing stack with one CR and one Lambda function. Following commands all face the same error:

cdk migrate --language typescript --from-stack --stack-name 'CustomResourceCrossRegionExport' --filter resource-type-prefix="AWS::Lambda::" 
cdk migrate --language typescript --from-stack --stack-name 'CustomResourceCrossRegionExport' --filter resource-type-prefix="AWS::DynamoDB::"

Expected Behavior

The filter should filter the Custom:: resource out and only import the Lambda function.

Current Behavior

Error:

Migrate failed for `CustomResourceCrossRegionExport`: stack generation failed due to error 'TransmuteError: invalid resource type name: Custom::GetExportsValueFromRegion'

Reproduction Steps

Deploy a CFN stack with one CR one Lambda function Function. Use the command described above

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

1.128

Framework Version

No response

Node.js Version

20

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

No response

@jiem-ying jiem-ying added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 25, 2024
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Feb 25, 2024
@tim-finnigan tim-finnigan self-assigned this Feb 26, 2024
@tim-finnigan
Copy link

Hi @jiem-ying thanks for reaching out. Can you share your stack code snippet for reproducing the issue? Also here is the cdk migrate documentation for reference: https://docs.aws.amazon.com/cdk/v2/guide/migrate.html. As noted there, "The CDK Migrate feature is in preview release for AWS CDK and is subject to change."

@tim-finnigan tim-finnigan added toolkit/migrate Related to cdk migrate p1 effort/medium Medium work item – several days of effort response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Feb 26, 2024
@jiem-ying
Copy link
Author

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  ExportsName:
    Description: The exports name from the export CFN stack
    Type: String
    Default: SampleNetworkCrossStack-VPCID
  ExportRegion:
    Description: Target Region
    Type: String
    Default: ap-southeast-2
  LambdaExecutionRole:
    Description: a sufficient role to allow Lambda execute actions on your CloudFormation resource.
    Type: String
    Default: arn:aws:iam::126672810070:role/lambda-ex

Resources:
  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: FunctionToGetOtherRegionCFNExportTest
      Handler: index.lambda_handler
      Role: !Ref LambdaExecutionRole
      Code:
        ZipFile: |
          import boto3
          import cfnresponse
          def lambda_handler(event, context):
              print ('REQUEST BODY:n' + str(event))
              #two properties values passed to variable "parameter_name" and "parameter_region"
              parameter_name = (event['ResourceProperties']['Parameter'])
              parameter_region = (event['ResourceProperties']['Region'])
              physicalResourceId = "hardcodephysicalidforbestpractice"
              print (parameter_name)
              try:
                  if event['RequestType'] == 'Delete':
                      print ("delete")
                      responseData = {"Message": "Delete success"}
                      cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData,physicalResourceId)
                  elif event['RequestType'] == 'Create':
                      print ("create")
                      #Boto3 module create a session based on the "parameter_region" for 'cloudformation' service:
                      client = boto3.Session(region_name= parameter_region).client('cloudformation')

                      #Then the list_exports() function[2] is called and all CFN exports of the target region is returned under the key ['Exports']:
                      exports = client.list_exports()['Exports']
                      
                      #Then a for loop is claimed to find the targeted export name's value
                      for export in exports:
                        if export['Name'] == parameter_name:
                          exportsValue = export['Value']
                      print (exportsValue)
                      #The export value finally put into the responseData and the cfnresponse module[3] 
                      #return the export value to Custom Resource "ExportFetchCustomResource" under attribute 'ExportsValue
                      responseData = {'ExportsValue': exportsValue}    
                      cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, physicalResourceId)
                  elif event['RequestType'] == 'Update':
                      print ("update")
                      client = boto3.Session(region_name=parameter_region).client('cloudformation')
                      exports = client.list_exports()['Exports']
                      for export in exports:
                        if export['Name'] == parameter_name:
                          exportsValue = export['Value']
                      print (exportsValue)
                      responseData = {'ExportsValue': exportsValue}
                      cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData,physicalResourceId)
                  else:
                      responseData = {"Message": "Unexpected event received from CloudFormation"}
                      cfnresponse.send(event, context, cfnresponse.FAILED, responseData,physicalResourceId)

              except Exception as e:
                  print (e)
                  responseData = {'Failure': "operation failed, please check if the region and export name are correct"}
                  cfnresponse.send(event, context, cfnresponse.FAILED, responseData)   
      Runtime: python3.9
      Timeout: 60
      
  ExportFetchCustomResource: 
    Type: Custom::GetExportsValueFromRegion
    Properties:
      #ServiceToken: arn:aws:lambda:us-west-2:126672810070:function:FunctionToGetOtherRegionCFNExportTest
      ServiceToken: !GetAtt LambdaFunction.Arn
      Parameter: !Ref ExportsName
      Region: !Ref ExportRegion
      TriggerUpdate: "L2"

Outputs:
  RetrievedCrossRegionValue:
    Description: Cross Region value from custom resource ExportFetchCustomResource
    Value: !GetAtt ExportFetchCustomResource.ExportsValue

Or can just create a separate random Lambda function with a CR -> service token to an existing lambda function. Same issue

@TheRealAmazonKendra
Copy link
Contributor

Filters only work on resource scans, not on --from-stack. We should improve the documentation on this and add error handling so that if someone provides the wrong set of flags on the wrong source type, we let them know immediately.

Thanks for reporting this!

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 27, 2024
@pahud pahud added the cli Issues related to the CDK CLI label Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. cli Issues related to the CDK CLI effort/medium Medium work item – several days of effort p1 package/tools Related to AWS CDK Tools or CLI toolkit/migrate Related to cdk migrate
Projects
None yet
Development

No branches or pull requests

4 participants