AWS CDK: SNS HTTPS Subscription "Must provide protocol if url is unresolved"

Question:

I’m having trouble finding any message on this is the documentation. I know that this line is the problem line, because when I comment it out, "cdk deploy" works just fine. Basically, I am getting a url as a parameter from the user (I am aware parameters are not recommended by AWS, but this is necessary for my use case). I then use that parameter to subscribe to an SNS topic in the following line:

my_sns_topic.add_subscription(subscriptions.UrlSubscription(pagerduty_url.value_as_string))

When this line isn’t commented out, I get the following error:

jsii.errors.JSIIError: Must provide protocol if url is unresolved

I can subscribe something using an email protocol to this SNS topic just fine, so I don’t think it’s the SNS topic itself. It works when I just directly pass the URL as a string into the function as well, so it seems to be an issue with the parameter. So, how do I fix this? I understand it wants to have an alternative in case the url is not valid, but there’s no information in the CDK documentation on how to actually do this, either that or I’m just not finding it.

Asked By: John

||

Answers:

Because you are using a CfnParameter and the value can’t be read at synth time then the UrlSubscription class has no way of inferring the protocol used, so you must provide it yourself. Take a look here for reference:

https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_sns_subscriptions/UrlSubscription.html

Try:

my_sns_topic.add_subscription(subscriptions.UrlSubscription(pagerduty_url.value_as_string, protocol=sns.SubscriptionProtocol.HTTPS))

If it isn’t always going to be an HTTPS protocol then that’ll likely have to be another Parameter you request from the user.

Answered By: Matthew Bonig