Skip to content

Instantly share code, notes, and snippets.

@fermayo
Last active February 9, 2023 09:23
Show Gist options
  • Save fermayo/02b0f69cd942115f8c70e6802516f368 to your computer and use it in GitHub Desktop.
Save fermayo/02b0f69cd942115f8c70e6802516f368 to your computer and use it in GitHub Desktop.
Lambda function to trigger a one-off ECS Fargate task
# Adapted from https://lobster1234.github.io/2017/12/03/run-tasks-with-aws-fargate-and-lambda/
import boto3
import os
def lambda_handler(event,context):
client = boto3.client('ecs')
response = client.run_task(
cluster=os.getenv('CLUSTER'),
launchType=os.getenv('LAUNCH_TYPE', 'FARGATE'),
taskDefinition=os.getenv('TASK_DEFINITION'),
count=int(os.getenv('COUNT', 1)),
platformVersion='LATEST',
networkConfiguration={
'awsvpcConfiguration': {
'subnets': os.getenv('SUBNETS').split(','),
'assignPublicIp': os.getenv('ASSIGN_PUBLIC_IP', 'ENABLED'),
'securityGroups': os.getenv('SECURITY_GROUPS').split(','),
},
}
)
return str(response)
# Required env vars:
# $CLUSTER: name of the ECS cluster
# $TASK_DEFINITION: name and revision of the task definition (i.e. `mytask:1`)
# $SUBNETS: comma-separated list of subnets to place the new task
# $SECURITY_GROUPS: comma-separated list of security groups to be used for the new task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment