Last active
April 26, 2017 19:48
-
-
Save alessandrobologna/e9e2c2eca2b4c42d4ec60348ba7d2cfc to your computer and use it in GitHub Desktop.
Custom Task Definition
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
import json | |
import boto3 | |
from botocore.vendored import requests | |
import json | |
SUCCESS = "SUCCESS" | |
FAILED = "FAILED" | |
def lower(s): | |
return s[:1].lower() + s[1:] if s else s | |
def convert(k,s): | |
if k in ('cpu','memory','memoryReservation','containerPort','hostPort','softLimit','hardLimit'): | |
return (k, int(s)) | |
if k in ('essential','readOnly','disableNetworking','privileged','readonlyRootFilesystem'): | |
return (k, s.lower() in ('yes','true','sure','yup')) | |
return (k, s) | |
def transform(d): | |
if type(d) is dict: | |
return dict([(convert(lower(k), transform(v))) for k, v in d.items()]) | |
elif type(d) is list: | |
return [transform(i) for i in d] | |
else: | |
return d | |
def send(event, context, status, data, physicalResourceId, reason=None): | |
url = event['ResponseURL'] | |
body = {} | |
body['Status'] = status | |
body['Reason'] = reason or 'See the details in CloudWatch Log Stream: ' + context.log_stream_name | |
body['PhysicalResourceId'] = physicalResourceId or context.log_stream_name | |
body['StackId'] = event['StackId'] | |
body['RequestId'] = event['RequestId'] | |
body['LogicalResourceId'] = event['LogicalResourceId'] | |
body['Data'] = data | |
json_body = json.dumps(body) | |
print "Response body:\n" + json_body | |
headers = {'content-type' : '', 'content-length' : str(len(json_body))} | |
try: | |
response = requests.put(url,data=json_body,headers=headers) | |
print "Status code: " + response.reason | |
except Exception as e: | |
print "send(..) failed executing requests.put(..): " + str(e) | |
def lambda_handler(event, context): | |
data = {} | |
reason="" | |
print ("Received event: \n" + json.dumps(event)) | |
if event['RequestType'] == "Delete": | |
send(event, context, SUCCESS, data, None, "Deleted") | |
return | |
region =event['ServiceToken'].split(':')[3] | |
taskdef=event['ResourceProperties'] | |
if region and taskdef: | |
try: | |
taskdef.pop("ServiceToken") | |
client = boto3.client('ecs', region) | |
taskdef=transform(taskdef) | |
print ("Registering task: \n" + json.dumps(taskdef)) | |
response = client.register_task_definition(**taskdef) | |
print ("Response: \n" + json.dumps(response)) | |
response = response['taskDefinition'] | |
arn=response['taskDefinitionArn'] | |
data['Family'] = response['family'] | |
data['Revision'] = response['revision'] | |
send(event, context, SUCCESS, data, arn, "Success") | |
return | |
except Exception as e: | |
reason=str(e) | |
send(event, context, FAILED, data, None, reason) | |
return | |
else: | |
reason="Missing TaskDefinition" | |
print reason | |
# if anything goes wrong | |
send(event, context, FAILED, data, None, reason) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment