https://pulumi.com logo
Title
r

rhythmic-tailor-1242

01/16/2023, 6:30 PM
Hi all, I have a domain let's say
<http://example.com|example.com>
and I'm able to load
<https://example.com>
I now want to make sure
<https://www.example.com>
gets redirected to
<https://example.com>
Any idea how to do it? This is my code right now:
// Step 2: Define the Networking for our service.
const alb = new awsx.elasticloadbalancingv2.ApplicationLoadBalancer(
    "net-lb",{
        external: true,
        vpc,
        subnets: vpcData.subnets.public,
        securityGroups: cluster.securityGroups,
    });

// Target group with the port of the Docker image
const target = alb.createTargetGroup(
    "web-target", {vpc, port: PORT}
);
// Listen to traffic on port 443 & route it through the target group
const httpsListener = target.createListener("web-listener", {
    port: 443,
    certificateArn: certificateValidation.certificateArn
});

const web = alb.createListener("nginx", {
    // const web = new awsx.elasticloadbalancingv2.ApplicationListener("nginx", {
    port: PORT,
    protocol: "HTTP",
    defaultAction: {
        type: "redirect",
        redirect: {
            protocol: "HTTPS",
            host: `${DOMAIN}`,
            port: "443",
            statusCode: "HTTP_301"
        },
    },
})

new aws.route53.Record(`<http://example.com|example.com>`, {
    name: `<http://example.com|example.com>`,
    zoneId: hostedZoneId,
    type: "A",
    aliases: [{
        name: httpsListener.endpoint.hostname,
        zoneId: alb.loadBalancer.zoneId,
        evaluateTargetHealth: true
    }],
});
b

billowy-army-68599

01/16/2023, 6:47 PM
@rhythmic-tailor-1242 you need to add a listener rule: https://www.pulumi.com/registry/packages/aws/api-docs/alb/listenerrule/
r

rhythmic-tailor-1242

01/16/2023, 8:47 PM
Thanks! This is what I have but
www
still doesn't work:
const target = alb.createTargetGroup(
    "web-target", {vpc: unikVpc, port: PORT}
);

const httpsListener = target.createListener("web-listener", {
    port: 443,
    certificateArn: certificateValidation.certificateArn
});

httpsListener.addListenerRule("nginx", {
    actions: [{
        type: "redirect",
        redirect: {
            host: "unik.style",
            protocol: "HTTPS",
            port: "443",
            statusCode: "HTTP_301",
        },
    }],
    conditions: [{
        hostHeader: {
            values: ["unik.style", "www.unik.style"],
        },
    }],
})