Skip to content

implement ref and getAtt for related resources#484

Open
gemammercado wants to merge 7 commits intomainfrom
relatedResources
Open

implement ref and getAtt for related resources#484
gemammercado wants to merge 7 commits intomainfrom
relatedResources

Conversation

@gemammercado
Copy link
Copy Markdown
Contributor

@gemammercado gemammercado commented Mar 18, 2026

  • related resources
  • implement getAtt for related resources

Issue #, if available:

Description of changes:
Auto-populate !Ref and !GetAtt in related resource snippets

When inserting related resources, properties that reference the parent resource type are now pre-populated with !Ref or !GetAtt intrinsic functions instead of empty strings.

Changes

src/relatedResources/RelatedResourcesSnippetProvider.ts

  • Added RelationshipSchemaService as a constructor dependency
  • Added findParentLogicalId() to resolve the parent resource's logical ID from the template syntax tree
  • Added getPropertyValueForRelatedResource() to check if a required property references the parent type and generate the appropriate intrinsic function
  • Added buildIntrinsicReference() to decide between !Ref (primary identifier match) and !GetAtt (non-primary attribute like Arn)
  • Added replaceIntrinsicPlaceholders() to handle YAML serialization (the YAML library quotes ! prefixed values, so placeholders are used during serialization and replaced afterward)
  • Added addParentReferencingProperties() to also populate non-required properties that reference the parent
  • Added countTopLevelParentReferences() guard: when multiple top-level properties reference the same parent (ambiguous), none are auto-populated
  • Array properties are skipped

src/handlers/RelatedResourcesHandler.ts

  • Added hasExactlyOnePopulatableRelationship() filter to the getRelatedResourceTypes handler
  • The dropdown now only includes resource types that have exactly one non-array, top-level property referencing the parent, matching the snippet provider's population logic. Only one to avoid a scenario where we arbitrarily choose which property will reference the parent resource:
ServiceCatalogStackSetConstraintRelatedToIAMRole:
    Type: AWS::ServiceCatalog::StackSetConstraint
    Properties:
      Description: ""
      StackInstanceControl: ""
      PortfolioId: ""
      ProductId: ""
      RegionList: ""
      AdminRole: !GetAtt MyRole.Arn
      AccountList: ""
      ExecutionRole: ""

In this example, the reference can go in AdminRole or ExecutionRole, but the code populates AdminRole because it is a top-level property.

  • Excludes resources with: no top-level refs, all-array refs, or multiple top-level refs to the parent

src/server/CfnLspProviders.ts

  • Passes relationshipSchemaService to the RelatedResourcesSnippetProvider constructor

src/services/RelationshipSchemaService.ts

  • Added reverse relationship cache for incoming lookups. This is the case where the child resource schema contains the reference to the parent. Right now the code only considers the resources that the parent resource schema has references to. For instance, the relationship schema for IAM::Role itself only has outgoing references to AWS::IAM::ManagedPolicy. But AWS::Lambda::Function has a Role property that points back to IAM::Role. The reverse cache captures this incoming direction

Example

Before:

LambdaFunctionRelatedToIAMRole:
  Type: AWS::Lambda::Function
  Properties:
    Code: ""
    Role: ""
    Runtime: ""

After:

LambdaFunctionRelatedToIAMRole:
  Type: AWS::Lambda::Function
  Properties:
    Code: ""
    Role: !GetAtt MyRole.Arn
    Runtime: ""

For JSON templates, produces {"Ref": "LogicalId"} or {"Fn::GetAtt": ["LogicalId", "Attribute"]}.

Testing

  • 36 unit tests passing (22 snippet provider + 14 handler)
  • Snippet provider coverage: 96.37% statements, 89.7% branches
  • Handler coverage: 97.72% statements, 93.75% branches
  • Manual testing performed against AWS::IAM::Role, AWS::EC2::VPC, and AWS::S3::Bucket parent types with full related resource insertion

Known limitations

These are pre-existing data quality issues in relationship_schemas.json, not introduced by this PR:

  • ServiceCatalog::StackSetConstraint — Missing ExecutionRole → IAM::Role relationship; only AdminRole is declared
  • SSM::Parameter Value — Maps to 100+ resource types; overly broad relationship
  • EC2::EIP Domain — Incorrectly maps to AWS::EC2::VPC; Domain is an enum, not a VPC ID
  • Glue::Crawler DatabaseName — Incorrectly maps to AWS::S3::Bucket; expects a Glue Database name
  • When multiple resources of the same parent type exist in the template, the first one found is used for the !Ref/!GetAtt (future enhancement: let user choose which)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@gemammercado gemammercado requested a review from a team as a code owner March 18, 2026 18:31
@satyakigh satyakigh force-pushed the relatedResources branch 2 times, most recently from 8780f05 to 0505e8e Compare April 1, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants