Code Monkey home page Code Monkey logo

efimeral-api's Introduction

Hello there! I'm Ariel

Twitter: ariel_17_ Linkedin: arielgerardorios GitHub: ariel17 Gmail: arielgerardorios@gmail.com Upwork: Ariel Gerardo Rios

I'm working as Technical Leader at OnTop.

I love distributed systems, team building and helping others succeed.

Reading a lot, writing some every now and then. See my blog at http://ariel17.com.ar.

Say hi! I enjoy meeting new people, so drop some lines; I would love to hear from you.

efimeral-api's People

Contributors

ariel17 avatar dependabot[bot] avatar

Watchers

 avatar  avatar

efimeral-api's Issues

Custom domains for boxes

I want to register custom domains through lambdas, when I run runTask() method on fargate. How can I do it?

Assuming that you are trying to associate a domain name with a specific task that you're running in AWS Fargate and that you are running this task using a Lambda function, you can make use of the AWS SDK for JavaScript (v3 as of this writing) in your Lambda function to perform these actions programmatically.

Here are the broad steps that you need to follow:

  1. Run your task - When you call the runTask() method to run your Fargate task, make sure you are using a task definition and service which are associated with the AWS Cloud Map service. You will then get the private IP address for the task from the response of the runTask() method.

  2. Register your instance in AWS Cloud Map - After your task is up and running, you can register your instance in AWS Cloud Map using the private IP address from the runTask() response.

  3. Create a Route53 DNS Record - After the instance has been registered, you can use this information to create a Route53 DNS record that points to your service instance.

Here's an example of how you might achieve these steps using AWS SDK for JavaScript:

const AWS = require('@aws-sdk/client-ecs');
const Route53 = require('@aws-sdk/client-route-53');
const CloudMap = require('@aws-sdk/client-servicediscovery');

const ecs = new AWS.ECSClient();
const route53 = new Route53.Route53Client();
const cloudMap = new CloudMap.ServiceDiscoveryClient();

async function registerCustomDomain() {
  const runTaskCommand = new AWS.RunTaskCommand({
    /* your task definition and service params */
  });

  const taskResponse = await ecs.send(runTaskCommand);
  const privateIpAddress = taskResponse.tasks[0].attachments[0].details.find(detail => detail.name === 'networkInterfaceId').value;

  const registerInstanceCommand = new CloudMap.RegisterInstanceCommand({
    /* your AWS Cloud Map params, and privateIpAddress for instance attributes */
  });

  await cloudMap.send(registerInstanceCommand);

  const changeResourceRecordSetsCommand = new Route53.ChangeResourceRecordSetsCommand({
    /* your Route53 params, using privateIpAddress for resource record */
  });

  await route53.send(changeResourceRecordSetsCommand);
}

registerCustomDomain().catch(console.error);

Please note that you will need to replace the placeholders with your actual values. Also, error handling and proper exception management should be considered when you implement this in a production environment. Please note that the exact details might vary depending on your use-case and infrastructure setup.

Also, remember that with this setup, you're directly exposing your tasks which can lead to some potential security risks and also does not provide the elasticity that a load balancer would provide. If your tasks need to scale, this method could quickly become unmanageable.

Support image tag in API

Accept an optional parameter to select the kind of image to instantiate. Default value alpine.

CDK Best practices

Investigate what are the best practices on CDK based projects. The stack is being built currently on a single stack call. Maybe I should split that?

Speed up container creation

Currently, when I call the API to create a new container, the Fargate task takes almost 40 seconds to be up and running. An alternative would be having a couple of instances running (autoscaling) on a service.

From ChatGPT:

Fargate tasks take almost 30 seconds to be up and running. How can I make it faster? Show me the example with cdk

To improve the startup time of your Fargate tasks, you can use the AWS Cloud Development Kit (CDK) to configure your Fargate service with a warm start strategy. This strategy keeps a certain number of tasks running at all times to reduce the delay when new tasks are needed.

Here's an example using the CDK in TypeScript:

  1. Install the required dependencies:
npm install aws-cdk-lib @aws-cdk/aws-ecs @aws-cdk/aws-ec2
  1. Create a new CDK stack (e.g., FargateStack.ts):
import * as cdk from 'aws-cdk-lib';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';

export class FargateStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const vpc = new ec2.Vpc(this, 'MyVpc'); // Replace with your VPC configuration

    // Create a Fargate service
    const fargateService = new ecs.FargateService(this, 'MyFargateService', {
      cluster: new ecs.Cluster(this, 'MyCluster', { vpc }),
      taskDefinition: new ecs.TaskDefinition(this, 'MyTaskDefinition', {
        memoryMiB: '512', // Adjust as per your requirements
        cpu: '256', // Adjust as per your requirements
      }),
      desiredCount: 1, // Number of warm tasks to keep running
    });

    // Enable service autoscaling to handle increased load
    const scaling = fargateService.autoScaleTaskCount({ maxCapacity: 10 }); // Adjust max capacity as needed

    scaling.scaleOnCpuUtilization('CpuScaling', {
      targetUtilizationPercent: 50, // Adjust the target utilization as per your needs
    });
  }
}

Make sure to replace the VPC configuration with your own.

  1. In your project's main file (e.g., index.ts), create an instance of cdk.App and add the stack:
import * as cdk from 'aws-cdk-lib';
import { FargateStack } from './FargateStack';

const app = new cdk.App();
new FargateStack(app, 'MyFargateStack');
app.synth();
  1. Build and deploy the CDK stack:
cdk synth
cdk deploy

By configuring the desiredCount parameter in the FargateService constructor, you can control the number of warm tasks that should be running at all times. Adjusting the memory and CPU resources in the TaskDefinition is also important to ensure optimal performance.

Additionally, the autoscaling configuration in the example enables the service to handle increased load by scaling the number of tasks based on CPU utilization. You can customize the scaling policies according to your application's requirements.

Remember to replace the placeholders and adjust the configurations to match your specific use case.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.