-
-
Save kandy/c63e99ebdbd2315ab6b73f2f414a337b to your computer and use it in GitHub Desktop.
CloudFormation Custom Task Definition POC
This file contains hidden or 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
# Sources: | |
# https://cloudonaut.io/how-to-create-a-customized-cloudwatch-dashboard-with-cloudformation/ | |
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html | |
# https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/ECS.html | |
Resources: | |
CustomTaskDefinition: | |
Type: 'Custom::TaskDefinition' | |
Version: '1.0' | |
Properties: | |
ServiceToken: !GetAtt 'CustomResourceFunction.Arn' | |
TaskDefinition: { | |
containerDefinitions: [ | |
{ | |
name: "sleep", | |
image: "busybox", | |
command: [ | |
"sleep", | |
"360" | |
], | |
mountPoints: [ | |
{sourceVolume: "efs", containerPath: "/efs"} | |
] | |
} | |
], | |
family: "sleep360", | |
taskRoleArn: "", # required for EFS permissions | |
cpu: "256", | |
memory: "512", | |
networkMode: "awsvpc", | |
volumes: [ | |
{ | |
name: "efs", | |
efsVolumeConfiguration: { | |
fileSystemId: "" # required for EFS | |
} | |
} | |
] | |
} | |
CustomResourceFunction: | |
Type: 'AWS::Lambda::Function' | |
Properties: | |
Code: | |
ZipFile: | | |
const aws = require('aws-sdk') | |
const response = require('cfn-response') | |
const ecs = new aws.ECS({apiVersion: '2014-11-13'}) | |
exports.handler = function(event, context) { | |
console.log("REQUEST RECEIVED:\n" + JSON.stringify(event)) | |
if (event.RequestType === 'Create' || event.RequestType === 'Update') { | |
ecs.registerTaskDefinition(event.ResourceProperties.TaskDefinition, function(err, data) { | |
if (err) { | |
console.error(err); | |
response.send(event, context, response.FAILED) | |
} else { | |
console.log(`Created/Updated task definition ${data.taskDefinition.taskDefinitionArn}`) | |
response.send(event, context, response.SUCCESS, {}, data.taskDefinition.taskDefinitionArn) | |
} | |
}) | |
} else if (event.RequestType === 'Delete') { | |
ecs.deregisterTaskDefinition({taskDefinition: event.PhysicalResourceId}, function(err) { | |
if (err) { | |
console.error(err); | |
response.send(event, context, response.FAILED) | |
} else { | |
console.log(`Removed task definition ${event.PhysicalResourceId}`) | |
response.send(event, context, response.SUCCESS) | |
} | |
}) | |
} else { | |
console.error(`Unsupported request type: ${event.RequestType}`) | |
response.send(event, context, response.FAILED) | |
} | |
} | |
Handler: 'index.handler' | |
MemorySize: 512 | |
Role: !GetAtt 'CustomResourceRole.Arn' | |
Runtime: 'nodejs10.x' | |
Timeout: 30 | |
CustomResourceRole: | |
Type: 'AWS::IAM::Role' | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: 'lambda.amazonaws.com' | |
Action: 'sts:AssumeRole' | |
Policies: | |
- PolicyName: 'customresource' | |
PolicyDocument: | |
Statement: | |
- Effect: Allow | |
Action: | |
- 'ecs:DeregisterTaskDefinition' | |
- 'ecs:RegisterTaskDefinition' | |
Resource: '*' | |
- Effect: Allow | |
Action: | |
- 'logs:CreateLogGroup' | |
- 'logs:CreateLogStream' | |
- 'logs:PutLogEvents' | |
Resource: '*' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment