Avoid task failures in ECS/Fixing “Essential container in task exited” error in ECS

In Amazon ECS (Elastic Container Service), the concept of “essential” and “non-essential” containers refers to the importance of specific containers within a task definition. This distinction is used to determine how the task behaves when individual containers exit or fail.

Essential Containers

  • Definition: An essential container is a critical part of the task. If this container stops or exits with an error, the entire task will stop.
  • Behavior: If an essential container fails, ECS will consider the task as failed and will potentially stop all other containers in the task. The task might be restarted or terminated based on the task’s restart policy.
  • Use Case: Essential containers are typically the main components of an application, like a web server, primary service, or a critical process that the task cannot function without.

Non-Essential Containers

  • Definition: A non-essential container is considered supplementary. If this container stops or exits, the task will continue to run as long as all essential containers are running.
  • Behavior: The failure of a non-essential container will not cause the entire task to stop. However, the status of the non-essential container will be reported as stopped.
  • Use Case: Non-essential containers are often used for auxiliary tasks like logging, monitoring, or sidecar containers that provide additional but non-critical functionality.

Example Scenario

Imagine a task with two containers:

  • Container A (Essential): A web server that serves application traffic.
  • Container B (Non-Essential): A logging agent that forwards logs to a remote server.

If Container A fails, the entire task will stop. If Container B fails, the task will continue running, but logs might not be forwarded until the logging container is restarted or replaced.

Configuration

In the ECS task definition JSON, you can specify whether a container is essential or non-essential by setting the essential parameter:

jsonCopy code{
  "containerDefinitions": [
    {
      "name": "web-server",
      "image": "nginx",
      "essential": true
    },
    {
      "name": "log-agent",
      "image": "log-agent-image",
      "essential": false
    }
  ]
}

In this example, “web-server” is essential, while “log-agent” is non-essential.

Power BI Desktop: Convert Rows to Columns with JSON Data

Step-by-Step Guide to Converting JSON Data into Columns in Power BI Desktop

Install Power BI Desktop if you don’t have it.

Follow below steps to convert rows into columns with json data

Source data –

Expected data –


  1. Open Power BI Desktop and click on Blank report

2. Click on Get data from another source

3. Select JSON and click on Connect

4. Select your JSON file and click Open

5. JSON data will be opened like below

6. Select the columns you want to convert

7. Select Transform tab and click on Pivot Column

8. Select the Values Column and from Advanced options, select Don’t Aggregate

9. Click OK

You will see the output

How to connect to SQL database from Lambda using Nodejs

To connect to SQL database from lambda, I am using Nuget package ‘mssql’.

For this, I have created a serverless application using Nodejs 16.x.

To connect to SQL database from lambda, I am using Nuget package ‘mssql’.

In your serverless application, install mssql like below –

npm install mssql

Usage

I am using Typescript and DBConfig used above is just an interface.
Also replace all the config with your database credentials.

import sql from "mssql";

const config : DBConfig = {
 user: ${process.env.DB_USER},
 password: ${process.env.DB_PASSWORD},
 server: ${process.env.DB_SERVER},
 database: ${process.env.DATABASE},
 options: {
  trustServerCertificate: true,
 },
};

export const run = async () => {
 try {
  await sql.connect(config);
  const result = await sql.query`Select * from [TableName]`;
  var result = result.recordset;
}
catch (err) {
 console.error("Error:", err);
 return {
  statusCode: 500,
  body: JSON.stringify({
  message: "Error accessing the database.",
  error: err,
  }),
 };
}
}

Please note that the RDS and lambda should be in same VPC settings. If while running lambda, you get a timeout error, do verify the VPC settings for both RDS and lambda. If they are in different VPC, then you have to do further settings. Please refer to AWS documentation for that.

Also make sure the IAM role in lambda should have permission to access RDS

Refer to this if you are looking to send an email with attachment from lambda.

Convert JSON to csv in C#

Convert JSON to csv in C#

  • Given users.json as the json file
using (var csv = new ChoCSVWriter("users.csv").WithFirstLineHeader())
{
   using (var json = new ChoJSONReader("users.json") 
      .WithField("Username")
      .WithField("Sub", jsonPath: "$.Attributes[?(@.Name == 'sub')].Value", isArray: true)
      .WithField("EmailVerified", jsonPath: "$.Attributes[?(@.Name == 'email_verified')].Value", isArray: true)
      .WithField("GivenName", jsonPath: "$.Attributes[?(@.Name == 'given_name')].Value", isArray: true)
      .WithField("FamilyName", jsonPath: "$.Attributes[?(@.Name == 'family_name')].Value", isArray: true)
      .WithField("Email", jsonPath: "$.Attributes[?(@.Name == 'email')].Value", isArray: true)
      .WithField("UserCreateDate")
      .WithField("UserLastModifiedDate")
      .WithField("Enabled")
      .WithField("UserStatus")
   )
{
   csv.Write(json);
}
}

Convert PHP script to JavaScript

Convert PHP script to JavaScript

  • Add the “base-reality/php-to-javascript”: “>=0.0.3” to your project’s composer.json file:

    “require”:{ “base-reality/php-to-javascript”: “0.1.16” }

    Or the latest tagged version. The dev master should only be used for development, not production.\
  • Include the Composer SPL autoload file in your project: require_once(‘../vendor/autoload.php’);

    Call the converter:
 $phpToJavascript = new PHPToJavascript\PHPToJavascript();
   $phpToJavascript->addFromFile($inputFilename); $jsOutput =
   $phpToJavascript->toJavascript();

$jsOutput will now contain an auto-generated Javascript version of the PHP source file.

Interested in Cryptocurrency. Register and start investing here

Earn a side income by affiliate marketing. Learn here how to do it.

Join now – Affiliate Program