1. How to set up a load balancer listener for AWS Fargate?

    TypeScript

    Setting up a load balancer listener for AWS Fargate involves creating an AWS Elastic Load Balancer (ELB) and configuring a listener to manage incoming connections.

    Here is a Pulumi program to set this up:

    import * as aws from "@pulumi/aws"; import * as pulumi from "@pulumi/pulumi"; import * as awsx from "@pulumi/awsx"; // Create an ECS cluster. const cluster = new awsx.ecs.Cluster("cluster"); // Create an AWS Fargate service task. const nginx = new awsx.ecs.FargateService("nginx", { cluster: cluster, taskDefinitionArgs: { containers: { nginx: { image: "nginx", memory: 128, portMappings: [{ containerPort: 80 }] } } }, desiredCount: 2, }); // Create a load balancer, a target group and a listener. const loadbalancer = new aws.lb.LoadBalancer("loadbalancer", { subnets: cluster.vpc.publicSubnetIds, internal: false, }); const httpListener = new aws.lb.Listener("http", { loadBalancerArn: loadbalancer.arn, port: 80, defaultActions: [{ type: "forward", targetGroupArn: nginx.targetGroup.arn, }], }); // Export the load balancer's URL since this is what users will use to connect. exports.url = loadbalancer.dnsName;

    In the above code:

    1. We create an ECS cluster using awsx.ecs.Cluster. This cluster will host our services and tasks.
    2. We create a Fargate service task in the ECS cluster. This task is running an nginx web server. The server listens on port 80.
    3. We create a load balancer using aws.lb.LoadBalancer. This load balancer will distribute incoming requests across our Fargate tasks.
    4. We create a listener on port 80 to direct incoming HTTP requests to our Fargate tasks. The forward action in defaultActions forwards traffic to the provided targetGroupArn, which is our Fargate service in this case.

    This program will create a cluster in AWS Fargate, host nginx services in the cluster, and expose these services to the internet via a Load balancer. You can check the Load Balancer's URL by using the url export we've provided at the end of the program.



  2. cluster.vpc does not exist

    TypeScript

    It seems there might be a misunderstanding in accessing the VPC from an ECS cluster in Pulumi. In awsx, the Cluster class does not have a direct vpc property. Instead, you can access the VPC associated with the cluster through the vpc property of the Cluster's ecsCluster property, which is an instance of aws.ecs.Cluster.

    Here's how you can access the VPC ID of an ECS cluster created using awsx:

    import * as awsx from "@pulumi/awsx"; // Create an ECS cluster. const cluster = new awsx.ecs.Cluster("cluster"); // Access the VPC ID associated with the ECS cluster. const vpcId = cluster.ecsCluster.vpcId; // Output the VPC ID. export const clusterVpcId = vpcId;

    In the above program, cluster.ecsCluster.vpcId will give you the ID of the VPC associated with the ECS cluster. If you need to work with the VPC directly, you might need to create it explicitly using awsx.ec2.Vpc or aws.ec2.Vpc and then pass it to the Cluster constructor. Here's an example of how to create a VPC and use it with an ECS cluster:

    import * as awsx from "@pulumi/awsx"; // Create a new VPC. const vpc = new awsx.ec2.Vpc("customVpc", { numberOfAvailabilityZones: 2, }); // Create an ECS cluster with the custom VPC. const cluster = new awsx.ecs.Cluster("cluster", { vpc: vpc, }); // Access the VPC ID. const vpcId = vpc.id; // Output the VPC ID. export const clusterVpcId = vpcId;

    In this program, we create a VPC explicitly and then use it when creating an ECS cluster. The VPC ID is exported so that it can be accessed as needed.