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.FnJoin synth bug #512

Closed
xdodocodex opened this issue Aug 6, 2018 · 5 comments
Closed

cdk.FnJoin synth bug #512

xdodocodex opened this issue Aug 6, 2018 · 5 comments
Labels
guidance Question that needs advice or information.

Comments

@xdodocodex
Copy link

xdodocodex commented Aug 6, 2018

I'm introducing an output for CloudFormation stack as follow:

new cdk.Output(this, "ApiEndpoint", {
                value: new cdk.FnJoin("", ["https://","dummy.ref",".execute-api.",this.env.region,".amazonaws.com/","stage_name"]),
                description: "Endpoint for this stage of the api"
            })

What I expect from the command

cdk synth

is that a code like the following is created inside the CloudFormation template:

Outputs:
    ApiEndpoint:
        Description: 'Endpoint for this stage of the api'
        Value:
            'Fn::Join':
                - ""
                -
                    - 'https://'
                    - dummy.ref
                    - .execute-api.
                    - eu-central-1
                    - .amazonaws.com/
                    - stage_name
        Export:
            Name: 'PvideoSmartCF:ApiEndpoint'

Instead the generated code is like:

Outputs:
    ApiEndpoint:
        Description: 'Endpoint for this stage of the api'
        Value:
            'Fn::Join':
                - ""
                -
                    -
                        - 'https://'
                        - dummy.ref
                        - .execute-api.
                        - eu-central-1
                        - .amazonaws.com/
                        - stage_name
        Export:
            Name: 'PvideoSmartCF:ApiEndpoint'

Which result in an error during the deployment:

failed: ValidationError: Template error: every Fn::Join object requires two parameters, (1) a string delimiter and (2) a list of strings to be joined or a function that returns a list of strings (such as Fn::GetAZs) to be joined.
Template error: every Fn::Join object requires two parameters, (1) a string delimiter and (2) a list of strings to be joined or a function that returns a list of strings (such as Fn::GetAZs) to be joined.

Do anyone know how to solve this problem, or if it is a a bug or I am doing something wrong?

Thank you fro the support.

@eladb
Copy link
Contributor

eladb commented Aug 6, 2018

FnJoin accepts a variadic argument, so you would want to use it like so:

new cdk.FnJoin("", "https://","dummy.ref",".execute-api.",this.env.region,".amazonaws.com/","stage_name")

Alternatively, you can use the sugar FnConcat:

new cdk.FnConcat("https://","dummy.ref",".execute-api.",this.env.region,".amazonaws.com/","stage_name")

@xdodocodex
Copy link
Author

Thank you this solved the problem

@eladb
Copy link
Contributor

eladb commented Aug 6, 2018

I wonder if we should change FnJoin to correspond 1:1 with CloudFormation. I am assuming you will not be the only person making that assumption...

@RomainMuller
Copy link
Contributor

I think the FnJoin function in CloudFormation isn't ideal in most programming languages. The fact its variadic certainly makes it worse here (probably we should just get an array and call it quits).

But I'm thinking there may be value in providing a more fluent API instead... Something similar to what Google Guava does --- Joiner.on('; ').join(value, value, value);.

@RomainMuller
Copy link
Contributor

Additionally, I guess the current typing of the arguments of FnJoin has listOfValues as any. This makes it extremely easy for people to pass incorrect things in (aka: an array). An option would be to flatten the argument (if the varargs contains a single value, and that is an array, just assume the user incorrectly passed in an array literal).

A type union would make things nicer here, but JSII won't support that (not transposable to most statically typed languages, such as Java, C#, ...).

RomainMuller added a commit that referenced this issue Aug 7, 2018
A common developer error is to pass an array as the second argument of
`new FnJoin`, but that argument is variadic. This causes invalid
CloudFormation templates to be generated. This change detects this usage
pattern (vararg contains a single element that is an array) and fixes
it up transparently.

Related to #512
@srchase srchase added guidance Question that needs advice or information. and removed question labels Jan 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

5 participants