ECS task does not inherit region from hosting EC2.

0

I'm using EC2 backed ECS tasks. When I'm trying to access an AWS service I receive an error "Missing Region".

On the Fargate exactly the same docker image works well. I'm using the latest official AWS Rust create which supposed to automatically detect region in normal conditions. I would like to avoid to hardcode specific region.

Here is how my service is created.

  makeFleet(): ecs.AsgCapacityProvider {
    const ecsInstanceRole = new iam.Role(this, `${this.appPrefix}-ec2-fleet-role`, {
      assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
      managedPolicies: [
        iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AmazonEC2ContainerServiceforEC2Role'),
      ],
    });

    const launchTemplate = new ec2.LaunchTemplate(this, `${this.appPrefix}-LaunchTemplate`, {
      launchTemplateName: `${this.appPrefix}-ecs-LaunchTemplate`,
      instanceType: ec2.InstanceType.of(ec2.InstanceClass.C7G, ec2.InstanceSize.LARGE),
      machineImage: ecs.EcsOptimizedImage.amazonLinux2(
        ecs.AmiHardwareType.ARM,
      ),
      userData: ec2.UserData.forLinux(),
      role: ecsInstanceRole,
    });

    const spotFleet = new autoscaling.AutoScalingGroup(this, `${this.appPrefix}-SpotFleet`, {
      vpc: this.cluster.vpc,
      minCapacity: 1,
      vpcSubnets: this.taskSubnets,
      maxCapacity: MAX_SCALING_CAPACITY,
      // associatePublicIpAddress: false,
      launchTemplate,
    });

    // Add the Auto Scaling group as a Capacity Provider
    const capacityProvider = new ecs.AsgCapacityProvider(this, `${this.appPrefix}-EC2-provider`, {
      capacityProviderName: `${this.appPrefix}-EC2-provider`,
      autoScalingGroup: spotFleet,
    });

    this.cluster.addAsgCapacityProvider(capacityProvider);
    return capacityProvider;
  }


  makeTransformationService(
    capacityProvider: ecs.AsgCapacityProvider,
  ): ecsPatterns.ApplicationLoadBalancedEc2Service {
    const taskImageOptions: ecsPatterns.ApplicationLoadBalancedTaskImageOptions = {
      family: `${this.appPrefix}-transform-task-ec2`,
      // image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
      image: this.rustImageArm,
      containerPort: CONTAINER_PORT,
      environment: {
      },
      logDriver: this.logDriver,
      command: ['./transform'],
    };

    const transformService = new ecsPatterns.ApplicationLoadBalancedEc2Service(this, `${this.appPrefix}-ec2-ingest`, {
      cluster: this.cluster,
      serviceName: `${this.appPrefix}-transform-ec2`,
      cpu: 1024 * 2,
      memoryLimitMiB: 1024 * 3,
      taskImageOptions,
      loadBalancerName: `${this.appPrefix}-transform-lb-ec2`,
      propagateTags: ecs.PropagatedTagSource.SERVICE,
      publicLoadBalancer: false,
      minHealthyPercent: 100,
      capacityProviderStrategies: [{
        capacityProvider: capacityProvider.capacityProviderName,
        weight: 1,
      }],
    });

    
    return transformService;
  }

Should I do any additional config on EC2 backed variant to make it work?

profile picture
Smotrov
asked 3 months ago106 views
1 Answer
0

the 'missing region' error typically means that the AWS SDK can't discover a region to use when sending requests to AWS.

For Fargate tasks, the AWS SDK can do this automatically because Fargate injects the region into the environment variables of your task.

For EC2 tasks, you will need to instruct your tasks with AWS_REGION so that the AWS SDK knows where your tasks are running.

Just add AWS_REGION environment variable as per your location in the environment section of your container definition (in taskImageOptions):

  const taskImageOptions: ecsPatterns.ApplicationLoadBalancedTaskImageOptions = {
    family: `${this.appPrefix}-transform-task-ec2`,
    image: this.rustImageArm,
    containerPort: CONTAINER_PORT,
    environment: {
      AWS_REGION: '<your-region>', // e.g., us-west-2
    },
    logDriver: this.logDriver,
    command: ['./transform'],
  };

Remember to replace '<your-region>' with the AWS region where your ECS service is running.

If you don't want to hardcode the region, you can also use CDK's Stack.region attribute:

  environment: {
    AWS_REGION: Stack.of(this).region,
  }
AWS
dov
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions