Last active
February 9, 2023 09:23
-
-
Save fermayo/02b0f69cd942115f8c70e6802516f368 to your computer and use it in GitHub Desktop.
Lambda function to trigger a one-off ECS Fargate task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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