Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

AWS Heroes profile imageDanielle Heberling
Danielle Heberling forAWS Heroes

Posted on • Originally published atdanielleheberling.xyz on

     

Ephemeral Jobs Longer than the Lambda Timeout

horse race

Photo byMathew Schwartz onUnsplash

The Problem

There's an ad-hoc job that runs in the background that doesn't have a client waiting for a synchronous response.

You could run this in AWS Lambda to save money. A benefit with an ephemeral job run is that AWS only charges for when the Lambda is running.

Problem is that this specific job runs longer than the Lambda timeout (at the time of writing this it is 15 minutes). We could write extra logic to make it work, but that adds unnecessary complexity.

You still want to run this ephemerally to save money and resources...so what can you do?

A Solution

One option is to useECS run-task with aFargate launch type.

AWS Resources Needed:

  • An ECS Cluster - a logical grouping of tasks or services (in my example we're using thedefault cluster that comes with new AWS accounts)
  • A Task Definition - a blueprint for your application which contains one or more containers and parameters to run
  • A Container - a lightweight, standalone, executable package of software that includes everything needed to run an application. This is commonly used withDocker.

Once those resources are deployed, the container can be triggered to run on demand withECS run-task. Once the code is done running and the container exits, then the running ECS Task will disappear. The benefit is that you'll no longer be charged money for a running Fargate task.

There's a few ways to run an ECS task on demand:

  • AWS CLI
  • AWS SDK (could invoke from within Lambda etc)
  • In a Step Function state
  • EventBridge Scheduler
  • AWS Console

Here's an example of what this command looks like via the AWS CLI:

aws ecs run-task \  --cluster default \  --task-definition my-task \  --launch-type FARGATE \  --network-configuration "awsvpcConfiguration={subnets=[subnet1,subnet2],securityGroups=[securityGroup1],assignPublicIp=ENABLED}"
Enter fullscreen modeExit fullscreen mode

Show Me the Code

There's afull code example of how to deploy these cloud resources and invoke the deployed task via ecs run-task with the AWS CLI.

Take a look attheREADME file for directions.

Bonus

There are a few more benefits to ECS run-task that I won't mention, but one notable one is the ability to override specific things in the task definition.

For example, if you want to override theCMD defined in theDockerfile used to build the Container Image for the task, you could do this by adding the--overrides flag.

aws ecs run-task \  --cluster default \  --task-definition my-task \  --launch-type FARGATE \  --network-configuration "awsvpcConfiguration={subnets=[subnet1,subnet2],securityGroups=[securityGroup1],assignPublicIp=ENABLED}" \  --overrides '{"containerOverrides":[{"name":"my-container","command":["npm", "run", "migrate"]}]}'
Enter fullscreen modeExit fullscreen mode

I've seen this used for one off commands that need to run every now and then such as database migrations, though I'm certain there are other good use cases. Here'sthe full list of what can be overridden.

Special thanks toChase Douglas for giving me this idea. 🙌🏻

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A vibrant, worldwide group of AWS experts.

More fromAWS Heroes

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp