question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Core] support complex types in Fn.conditionIf

See original GitHub issue

I am using the CDK to output a raw CloudFormation template as a final deliverable and am using primarily Cfn* resource constructs. We have a CfnPipeline object where I want to conditionally add a stage depending on a CfnParameter value. I have the following code:

codepipeline = aws_codepipeline.CfnPipeline(
            self,
            "Pipeline",
            artifact_store=aws_codepipeline.CfnPipeline.ArtifactStoreProperty(...),
                type="S3"
            ),
            role_arn=codepipeline_role.role_arn,
            stages=[
                aws_codepipeline.CfnPipeline.StageDeclarationProperty(...),
                aws_codepipeline.CfnPipeline.StageDeclarationProperty(...),
                aws_codepipeline.CfnPipeline.StageDeclarationProperty(...),
                core.Fn.condition_if(
                    cloudfront_enable_condition.logical_id,
                    aws_codepipeline.CfnPipeline.StageDeclarationProperty(
                        name="Finalize",
                        actions=[
                            aws_codepipeline.CfnPipeline.ActionDeclarationProperty(
                                action_type_id=aws_codepipeline.CfnPipeline.ActionTypeIdProperty(
			            category="Invoke",
                                    owner="AWS",
                                    provider="Lambda",
                                    version="1"
                                ),
                                configuration={
                                    "FunctionName": cloudfront_invalidation_lambda_function.ref
                                },
                                name="CloudFrontInvalidationAction",
                                role_arn=codepipeline_finalize_stage_role.attr_arn
                            )
                        ]
                    ),
                    core.Aws.NO_VALUE
                )
            ]
)

Which produces the following output in my CloudFormation template:

          {
            "Fn::If": [
              "CloudFrontEnableCondition",
              {
                "actions": [
                  {
                    "actionTypeId": {
                      "category": "Invoke",
                      "owner": "AWS",
                      "provider": "Lambda",
                      "version": "1"
                    },
                    "name": "CloudFrontInvalidationAction",
                    "configuration": {
                      "FunctionName": {
                        "Ref": "CloudFrontInvalidationLambdaFunction"
                      }
                    },
                    "roleArn": {
                      "Fn::GetAtt": [
                        "CodePipelineFinalizeStageRole",
                        "Arn"
                      ]
                    }
                  }
                ],
                "name": "Finalize"
              },
              {
                "Ref": "AWS::NoValue"
              }
            ]
          }
        ]

The keys of the CfnPipeline.StageDeclarationProperty and its children (CfnPipeline.ActionDeclarationProperty and CfnPipeline.ActionTypeIdProperty) are printed in camelCase, making for invalid CloudFormation objects on submission. I’ve also tried by wrapping the conditional statement in core.Token.as_any() with the same result.

I have resorted to using the following code:

codepipeline.add_override(
            "Properties.Stages.3",
            {
                "Fn::If": [
                    cloudfront_enable_condition.logical_id,
                    {
                        "Actions": [
                            {
                                "ActionTypeId": {
                                    "Category": "Invoke",
                                    "Owner": "AWS",
                                    "Provider": "Lambda",
                                    "Version": 1
                                },
                                "Configuration": {
                                    "FunctionName": cloudfront_invalidation_lambda_function.ref
                                },
                                "Name": "CloudFrontInvalidationAction",
                                "RoleArn": codepipeline_finalize_stage_role.attr_arn
                            }
                        ],
                        "Name": "Finalize"
                    },
                    core.Aws.NO_VALUE
                ]
            }
        )

which fixes my issue and proves that my conditional is valid in CloudFormation, but it feels messy and like the Cfn* properties should be supported in conditionals (IResolvable?).

Feels like this is a bug, but might be a feature request – perhaps something is missing in the translation layer between nodejs and the Python API? If I’m supposed to be using the API differently, please advise.

Reproduction Steps

Run the above code patterns and inspect the cloudformation template in cdk.out

Error Log

CloudFormation complains of invalid object syntax.

Environment

  • CLI Version : 1.42.1
  • Framework Version:
  • Node.js Version: 12.16.2
  • OS : Ubuntu
  • Language (Version): Python3

Other

https://github.com/ordinaryexperts/aws-marketplace-oe-patterns-drupal/blob/f0201c49583a553832d72267ba07850b1bbe4aca/cdk/drupal/drupal_stack.py#L1870


This is 🐛 Bug Report

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
acarltoncommented, Oct 5, 2020

I will reopen this issue for reference but I will reiterate that the happy path for CDK is writing ifs in TypeScript, not in CloudFormation.

@rix0rrr Thanks for reconsidering. A reminder that the use case presented in the ticket description is using Cfn* resources to generate a CloudFormation template final deliverable and the suggested happy path of doing if statements in TypeScript or Python isn’t an option for us.

1reaction
rix0rrrcommented, Oct 5, 2020

I will reopen this issue for reference but I will reiterate that the happy path for CDK is writing ifs in TypeScript, not in CloudFormation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

interface ICfnConditionExpression · AWS CDK
Represents a CloudFormation element that can be used within a Condition. You can use intrinsic functions, such as Fn.conditionIf , Fn.conditionEquals , and...
Read more >
Using [ComplexType] in Entity Framework Core - Stack Overflow
My problem is that when I try to create a migration, EF Core reports a error. Microsoft.Data.Entity.Metadata.ModelItemNotFoundException: The entity type ' ...
Read more >
Cisco Catalyst 6500 Series Mixed Media Gigabit Ethernet ...
Choice of media and connector type: Support multi-mode fiber or single-mode fiber using GBIC ... High Performance Distribution, Core Layer and Data Center....
Read more >
Single Mode vs Multimode Fiber Cable Guide - FS Community
Single mode fiber core diameter is much smaller than multimode fiber. ... but OM3/OM4/OM5 multimode fiber supports a higher data rate.
Read more >
An Introduction to R - The Comprehensive R Archive Network
This manual provides information on data types, programming elements, ... this permission notice may be stated in a translation approved by the R...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found