UserData or Bootstrapping in AWS ECS Fargate - amazon-web-services

I have an ECS cluster - EC2-based built using Terraform. The Autoscaling group for this cluster has been configured that is having User-Data. Now, I want to migrate this ECS cluster from EC2 to Fargate. How I can migrate/mention user-data when I'm defining Autoscaling configuration for Fargate?

How I can migrate/mention user-data
You don't. User data is only for EC2 instances, and in Fargate you have no control over them. You have to abandon your user-data, as it has no use in you Fargate.

Related

EKS and ECS Pricing confusion

I have one confusion, did we need to pay for ECS separately, if i had already EKS cluster with t3.medium EC2 instance as worker node. Because in documentation i found that there is no addition charge for ECS if we have EC2 instance.
EKS (Elastic Kubernetes Service) and ECS (Elastic Container Service) are two distinct services on AWS. Both are used for container orchestration. EKS, as the name suggests, is a managed Kubernetes cluster whereas ECS is an AWS-native solution.
Both EKS and ECS can be used on top of either EC2 instances or on Fargate. Fargate is a “serverless” solution in the sense that it manages the underlying servers and the autoscaling for you.
If you run ECS on EC2 instances instead of Fargate, you will only pay for the EC2 instances, just like normal. There is no additional cost for the fact that you run an ECS cluster on top of them. EKS however does charge you for the cluster itself, regardless of the underlying launch type.

How to use AWS Nitro Enclave with ECS?

I am trying to setup AWS Nitro Enclave with ECS using AWS Cloudformation but I am struggling with assigning a launch template to an ECS service.
As far as I know, we can specify a launchType with the value of EC2 while creating the ECS Service but there is no way to manipulate the launch parameters.
Any help with examples will be appreciated.
The Nitro Enclave is part of the EC2 server. An ECS service is just a docker container running on the EC2 server. The EC2 server has to already be up and running (with things like Nitro Enclave already configured) before the ECS service is started on the server. The launchType parameter of the ECS Service just specifies if it should run on Fargate or EC2.
You can't configure your cluster's EC2 servers through an ECS service configuration. You would configure the EC2 servers in the ECS cluster through the cluster's Capacity Provider configuration. Specifically, since you are using EC2 instead of Fargate, you would need to configure the capacity provider with an EC2 auto-scaling group which is configured with an EC2 launch template that handles the Nitro Enclave setup.
To be clear, I've never heard of anyone using Nitro Enclaves with ECS, and I don't think you can actually run ECS services inside the Nitro Enclave. At most you could have ECS services running on the same server that also has a Nitro Enclave running some other process.

Create ECS task from AMI

I have an EC2 instance which is currently stopped and I created an AMI from it. It has the ECS and Docker services installed and enabled to start at boot. How can I create an ECS task to clone this AMI and have the ECS service create more EC2 instances from the AMI as needed?
The ECS tasks appear to only allow me to create EC2 instances from Docker containers. Is there a way to create them from AMI images?

AWS ECS containers on EC2 are taking different region than the region EC2 is launched in

I have an AWS ECS cluster on EC2. The region of ECS cluster and EC2 is us-west-2. But the task container image on running has AWS_REGION environment variable set to a different region in the task container.
Somehow the ecs task role is setting region as us-east-2.
How to have the same region as ECS cluster and EC2 within the ECS task container as well ?
One way I figured out is in AWS ECS Service task definition we can add an environment variable AWS_REGION with required value and it works.
I know EC2 and ECS metadata are also options but don't want anything to include is script to read metadata and set AWS_REGION variable.
Mainly need to check if aws provides anything dynamic to have it set by default based on ecs cluster region and ecs cluster ec2 region.

How to scale ecs cluster with autoscaling?

I think you have 2 scaling
1 for ec2 instance
1 for ecs task
So I think flow goes like
You get more requests
Your tasks need more resources (cpu/memory)
Your ecs task autoscaling kicks in and creates more task
your ec2 autoscaling also kicks in and creates more ec2
Questions
your ecs needs to know newly launched ec2 (from ec2 autoscaling group) is the host that it can run on (how?)
I want the number of ec2 instance starts from one.
I think one instance is already running in the ecs cluster
Do I make ec2 autoscaling group start from 0?
ec2 autoscaling group has pricing option to use spot instance, but I didn't see such option when I created ecs cluster instances. To be clear, can I make ecs cluster with 0 ecs-instance, and let ec2 autoscaling group starts with 1 instance?
Your ecs task autoscaling kicks in and creates more task
your ec2 autoscaling also kicks in and creates more ec2
Your ECS task scale if there is a memory so fine it will scale and launch new container, if not enough memory then it will not launch an instance, you need to configure cloud watch alarm to scale ECS cluster based on the task scaling.
Tutorial: Scaling Container Instances with CloudWatch Alarms
your ecs needs to know newly launched ec2 (from ec2 autoscaling group)
is the host that it can run on (how?)
ECS agent registers the new instance with the same cluster, and the new task can be placed in any instance that have available resources to handle and launch the container.
I want the number of ec2 instance starts from one. I think one
instance is already running in the ecs cluster Do I make ec2
autoscaling group start from 0?
You should start from 1 otherwise no container will launch and will throw error no instance found in the cluster. if you do not want run container for now then you can start from 0.
ec2 autoscaling group has pricing option to use spot instance, but I
didn't see such option when I created ecs cluster instances. To be
clear, can I make ecs cluster with 0 ecs-instance, and let ec2
autoscaling group starts with 1 instance?
You can read about spot ECS cluster instance here.
Amazon ECS is a highly scalable, high performance, container
management service that supports Docker containers and allows you to
run applications on a managed cluster of Amazon EC2 instances easily.
ECS already handles the placement and scheduling of containers on EC2
instances. When combined with Spot fleet, ECS can deliver significant
savings over EC2 on-demand pricing.