aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Framework: remove property renames for CloudFormation constructs

Open elexisvenator opened this issue 8 years ago • 3 comments

in AssociationResourceProps: documentName is documentName in the CF template, this becomes DocumentName

This fails when deploying the stack. According to the documentation, the parameter should be Name. Manually modifying the CF template generated by CDK to use the correct name works.

My workaround Because AssociationResource is a L1 model, I have built a wrapper around it to do what I need. I have change my wrapper to instead create an instance of OverloadedAssociation - a class I created that extends AssociationResource.

  /**
   * ``AssociationResource`` does not generate the correct CF file, one of the parameters needs to be renamed.
   */
  class OverloadedAssociation extends Ssm.AssociationResource {
    /**
     * Creates a new ``AWS::SSM::Association``.
     *
     * @param parent   the ``cdk.Construct`` this ``AssociationResource`` is a part of
     * @param name     the name of the resource in the ``cdk.Construct`` tree
     * @param properties the properties of this ``AssociationResource``
     */
    constructor(parent: Construct, name: string, properties: Ssm.AssociationResourceProps) {
      super(parent, name, properties);
    }

    protected renderProperties(properties: any): { [key: string]: any } {
      const baseResult = super.renderProperties(properties);

      return {
        // this is the line that changed
        Name: baseResult.DocumentName,
        AssociationName: baseResult.AssociationName,
        DocumentVersion: baseResult.DocumentVersion,
        InstanceId: baseResult.InstanceId,
        OutputLocation: baseResult.OutputLocation,
        Parameters: baseResult.Parameters,
        ScheduleExpression: baseResult.ScheduleExpression,
        Targets: baseResult.Targets,
      };
    }
  }

Is this the right way to workaround this problem? Should I be modifying .ToCloudFormation() instead?

elexisvenator avatar Oct 05 '18 06:10 elexisvenator

You shouldn't have to do anything yourself. This translation happens in the code generator. We have an issue there. Thanks for reporting.

rix0rrr avatar Oct 05 '18 08:10 rix0rrr

here's what I've found as a workaround while this bug gets fixed:

import { cloudformation as ssmCloudFormation } from '@aws-cdk/aws-ssm';
import { App, Stack, StackProps } from '@aws-cdk/cdk';

export class TestStack extends Stack {
    constructor(parent: App, name: string, props: StackProps) {
      super(parent, name, props);

      const nameOfDocument = 'something';
      const associationResource = new ssmCloudFormation.AssociationResource(this, "bob", {
        documentName: nameOfDocument
      })
      associationResource.addOverride('Properties.Name', nameOfDocument);
      associationResource.addOverride('Properties.DocumentName', undefined);
    }
}

and it gives the below output:

Resources:
    bob:
        Type: 'AWS::SSM::Association'
        Properties:
            Name: something
    CDKMetadata:
        Type: 'AWS::CDK::Metadata'
        Properties:
            Modules: '@aws-cdk/aws-directoryservice=0.10.0,@aws-cdk/aws-ec2=0.10.0,@aws-cdk/aws-iam=0.10.0,@aws-cdk/aws-ssm=0.10.0,@aws-cdk/cdk=0.10.0,@aws-cdk/cx-api=0.10.0,esprima=4.0.1,js-base64=2.4.9,js-yaml=3.12.0,monolith-sql-servers=0.0.1'

Found help in release notes here and documentation here.

ChintanRaval avatar Oct 07 '18 22:10 ChintanRaval

@ChintanRaval glad you were able to work around this issue. Stay tuned for #854

eladb avatar Oct 08 '18 06:10 eladb