skip to Main Content

I’m trying to move over from Cloudformation to CDK and have been struggling here.
I’m trying to create a ELB listner and add a default action to it. Port 80 Listner with default action to redirect to 443. It seems like this should be easy but I can’t see any way to hook in to the default actions for a listner. Some sources say default_action is a prop of the Listner class but I can’t seem to find how to use it on instantiation. Others say to use .add_action as in this example:

    listener80.add_action('DefaultAction',
        elbv2.ListenerAction.fixed_response(200,
            content_type=elbv2.ContentType.TEXT_PLAIN,
            message_body="OK"
            ))

However i keep getting this error:

AttributeError: module 'aws_cdk.aws_elasticloadbalancingv2' has no attribute 'ContentType'

Even though that example is straight out of AWS docs…

2

Answers


  1. Chosen as BEST ANSWER

    For anyone else struggling like i was here is the way to declare it on instantiation:

     listener80 = lb.add_listener(
                "listener80",
                port=80,
                default_action=elbv2.ListenerAction.redirect(host="#{host}", path="/#{path}", permanent=True, port="443", protocol="HTTPS", query="#{query}")
            )
    

    or to add it with add action:

    listener80.add_action('DefaultAction', action=elbv2.ListenerAction.redirect(host="#{host}", path="/#{path}", permanent=True, port="443", protocol="HTTPS", query="#{query}"))
    

  2. I had the same problem, and until now I didn’t find any information about the new position of the enum ContentType in cdk v2, alternatively, according to the definition of the interface FixedResponseOptions :

    export interface FixedResponseOptions {
    /**
     * Content Type of the response
     *
     * Valid Values: text/plain | text/css | text/html | application/javascript | application/json
     *
     * @default - Automatically determined
     */
    readonly contentType?: string;
    /**
     * The response body
     *
     * @default - No body
     */
    readonly messageBody?: string;}
    

    I just put a string value instead, for example ‘text/plain’

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search