Hello, is there a working example (preferably in G...
# aws
l
Hello, is there a working example (preferably in Go) of defining an EC2 backed ECS service? I have had pretty good luck using Pulumi AI for other things, but I can't seem to figure out all the pieces I need to get an EC2 cluster up and running. I'm starting with nothing, so I need to define "all the things". My goal is to create everything via Pulumi, without ever using the aws console. I can make simple things (S3), and slightly more complicated things (RDS instances), but I'm completely stuck on the details needed for ECS backed by EC2. It looks like Fargate is way easier, but I'm trying to do this as inexpensively as possible.
b
It's fargate instead of EC2, and typescript instead of go, but maybe my example repo might be a starting point? https://github.com/darkmagedtm/pulumi_aws_examples/blob/main/ecs_full_stack.ts
q
You can create an EC2 backed capacity provider via auto scaling groups like this:
Copy code
// Define an Auto Scaling Group (ASG) that will provide the capacity for our ECS tasks
const asg = new aws.autoscaling.Group("asg", {
    // ... other configuration like VPC subnets, security group, launch configuration/template
    maxSize: 5,
    minSize: 1,
});

// Create an ECS capacity provider that is tied to the ASG defined above
const capacityProvider = new aws.ecs.CapacityProvider("my-capacity-provider", {
    autoScalingGroupProvider: {
        autoScalingGroupArn: asg.arn,
        managedScaling: {
            status: "ENABLED",
            targetCapacity: 75, // The target utilization for the capacity provider
        },
    },
});

// Associate the capacity provider with your ECS cluster
const clusterCapacityProviders = new aws.ecs.ClusterCapacityProviders("my-cluster-cap-providers", {
    clusterName: cluster.name,
    capacityProviders: [capacityProvider.name],
    defaultCapacityProviderStrategies: [
        {
            capacityProvider: capacityProvider.name,
            weight: 1,
            base: 1,
        },
    ],
});
In the service resource you also need to set
launchType: "EC2"
and reference the capacity provider like so:
Copy code
capacityProviderStrategies: [
        {
            capacityProvider: capacityProvider.name,
            weight: 1,
            base: 1,
        },
    ]
l
@quick-house-41860 Thanks for the info! I was missing the
ClusterCapacityProviders
piece. This is a side project, so I can't put the time I would like into it (thus the delay in testing out your suggestion). My cluster now finds the ec2 backed asg and gets stuck in pending (before it said it couldn't find anywhere to deploy, so progress 🙂). It seems that the "stuck on pending" issue is related to networking, so I just need to do some more research/digging there. Interestingly, if I ssh to the ec2 instance that is in the asg, I can execute
docker run -p 80:80 httpd
and then hit the public IP from my browser. So, some of the networking pieces are there, but there is probably a link in the chain I have overlooked. I was completely out of ideas, and the
ClusterCapacityProviders
piece has gotten me un-stuck. I really appreciate your help!